1

I know that using Spring MVC it is possible to annotate an entire controller class with a RequestMapping annotation. It is also possible to annotate separate methods with Requestmapping, so that each of their request mappings is relative to the request mapping of the entire class.

It would be great if then I could assign one method of the controller, as a some sort of per-controller filter, which gets executed before every corresponding action method of the given method. Is it possible, or I should keep to the existing way of using a separate Filter class for that (which is something that could be avoided I hope)

4 Answers 4

1

It would be great if then I could assign one method of the controller

No, there is no built-in method to do this job.


But you can do a very very dirty hack: a method annotated with @ModelAttribute will be executed before the controller methods will invoked.

Spring Reference: Chapter 15.3.2.8 Providing a link to data from the model with @ModelAttribute

Note

@ModelAttribute annotated methods are executed before the chosen @RequestMapping annotated handler method. They effectively pre-populate the implicit model with specific attributes, often loaded from a database. Such an attribute can then already be accessed through @ModelAttribute annotated handler method parameters in the chosen handler method, potentially with binding and validation applied to it.

But I strongly recommend not to do this hack, instead use AOP!

Sign up to request clarification or add additional context in comments.

Comments

0

The above workaround might work fine, but if you want to intercept method calls may them be in controllers or anywhere else you should use aspects.

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html

Look for

aop:config or @Aspect

Additionally for controllers the interceptors might work

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor

Comments

0

It sounds like you only want to apply a bit of common logic to methods within a given controller (otherwise use a Filter). If that's so, why not put that logic in a private method and call it from all the request mapped methods? This would add one line to each method. Applying an aspect or interceptor to each method you would need at least one line (e.g. an annotation) added anyhow. KISS.

Comments

0

The purpose of ModelAttribute is completely different . we can do it by creating a custom interceptor implementation of MethodInterceptor (from aopalliance) and using it as a AOP advisor.

Create a aop configuration in your application for your Request controller class and add this as advisor with respect to the join point you have created for this controller class.

Then this advisor will be invoked before your method invocation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.