Application-wide process in SpringMVC

会有一股神秘感。 提交于 2021-01-29 11:11:16

问题


Suppose I have a certain operation that should be available to every process running in Spring MVC. Say string normalization-- i need to run a method that normalizes the string fields before doing anything else on that form/data.

One thing specific to do is, to normalize the String fields on every input form before they are dispatched to the back-end services. Likewise, that operation (normalization) should be run on data from the back-end before it is dispatched to the view component.

One way of doing this that I can think of is:

Code a bean doing it-- the normalization. Then, define this bean somewhere at the top in the context hierarchy of Spring-- ApplicationContext.xml or WebApplicationContext.xml(?), so that it will be visible and can be used accross all the processes/servlets in the application. Then, Whenever and from wherever needed, invoke that method on the bean defined up there. Or, inject it to the relevant fields in the bean definitions(?)

In this case, is there a way to call it before or during a HandlerMapping is running? if so, how?

Another i can come up with is:

Code a validator (implement Validator) to run that process and "validate" the String fields for you. But i dont see how this would be of good help. From what i know, a validator runs on specific object types. I can define that type generically(?) but then I'm operating on the fields-- not objects as a whole each. Coding validator(s) seems too costly to me for this use-- even if it is an option here.

I'm new to Spring. pls bear with me on this.

来源:https://stackoverflow.com/questions/20411864/application-wide-process-in-springmvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!