Spring MVC中的拦截器 [英] interceptors in spring MVC

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

问题描述

我正在尝试在春季学习拦截器,但无法理解.我尝试了一个示例示例,但没有成功.我创建了一个简单的拦截器,如

I am trying learn Interceptors in spring but not able to understand it. I tried a sample example but get no success. I have created a simple interceptor like

@Component("testInterceptor")
public class testInterceptor extends HandlerInterceptorAdapter {      
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response,   Object handler){
//I suppose this method will be called when I open my login page so just write something here       
        System.out.println("Inside Interceptor...");
        ModelAndView mv = new ModelAndView();
        mv.addObject("name", "test name");
        return true;
}

}

在我的spring-servlet.xml中,我有

In my spring-servlet.xml I have

<context:component-scan base-package="com.test.interceptor" />
 <mvc:interceptors>
            <bean class="com.test.interceptors.testInterceptor " />         
</mvc:interceptors>

在登录页面上,我在$ {name}

On my login page I write somewhere ${name}

所以我什至都看不到$ {name}的值或sysout.

So I can't even see even ${name} value or sysout anywhere.

我什至无法理解何时将调用postHandler或afterCompletion方法,以及在实现之前如何测试它们.这可能是因为我对春季也很陌生,但我无法完全理解它.如果您建议阅读一些好的链接以显示一些示例,将对您大有帮助.

I am not even able to understand when postHandler or afterCompletion method will be called and how I can test them before implementing. This may be because I am new to spring also and I am not able to understand it fully. It would be great help if you suggest some good link to read which shows some examples.

预先感谢

现在,当我调用登录页面时,可以看到我的sysout commnet,但是它显示了16次,为什么呢?

Edited: Now I can see my sysout commnet when I call my login page but it displaying it 16 times why is this so?

推荐答案

首先查看接口

First have a look at the Interface HandlerInterceptor it is very well documented! (HandlerInterceptorAdapter is only a subclass that helps you if you do not want to implement all 3 methods) .

然后您会发现有3种方法,每种方法都属于处理"链中的一个步骤.

Then you will notice that there are 3 methods, each belong to one step in the "processing" chain.

然后您会发现您使用了错误的方法:使用postHandle而不是preHandle.

Then you will notice that you used the wrong method: use postHandle instead of preHandle.

然后,您会注意到在过滤器ModelAndView mv = new ModelAndView();中创建的模型图未连接到某物,因此无法使用!但是幸运的是postHandle有一个ModelAndView modelAndView参数.而且,您必须使用它而不是创建未连接的模型图.

Then you will notice that the model map that you have created in your filter ModelAndView mv = new ModelAndView(); is not connected to something, and therefore it can not work! But fortunately postHandle has a ModelAndView modelAndView parameter. And you must use this instead of creating your unconnected model map.

也许以后您会发现您两次创建了过滤器.一次通过组件扫描,一次通过xml声明. (在这种情况下,我建议删除@Compnent注释)

Maybe later you will notice that you created the filter twice. Once by component scan and once by xml declaration. (In this case I would recommend to remove the @Compnent Annotation)

所以最后您的课程看起来像:

So at the end your class would look like:

public class testInterceptor extends HandlerInterceptorAdapter {      
   @Override
   public boolean postHandle(HttpServletRequest request,
            HttpServletResponse response,
             Object handler,
            ModelAndView modelAndView){
       modelAndView.addObject("name", "test name");
   }
 }

最后,您会发现(我不确定100%确定)该拦截器未针对Spring安全性登录请求(j_spring_security_check)或注销被调用.因为这是在调用任何HandlerInterceptor之前应用的spring安全过滤器中处理的.

At the end you will notice (I am not 100% sure) that this interceptor is not invoked for the spring security login request (j_spring_security_check) or logout. Because this is handled in a spring security filter that is applied before any HandlerInterceptor is called.

(注释)现在,当我调用登录页面时,我可以看到我的sysout注释,但是它将显示16次,为什么呢?

(comment) Now I can see my sysout comment when I call my login page but it is displaying it 16 times why is this so?

也许是因为您正在通过控制器加载一些资源(图像,css,js).

Maybe because you are loading some resources (images, css, js) though a controller.

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

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