使用 Spring MVC 可以在控制器本身中定义过滤方法
我知道使用 Spring MVC 可以使用 RequestMapping 注释来注释整个控制器类。还可以使用Requestmapping注释单独的方法,以便它们的每个请求映射都相对于整个类的请求映射。
如果我可以分配控制器的一种方法,作为某种每个控制器的过滤器,它会在给定方法的每个相应的操作方法之前执行,那就太好了。是否可能,或者我应该保留使用单独的 Filter 类的现有方式(我希望这是可以避免的)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,没有内置方法来完成这项工作。
但是你可以做一个非常非常肮脏的黑客:用
@ModelAttribute
注释的方法将在调用控制器方法之前执行。Spring参考: 第 15.3.2.8 章使用 @ModelAttribute 提供模型数据的链接
但我强烈建议不要这样做,而是使用 AOP!
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
But I strongly recommend not to do this hack, instead use AOP!
上述解决方法可能工作正常,但如果您想拦截方法调用,它们可能位于控制器中或您应该使用方面的其他任何地方。
http://static.springsource.org /spring/docs/3.0.x/spring-framework-reference/html/aop.html
查找
aop:config 或 @Aspect
另外,对于控制器,拦截器可能会起作用
http://static .springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-handlermapping-interceptor
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
听起来您只想将一些常见逻辑应用于给定控制器中的方法(否则使用过滤器)。如果是这样,为什么不将该逻辑放入私有方法中并从所有请求映射方法中调用它?这将为每个方法添加一行。无论如何,将方面或拦截器应用于每个方法都需要添加至少一行(例如注释)。吻。
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.
ModelAttribute 的目的完全不同。
我们可以通过创建 MethodInterceptor 的自定义拦截器实现(来自 aopalliance)并将其用作 AOP Advisor 来实现。
在您的应用程序中为您的请求控制器类创建一个 aop 配置,并将其添加为您为此控制器类创建的连接点的顾问。
然后,该顾问程序将在您的方法调用之前被调用。
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.