使用 Spring MVC 可以在控制器本身中定义过滤方法

发布于 2024-12-13 08:41:56 字数 217 浏览 3 评论 0原文

我知道使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没企图 2024-12-20 08:41:56

如果我可以分配控制器的一种方法,那就太好了

不,没有内置方法来完成这项工作。


但是你可以做一个非常非常肮脏的黑客:用@ModelAttribute注释的方法将在调用控制器方法之前执行。

Spring参考: 第 15.3.2.8 章使用 @ModelAttribute 提供模型数据的链接

注意

@ModelAttribute 注解的方法在选择的方法之前执行
@RequestMapping 注解的处理方法。他们有效地
使用通常加载的特定属性预先填充隐式模型
来自数据库。这样的属性就已经可以被访问了
通过@ModelAttribute注解的handler方法中的参数
选择的处理程序方法,可能应用绑定和验证
到它。

但我强烈建议不要这样做,而是使用 AOP!

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!

娜些时光,永不杰束 2024-12-20 08:41:56

上述解决方法可能工作正常,但如果您想拦截方法调用,它们可能位于控制器中或您应该使用方面的其他任何地方。

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

祁梦 2024-12-20 08:41:56

听起来您只想将一些常见逻辑应用于给定控制器中的方法(否则使用过滤器)。如果是这样,为什么不将该逻辑放入私有方法中并从所有请求映射方法中调用它?这将为每个方法添加一行。无论如何,将方面或拦截器应用于每个方法都需要添加至少一行(例如注释)。吻。

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.

情痴 2024-12-20 08:41:56

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文