Spring migration Annotated controllers and the traditional onSubmit method

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

I used to define a MyController controller inheriting from SimpleFormController and had some logic written in the onSubmit method. All my controllers that have handler methods inherit from MyController . Thus, the logic written in onSubmit of MyController is used to execute for all requests.

Now that I move on to the annotated controller, in which my controller is a simple pojo, how can I ensure that onSubmit executed every time? One way is to call onSubmit from all the handler methods of all controllers. This is cumbersome.

Can anyone suggest any possible solution. How does the annotation formBackingObject with @ModelAttribute provide a call for all requests, is there an analogy to the onSubmit method?

+4
source share
3 answers

If you want to perform the same action before each call to any annotated controller, you can use an interceptor. You can write your own interceptor simply by using the preHandle method. Then you will need to register this interceptor in the DefaultAnnotationHandlerMapping file or any Handler mapping that you use to send to your controllers. Interceptor registration is explained in this article: http://www.scottmurphy.info/spring_framework_annotation_based_controller_interceptors

+1
source

Annotate the method you want to call. The signature of the method is very flexible. Take a look at the docs for @RequestMapping

 @RequestMapping(value={"/foo"}, method=RequestMethod.POST) public String myMethod(many options for parameters) {... 
0
source

Good, therefore, if I understand correctly, you want inheritance to continue to play a role on the stack when the request is processed by the controller. You can extend any class in annotated POJO @RequestMapping, but you will need to define an @override method to annotate it. All you do basically is call super with the arguments in the override method. If you extend the annotated class and both are declared as Controller, then you will get an exception, since the route will be defined more than once.

he will look like

 public class Pojo{ public String someBaseMethod(){ return ""; } } @Controller public class ChildController extends Pojo { @Override @RequestMapping("/do_it") public String someBaseMethod() { return super.someBaseMethod(); } } 

A good case can be made to use composition over inheritance. I even suggest using a filtering mechanism if it can be used to perform general operations. AOP can also be a good tool.

0
source

All Articles