zgsdzczh 2017-04-10 14:46 采纳率: 100%
浏览 1743
已采纳

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条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 已采纳回答 7月28日

      悬赏问题

      • ¥15 unity 使用 Vuforia 导入Model target 成功后database下拉菜单不显示导入的内容
      • ¥15 VBA跨工作簿获取目标工作簿内已有数据的列号
      • ¥30 Dijkstra‘s 最短路径
      • ¥15 在线教育培训平台,主要以点播视频和在线练习测试为主,除将视频做切片外,有其他哪种方式降低流量?(关键词-带宽速率)
      • ¥20 用c语音或c++实现银行叫号系统
      • ¥15 人工智能 规则正向演绎和推理
      • ¥20 基于STM32F401的电子密码锁设计
      • ¥15 famamacbeth回归中遇到only size-1 arrays can be converted to Python scalars,求解答
      • ¥15 单片机多个自锁按键的编程实践
      • ¥15 用python操作redis存储中文后,再取出的数据变成了乱码怎么办?