详解SpringMVC的url-pattern配置及原理剖析
作者:易水寒的博客 时间:2023-08-11 12:00:27
xml里面配置标签:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
方式一:带后缀,比如*.action *.do *.aaa
该种方式比较精确、方便,在以前和现在企业中都有很大的使用比例
方式二:/ 不会拦截 .jsp,但是会拦截.html等静态资源(静态资源:除了servlet和jsp之外的js、css、png等)
为什么配置为/ 会拦截静态资源???
因为tomcat容器中有一个web.xml(父),你的项目中也有一个web.xml(子),是一个继承关系
父web.xml中有一个DefaultServlet, url-pattern 是一个 /
此时我们自己的web.xml中也配置了一个 / ,覆写了父web.xml的配置
为什么不拦截.jsp呢?
因为父web.xml中有一个JspServlet,这个servlet拦截.jsp文件,而我们并没有覆写这个配置,
所以springmvc此时不拦截jsp,jsp的处理交给了tomcat
如何解决/拦截静态资源这件事?
方式三:/* 拦截所有,包括.jsp
-->
<!--拦截匹配规则的url请求,进入springmvc框架处理-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
父xml的路径:
我们配置的springmvc.xml只能对jsp进行拦截,但不能对html等进行拦截,我们在webapp下新建个test.html
然后访问:
原因是没有对html等静态资源后缀进行处理的handle.
我们在springmvc.xml里面配置:
<mvc:default-servlet-handler/>
访问:
继续测试:
如果我们将html页面放在webapp以外的其他位置,都是无法访问的:
那如果我们想让静态资源在resources下面访问,我们应该怎么配置呢?
静态资源配置方案二
将test.html放在resources目录下面,然后配置springmvc.xml:
<!--静态资源配置,方案二,SpringMVC框架自己处理静态资源
mapping:约定的静态资源的url规则
location:指定的静态资源的存放位置
-->
<mvc:resources location="classpath:/" mapping="/resources/**"/>
resources目录就是咱们的classpath路径。
访问:
http://localhost:8080/resources/test.html
来源:https://www.cnblogs.com/liuyj-top/p/13137736.html
标签:SpringMVC,url-pattern
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android耳机左右声道接反具体修正方法
2021-05-27 21:16:38
Netty分布式pipeline管道创建方法跟踪解析
2023-11-03 02:55:51
![](https://img.aspxhome.com/file/2023/0/58890_0s.png)
OpenCV实现直线拟合
2023-06-22 15:22:37
![](https://img.aspxhome.com/file/2023/4/92494_0s.jpg)
在Linux上运行C#的方法
2023-03-06 00:28:39
winfrom 在业务层实现事务控制的小例子
2021-11-16 14:19:50
RadioButton实现选择后可取消选择
2023-10-15 23:50:39
![](https://img.aspxhome.com/file/2023/3/108243_0s.jpg)
百度地图API提示230 错误app scode码校验失败的解决办法
2022-02-17 11:48:10
![](https://img.aspxhome.com/file/2023/3/109173_0s.png)
Android 仿今日头条评论时键盘自动弹出的效果(推荐)
2022-07-24 15:00:45
![](https://img.aspxhome.com/file/2023/7/139547_0s.png)
代码分析Android消息机制
2023-07-26 09:44:44
Swing常用组件之多行文本区JTextArea
2023-11-08 14:16:49
Java实现的日历功能完整示例
2021-10-12 18:30:21
![](https://img.aspxhome.com/file/2023/4/91124_0s.png)
Springboot打成war包并在tomcat中运行的部署方法
2022-06-29 07:53:32
C#最简单的字符串加密解密方法
2022-10-30 12:46:36
Android Fragment概述及用法
2022-08-23 15:54:22
![](https://img.aspxhome.com/file/2023/5/138255_0s.gif)
Spring Cloud项目前后端分离跨域的操作
2022-05-20 08:11:16
android生命周期深入分析(二)
2021-07-25 16:26:14
![](https://img.aspxhome.com/file/2023/4/128994_0s.png)
MyBatis-Plus工具使用之EntityWrapper解析
2021-10-09 06:13:48
Spring boot使用多线程过程步骤解析
2023-04-03 04:54:50
Android实现页面滑动切换动画
2023-12-26 11:47:54
Android下拉刷新以及GridView使用方法详解
2022-12-26 18:01:24