码上敲享录 > Spring框架常见问题详解分享 > springmvc请求不要后缀名

springmvc请求不要后缀名

上一章章节目录下一章 2019-03-09已有152人阅读 评论(0)

springmvc请求不要后缀名,例如http://127.0.0.1:8080/plant/list 而不是http://127.0.0.1:8080/plant/list.do


解决方法
1.web.xml配置
<servlet-mapping>
       <servlet-name>Spring-Servlet</servlet-name>
       <url-pattern>/</url-pattern>
   </servlet-mapping>


2.springmvc配置文件配置:
<!-- 配置 spring 拦截所有请求 -->
   <mvc:interceptors>
       <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
       <bean class="com.interceptor.AuthInterceptor" />
   </mvc:interceptors>

   <!--  配置静态不拦截的目录页面   -->
   <mvc:resources mapping="/css/**" location="/css/"   cache-period="8643000"/>
   <mvc:resources mapping="/js/**" location="/js/"   cache-period="8643000"/>
   <mvc:resources mapping="/images/**" location="/images/"   cache-period="8643000"/>


3.com.interceptor.AuthInterceptor拦截器中判断是静态资源就放过:
package com.manage.base.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class AuthInterceptor extends HandlerInterceptorAdapter {
   @Override
   public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
   
           String path = request.getServletPath().toLowerCase().replace("//", "/");
           
               if(path.startsWith("/images/")||path.startsWith("/css/")||path.startsWith("/js/")||path.startsWith("/plugins/")||path.startsWith("/upload/")){
                   return super.preHandle(request, response, handler);                
               }else{
                   return false;
               }
           
             
           
   }
   @Override
   public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex)throws Exception {
           super.afterCompletion(request, response, handler, ex);
   }
   
}

   


0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交