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

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条回答 默认 最新

  • devmiao 2017-04-10 15:56
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 7月28日

悬赏问题

  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡