web.xml中 /和/*的区别

< url-pattern>/</url-pattern>  匹配到/login这样的路径型url不会匹配到模式为*.jsp这样的后缀型url
< url-pattern>/*</url-pattern> 匹配所有url:路径型和后缀型的url(包括/login,*.jsp,*.js和*.html等)

因为 springmvc 要使用DispatcherServlet类 处理 前端的 请求,所以配置为< url-pattern > / </ url-pattern >    将路径型url  交给springMVC处理

例如  中文乱码处理、shiro 安全认证处理需要处理所有的 url ,所以配置为< url-pattern > /* </ url-pattern > 

< url-pattern > / </ url-pattern >   不会匹配到*.jsp,即:*.jsp不会进入spring的 DispatcherServlet类 。
< url-pattern > /* </ url-pattern >  会匹配*.jsp,会出现返回jsp视图时再次进入spring的DispatcherServlet 类,导致找不到对应的controller所以报404错。 

 

 

例某 web.xml文件配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3    <!-- 配置spring   上下文参数 -->
 4   <context-param>
 5     <param-name>contextConfigLocation</param-name>
 6     <param-value>classpath:applicationContext.xml</param-value>
 7   </context-param>
 8    <!-- 配置spring ---- 配置侦听器 -->
 9   <listener>
10     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
11   </listener>
12   <!-- 配置springmvc  核心控制器 -->
13   <servlet>
14     <servlet-name>springDispatcherServlet</servlet-name>
15     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16     <init-param>
17       <param-name>contextConfigLocation</param-name>
18       <param-value>classpath:springMVC.xml</param-value>
19     </init-param>
20     <load-on-startup>1</load-on-startup>
21   </servlet>
22   <servlet-mapping>
23     <servlet-name>springDispatcherServlet</servlet-name>
24     <url-pattern>/</url-pattern>
25   </servlet-mapping>
26   <!-- 配置shiro认证过滤器 -->
27   <filter>
28     <filter-name>shiroFilter</filter-name>
29     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
30     <init-param>
31       <param-name>targetFilterLifecycle</param-name>
32       <param-value>true</param-value>
33     </init-param>
34   </filter>
35   <filter-mapping>
36     <filter-name>shiroFilter</filter-name>
37     <url-pattern>/*</url-pattern>
38   </filter-mapping>
39   <!-- 配置编码过滤器(只对post方法) -->
40   <filter>
41     <filter-name>CharacterEncodingFilter</filter-name>
42     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
43     <init-param>
44       <param-name>encoding</param-name>
45       <param-value>UTF-8</param-value>
46     </init-param>
47     <init-param>
48       <param-name>forceRequestEncoding</param-name>
49       <param-value>true</param-value>
50     </init-param>
51     <init-param>
52       <param-name>forceResponseEncoding</param-name>
53       <param-value>true</param-value>
54     </init-param>
55   </filter>
56   <filter-mapping>
57     <filter-name>CharacterEncodingFilter</filter-name>
58     <url-pattern>/*</url-pattern>
59   </filter-mapping>
60 </web-app>

 

posted @ 2018-12-22 17:15  mumu122  阅读(4311)  评论(0编辑  收藏  举报