Click here to Skip to main content
15,886,817 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to determine whether he has rights to access the url or not.But I want to give all the css,js can be accessed.So I config the web.xml like this:
Java
    <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-  class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

    <mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**" />
        <bean class="com.test.web.interceptor.MyInterceptor">
            <property name="loginUrl">
                <value>http://localhost:8080/my-web/login.jsp</value>                   
            </property> 
        </bean>
    </mvc:interceptor>
</mvc:interceptors> 

and set spring-servlet.xml like this:
Java
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/js/" mapping="/js/**" />

and MyInterceptor like this:
Java
public class MyInterceptor implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest arg0,
        HttpServletResponse arg1, Object arg2, Exception arg3)
        throws Exception {
    // TODO Auto-generated method stub

}

@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
        Object arg2, ModelAndView arg3) throws Exception {
    // TODO Auto-generated method stub

}

@Override
public boolean preHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler) throws Exception {
    response.setCharacterEncoding("UTF-8");
    StringBuffer currentUrl = request.getRequestURL(); 
    String path = request.getServletPath(); 
     if(path.matches( "(.*/((css)|(js)|(images)).*)"
    )){
        System.out.println(path);
        return true;
    }
     //check token,if null return to login.jsp
  }

After this,I think that the css and js request can be accessed and not go to my interceptors.BUt it is wrong.Now I have two questions:
I have set the mvc:resources,the book say that it will not be processed by DispatcherServlet,Why do the css or js url request still go to MyInterceptor?
I visit the index.jsp page,in MyInterceptor I can get the css request,js request,but Why do I not get the jsp page request?

What I have tried:

I have search the intenet and try to code two hours.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900