年前做网站的好处,远洋国际一期官方网站建设,网站空间送域名,做企业网站制作上一篇我们详细解释了一下SrpingMVC的执行流程以及一些默认的配置#xff0c;在Spring的思想中#xff0c;就是默认大于配置。今天我们来详细的研究一下DispatcherServlet的url-pattern配置。 一、DispatcherServlet的url-pattern配置在没有特别要求的情况下#xff0c;Spri…上一篇我们详细解释了一下SrpingMVC的执行流程以及一些默认的配置在Spring的思想中就是默认大于配置。今天我们来详细的研究一下DispatcherServlet的url-pattern配置。 一、DispatcherServlet的url-pattern配置在没有特别要求的情况下SpringMVC的中央调度器DispatcherServlet的url-pattern常使用后缀匹配方式进行配置如*.do、*.action注意这里的url-pattern不能写/*因为DispatcherServlet会将向JSP的动态页面跳转请求也当作为普通的Controller来处理。中央调度器在调用处理器映射器来为其查找相应的处理器时肯定找不到。所以在这种情况下所有的JSP页面跳转都会变为404。最好也不要写成/因为DispatcherServlet会将向静态资源的请求当作为普通的Controller来处理。如.css、.jpg、.js等。所以静态资源也会变成404。所以建议写成*.do、*.action之类的配置。当然也有一些时候不得不配置成/当开发一些移动端接口采用restful请求时需要配置成/。 二、url-pattern配置为/时静态资源的访问1使用tomcat的默认Servlet解决在web.xml中添加如下代码 servlet-mappingservlet-namedefault/servlet-nameurl-pattern*.js/url-pattern
/servlet-mapping 注意上方只处理*.js如果需要大家可以再加几个拦截其它资源。使用该配置只需要配置servlet-mapping即可default的Servlet配置在tomcat的conf/web.xml文件中。如下图 具体的解释在该段代码的上方注释里。 !-- The default servlet for all web applications, that serves static --
!-- resources. It processes all requests that are not mapped to other --
!-- servlets with servlet mappings (defined either here or in your own --
!-- web.xml file). This servlet supports the following initialization --
!-- parameters (default values are in square brackets): -- 该default的servlet对所有的web应用程序生效专门处理静态资源。处理所有没有匹配到servlet mappings的请求 2使用SpringMVC的default-servlet-handler解决在springmvc.xml中添加mvc:default-servlet-handler/。当然添加这个default-servlet-handler时需要对当前xml添加mvc的约束xsd。如下图 最终springmvc.xml如下 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:aophttp://www.springframework.org/schema/aopxmlns:txhttp://www.springframework.org/schema/txxmlns:mvchttp://www.springframework.org/schema/mvcxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdmvc:default-servlet-handler/!-- 注册视图解析器 --bean classorg.springframework.web.servlet.view.InternalResourceViewResolverproperty nameprefix value/WEB-INF/jsp/ /property namesuffix value.jsp //bean!-- 注册SpringMVC处理器 --bean id/my.do classcn.wechatbao.controller.MyController/bean
/beans 注意default-servlet-handler会对静态资源的访问请求通过handlerMapping映射到默认的Servlet请求处理器DefaultServletHttpRequestHandler类上。而该类最终调用的是Tomcat的defaultServlet来处理的请求。如图 3使用SpringMVC的resources解决在springmvc.xml中添加如下代码mvc:resources location/images/ mapping/images/**/mvc:resources其中的location和mapping为具体的静态资源文件夹大家可以根据具体的项目来定义。注意该方法是在spring3.0.4版本后专门定义的一个静态资源的处理器ResourceHttpRequestHandler类该种配置文件会将所有的静态资源映射到ResourceHttpRequestHandler该类转载于:https://www.cnblogs.com/xinhudong/p/8323857.html