java - springmvc mvc:exclude-mapping 不起作用
天蓬老师
天蓬老师 2017-04-18 10:23:23
0
3
948

在springmvc配置文件中加入下面的代码,路由为"/login"的url还是会被拦截到拦截器中

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/login" />
            <bean class="com.stooges.common.interceptor.LoginInterceptor">             </bean>
        </mvc:interceptor>
    </mvc:interceptors>

拦截器LoginInterceptor

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        if (session.getAttribute(Constants.SESS_MANAGER) == null) {
            if (request.getHeader("x-requested-with") != null
                    && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){ //如果是ajax请求响应头会有,x-requested-with
                response.setHeader("sessionstatus", "timeout");//在响应头设置session状态
                response.setHeader("redirectUrl", request.getContextPath() + "/login");
            }else{
                String path = request.getRequestURI();////原页面

                //根据方法不同拼接参数
                String queryString="";
                if(request.getMethod().equals("GET")){
                    queryString = request.getQueryString();
                }else{
                    Enumeration<String> params=request.getParameterNames();
                    while(params.hasMoreElements()){
                        String paraName=params.nextElement();
                        queryString+=paraName+"="+request.getParameter(paraName)+"&";
                    }
                }
                if (queryString!=null && (!queryString.equals(""))) {
                    path+="?"+queryString;
                }
                response.sendRedirect(request.getContextPath() + "/login");
            }
            return false;
        }else{
            return true;
        }
    }
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(3)
大家讲道理

The reason found out today is that the returned view was also intercepted by the interceptor and redirected all the time. Just modify web.xml

The following is to configure all requests to be handled by springmvc

  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

Static resources configured below

  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/dist/*</url-pattern>
    <url-pattern>/WEB-INF/view/*</url-pattern>
  </servlet-mapping>

Corresponding to <mvc:default-servlet-handler/>
default-servlet-handler in springmvc.xml will define a DefaultServletHttpRequestHandler in the SpringMVC context, which will screen the requests entering the DispatcherServlet. If it is found that there is no mapping request, the request will be handed over to the WEB The default Servlet processing of the application server. If the request is not a static resource, it will continue to be processed by the DispatcherServlet

Generally, the default Servlet name of a WEB application server is default. If the default Servlet name of the WEB server used is not default, you need to specify it explicitly through the default-servlet-name attribute

左手右手慢动作

Whether your interceptor inherits the class HandlerInterceptorAdapter and overrides the method.
Perhaps you can create a regular pattern without interception, as follows:


Then make regular judgment in the interception method preHandle

洪涛

<mvc:exclude-mapping path="/login" />Wrong writing?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template