Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migration to Spring Annotated Controllers and traditional onSubmit method

I am trying to migrate from Spring 2.0 to Spring 3.0.

Previously I defined a controller MyController inheriting from SimpleFormController and have some logic written in the onSubmit method. All my controllers having the handler methods are inherited from MyController. Thus, the logic written in onSubmit of MyController used to get executed for all requests.

Now as I migrate to annotated controller wherein my controller is a simple pojo, how do I ensure the execution of onSubmit everytime? One way is to call onSubmit from all handler methods of all the controllers. This is cumbersome.

Can anyone suggest any feasible solution. As annotating formBackingObject with @ModelAttribute ensures the invocation for all requests, isn't there an analogy for onSubmit method?

like image 311
Chayan Avatar asked Nov 14 '22 23:11

Chayan


1 Answers

If you want to perform the same action before each invokation of any annotated controller, you could use an interceptor. You can write your own interceptor by just implementing the preHandle method. You will then need to register this interceptor in the DefaultAnnotationHandlerMapping or whatever Handler mapping you use to dispatch to your controllers. Registering interceptors is explained in this article: http://www.scottmurphy.info/spring_framework_annotation_based_controller_interceptors

like image 191
Kristiaan Avatar answered Jan 03 '23 10:01

Kristiaan