Springmvc拦截器导致controllor不执行

拦截器代码:

  @Override    
    public boolean preHandle(HttpServletRequest request,    
            HttpServletResponse response, Object handler) throws Exception {    
        logger.warn("==============执行顺序: 1、preHandle================");    
        String requestUri = request.getRequestURI();  
        String contextPath = request.getContextPath();  
        String url = requestUri.substring(contextPath.length());  

        logger.warn("requestUri:"+requestUri);    
        logger.warn("contextPath:"+contextPath);    
        logger.warn("url:"+url);    
        String username =  (String)request.getSession().getAttribute("un");   
        System.out.println(username);
        if(username == null){  
            logger.warn("Interceptor:跳转到login页面!");  
    //        response.sendRedirect("loginStuUI.jsp");  
            request.getRequestDispatcher("loginStuUI.jsp").forward(request, response);  
            return false;  
        }else  
            logger.warn("登录成功");  
            return true;     
    }    


    @Override    
    public void postHandle(HttpServletRequest request,    
            HttpServletResponse response, Object handler,    
            ModelAndView modelAndView) throws Exception {     
        logger.warn("==============执行顺序: 2、postHandle================");    
        response.sendRedirect("/loginStuUI.jsp");  
        if(modelAndView != null){  //加入当前时间    
            modelAndView.addObject("var", "测试postHandle");    
        }    
    }    

    @Override    
    public void afterCompletion(HttpServletRequest request,    
            HttpServletResponse response, Object handler, Exception ex)    
            throws Exception {    
        logger.warn("==============执行顺序: 3、afterCompletion================");    
    }    

Springmvc.xml代码:

 <context:annotation-config />
    <context:component-scan base-package="com.iss.controllor"></context:component-scan>


    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/admin/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean
                class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <mvc:default-servlet-handler />
<mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.iss.controllor.CommonInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

controllor代码:

 @RequestMapping("/login")
    public String login(User user, Map map, HttpSession session) {
        System.out.println("login");
        if (userServiceImpl.userLogin(user)) {
            session.setAttribute("user", user);
            session.setAttribute("un", user.getUname());
            map.put("unameMsg",user.getUname());
            logger.warn(user.getUname() + "成功登陆一次");
            return "iiindex";
        } else {
            map.put("loginMsg", "登录失败,请重试");
            return "../loginStuUI"; 
        }
    }

登录界面代码:

 <form action="login" id="signupForm" method="post">
        ${loginMsg}
            <input type="text" name="uname" 
                            placeholder="请输入登录名" required>
            <input type="password" name="upwd" 
                            placeholder="请输入密码" required>
            <input type="submit" name="submit" id="submit" value="登录"
                        class="btn-style-01"> 
                        <a href="register2.jsp" >注册</a>
        </form>

控制台输出语句:

 2017-04-10 22:46:10 WARN [com.iss.controllor.CommonInterceptor] ==============执行顺序: 1、preHandle================
 2017-04-10 22:46:10 WARN [com.iss.controllor.CommonInterceptor] requestUri:/Test1/login
 2017-04-10 22:46:10 WARN [com.iss.controllor.CommonInterceptor] contextPath:/Test1
 2017-04-10 22:46:10 WARN [com.iss.controllor.CommonInterceptor] url:/login
 null
2017-04-10 22:46:10 WARN [com.iss.controllor.CommonInterceptor] Interceptor:跳转到login页面!

假如把拦截器设置成如下:

         if(username == null){  
            logger.warn("登录成功");  
            return true;     
        }else  

则可执行controllor 控制台输出如下所示:

 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] ==============执行顺序: 1、preHandle================
 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] requestUri:/Test1/login
 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] contextPath:/Test1
 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] url:/login
 null
2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] 登录成功
 login
2017-04-10 23:10:17 WARN [com.iss.controllor.ACTION] admin成功登陆一次
 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] ==============执行顺序: 2、postHandle================
 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] ==============执行顺序: 3、afterCompletion================
 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] ==============执行顺序: 1、preHandle================
 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] ==============执行顺序: 1、preHandle================
 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] requestUri:/Test1/admin/img/icon_san.jpg
 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] contextPath:/Test1
 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] url:/admin/img/icon_san.jpg
 admin
2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] ==============执行顺序: 1、preHandle================
 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] requestUri:/Test1/admin/img/dyz.JPG
 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] contextPath:/Test1
 2017-04-10 23:10:17 WARN [com.iss.controllor.CommonInterceptor] url:/admin/img/dyz.JPG
 admin

请问一下这是什么原因导致的 谢谢

1个回答

zgsdzczh
zgsdzczh 你好 这个不是很明白 能否详细说一下
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问