拦截Spring MVC 3中的视图/响应 [英] Intercept the view/response in Spring MVC 3

查看:133
本文介绍了拦截Spring MVC 3中的视图/响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring MVC 3的新手,我理解基本概念。我能够做一些简单的事情,比如创建控制器,服务和视图。但是,我没有进入更先进的领域。因此,如果这个问题看起来很愚蠢(或不可能),我道歉。

I am new to Spring MVC 3 and I understand the basic concepts. I am able to do simple things like create controllers and services and views. However, I haven't made a foray into more advanced territory. Hence, I apologize if this question seems silly (or impossible).

我想知道是否有办法拦截视图和/或响应并在它之前修改它被送到客户端?我想这就是Spring在向客户端发出的过程中执行数据绑定以形成元素的方式。我想做的是检查域类中元素的注释,并根据这些注释修改视图。这将涉及在响应中注入新代码(HTML或Javascript)。

I am wondering if there is a way to intercept the view and/or response and modify it before it gets sent to the client? I imagine this is how Spring performs data binding to form elements on the way out to the client. What I would like to do is inspect annotations on elements within a domain class and modify the view according to those annotations. This will involve injecting new code (HTML or Javascript) into the response.

更新

当我想到这一点时,我意识到最终的渲染是由JSP完成的。所以我想问题是,是否有一种方法可以在模型移出页面之前拦截模型,并找出数据被绑定到bean上的注释。

As I thought about this a bit more, I realized that the final rendering is done by the JSP. So I guess the question is if there is a way to intercept the model before it moves out to the page and to figure out the annotations on the bean that the data is being bound to.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

你可能正在寻找的课程是 org.springframework.web.servlet.HandlerInterceptor 您可以在该接口上实现postHandle方法,并且如签名所示,可以访问请求和响应,以及地图控制器创建的模型对象。 (和控制器本身,这是对象处理程序参数是什么。)

The class you're probably looking for is org.springframework.web.servlet.HandlerInterceptor You can implement the postHandle method on that interface and, as the signature implies, have access to both the request and the response, as well as the map of model objects that your controller created. (and the controller itself, that's what the Object handler parameter is.)

你'打开它'将它们添加到调度程序servlet中的处理程序映射。

You 'turn them on' by adding them to the handler mapping in your dispatcher servlet.

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <list>
            <bean class="a.package.MyHandlerInterceptor"/>
        </list>
    </property>
</bean>

顺便提一下,绑定实际上是在HandlerAdapter里面完成的,它定位了Controller方法并调用它们,它不是拦截器。

Incidentally, binding is actually done inside the HandlerAdapter that locates Controller methods and invokes them, it's not an interceptor.

编辑:
要回答您的编辑,是的,您可以在控制器完成后抓取模型对象并更多地使用它。完成,但在进入JSP渲染之前。所以你可以做一些事情,比如将 ModelMap 并在 $ {myCustomScript} >< head> 你的jsp,从 ModelMap 中获取一个支持对象并检查它等等。

To answer your edit, yes this is where you have a chance to grab the model object and work with it more, after the controller is done, but before it goes to JSP rendering. So you could do something like add myCustomScript to the ModelMap and toss ${myCustomScript} in the <head> of your jsp, get a backing object out of the ModelMap and examine it, etc etc.

这篇关于拦截Spring MVC 3中的视图/响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆