url-pattern 拦截匹配规则的 url 请求,进入SpringMVC框架处理, SpringMVC就会去找能够处理这个url 的 handler去执行业务逻辑
可使用两种值
(1)使用扩展名方式,语法 *.xx , xx是自定义的扩展名,
常用的方式 *.do, *.action, *.mvc等等,不能使用 *.jsp,如下:
http://localhost:8080/myweb/some.do
http://localhost:8080/myweb/other.do
(2)使用斜杠 "/"
当项目中使用了 "/",它会替代 tomcat中的default 导致所有的静态资源都交给DispatcherServlet处理

(1)index.jsp
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
-
- <head>
- <title>Titletitle>
-
- <script type="text/javascript" src="js/jquery-3.4.1.js">script>
- head>
-
- <body>
- <p>SpringMVC项目p>
-
- <p>url-patten路径p>
- <form action="dosome" method="post">
- 姓名:<input type="text" name="name"> <br/>
- 年龄:<input type="text" name="age"> <br/>
- <input type="submit" value="提交参数">
- form>
-
- <br/>
- <img src="images/image-1.jpeg" alt="我是images/下的静态资源图片,不能显示">
-
- <br/>
- <img src="static/images/image-2.jpeg" alt="我是static/images/下的静态资源图片,不能显示">
-
- body>
- html>
(2)web.xml
- "1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
-
- <servlet>
- <servlet-name>mywebservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
-
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:springmvc.xmlparam-value>
- init-param>
-
- <load-on-startup>1load-on-startup>
- servlet>
-
- <servlet-mapping>
- <servlet-name>mywebservlet-name>
-
-
- <url-pattern>/url-pattern>
- servlet-mapping>
-
- web-app>
(3)处理器MyController
- @Controller
- public class MyController {
- @RequestMapping(value = "/dosome")
- public ModelAndView doSome(HttpServletRequest request , String name, Integer age){
- System.out.println("doSome name="+name+" age="+age);
- ModelAndView mv = new ModelAndView();
- mv.addObject("myname",name);
- mv.addObject("myage",age);
- mv.setViewName("show");
-
- // RequestDispatcher rd =request.getRequestDispatcher("/show.jsp");
- // rd.forward(request,null);
-
- // request.getRequestDispatcher("/show.jsp").forward(request,null);
-
- return mv;
- }
- }
我们在
默认情况下DispatcherServlet没有处理静态资源的能力。没有控制器对象能处理静态资源的访问,因此静态资源(html,js,图片,css)都是404
动态资源some.do是可以访问,因为我们程序中有MyController控制器对象,能处理some.do请求

此时,我们可以打开安装Tomcat的安装路径下的 conf 目录,打开 web.xml文件,看源码
-
-
-
-
-
-
- ......
-
- <servlet>
- <servlet-name>defaultservlet-name>
- <servlet-class>org.apache.catalina.servlets.DefaultServletservlet-class>
- <init-param>
- <param-name>debugparam-name>
- <param-value>0param-value>
- init-param>
- <init-param>
- <param-name>listingsparam-name>
- <param-value>falseparam-value>
- init-param>
- <load-on-startup>1load-on-startup>
- servlet>

DefaultServlet作用:
【1】处理静态资源
【2】处理未映射到其它servlet的请求
对于静态资源的访问,我们有两种处理方式
springmvc.xml
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- https://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc
- https://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
-
- <context:component-scan base-package="com.mycompany.controller" />
-
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
- <property name="prefix" value="/WEB-INF/view/" />
-
- <property name="suffix" value=".jsp" />
- bean>
-
-
- <mvc:default-servlet-handler />
-
-
- <mvc:annotation-driven />
-
- beans>
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- https://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc
- https://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
-
- <context:component-scan base-package="com.mycompany.controller" />
-
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
- <property name="prefix" value="/WEB-INF/view/" />
-
- <property name="suffix" value=".jsp" />
- bean>
-
-
-
-
-
- <mvc:resources mapping="/static/**" location="/static/" />
-
-
- <mvc:annotation-driven />
-
- beans>
不管上述哪种方式,我们同事访问动态资源和静态资源时,会导致冲突

因此需要 在 springmvc.xml 配置文件中加入
- <mvc:annotation-driven />
我们启动Tomcat服务之后,发现在浏览器中输入项目中输入 IP、端口、项目名之后就可以访问页面,这里就存在一个访问路径问题,在 index.jsp 页面中 发现带 斜杠"/" 和不带斜杠 "/" 区别很大
首先先说一下访问路径问题
访问地址分类:
带有协议名称的是绝对地址;如: http://www.baidu.com ;ftp://10.122.6.1
没有协议开头的, 例如 menu/some.do,/menu/some.do
相对地址不能独立使用,必须有一个参考地址;通过参考地址+相对地址本身才能指定资源。
在jsp , html中使用的地址, 都是在前端页面中的地址,都是相对地址
我们有如下 index.jsp 页面
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
-
- <html>
- <head>
- <title>Titletitle>
-
- head>
- <body>
- <p>SpringMVC项目p>
- <p> <a href="menu/some.do">发起menu/some.do请求a> p>
- <br/>
- <p> <a href="/menu/some.do">发起/menu/some.do请求a> p>
- <br/>
- <p> <a href="springmvc-6-path/menu/some.do">发起springmvc-6-path/menu/some.do请求a> p>
- <br/>
- <p> <a href="/springmvc-6-path/menu/some.do">发起/springmvc-6-path/menu/some.do请求a> p>
-
- body>
- html>
处理器方法MyController方法如下
- @Controller
- @RequestMapping("/menu")
- public class MyController {
- @RequestMapping(value = "/some.do")
- public ModelAndView dSome(){
- //处理some.do请求了。 相当于service调用处理完成了。
- ModelAndView mv = new ModelAndView();
- mv.addObject("msg","欢迎使用springmvc做web开发");
- mv.addObject("fun","执行的是doSome方法");
- mv.setViewName("/index.jsp");
- return mv;
- }
- }
带 斜杠"/" 和不带斜杠 "/",带项目名和不带项目名区别如下
1、"menu/some.do" 不带斜杠 "/"
http://localhost:8081/springmvc-6-path/menu/some.do(正确)
访问地址: http://localhost:8081/springmvc-6-path/index.jsp
路径: http://localhost:8081/springmvc-6-path/;资源: index.jsp
在index.jsp发起 menu/some.do请求
访问地址变为http://localhost:8081/springmvc-6-path/menu/some.do
当地址中 不带斜杠 "/"开头,例如 menu/some.do , 当点击链接时, 访问地址是当前页面的地址 + 链接的地址
http://localhost:8081/springmvc-6-path/ + menu/some.do
2、"/menu/some.do" 带斜杠 "/"
HTTP状态 404 - 未找到
http://localhost:8081/menu/some.do
访问的是: http://localhost:8081/springmvc-6-path/menu/index.jsp
路径: http://localhost:8081/springmvc-6-path/;资源: index.jsp
点击 /menu/some.do,访问地址变为 http://localhost:8081/menu/some.do
参考地址是 服务器地址, 也就是 http://localhost:8081
3、"springmvc-6-path/menu/some.do" 带项目名,不带斜杠 "/"
HTTP状态 404 - 未找到
http://localhost:8081/springmvc-6-path/springmvc-6-path/menu/some.do
4、"/springmvc-6-path/menu/some.do" 带项目名,带斜杠 "/"
http://localhost:8081/springmvc-6-path/menu/some.do(正确)
5、"${pageContext.request.contextPath}/menu/some.do"
在 jsp 视图上显示 "/springmvc-6-path/menu/some.do"
http://localhost:8081/springmvc-6-path/menu/some.do(正确)
针对上述访问路径问题,有两种解决方案
1、加入${pageContext.request.contextPath}
- <%-- 如果资源不能访问: 加入${pageContext.request.contextPath}--%>
- <a href="${pageContext.request.contextPath}/menu/some.do">发起${pageContext.request.contextPath}/menu/some.do请求a>
2、加入一个base标签(html语言中的标签); 表示当前页面中访问地址的基地址
页面中所有 没有 斜杠 “/” 开头的地址,都是以base标签中的地址为参考地址
使用base中的地址 + menu/some.do 组成访问地址
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
- <%--base标签, 是html语言中的标签;表示当前页面中访问地址的基地址--%>
- <%--页面中所有没有 "/" 开头的地址,都是以base标签中的地址为参考地址--%>
- <%--使用base中的地址 + menu/some.do 组成访问地址--%>
- <%
- String basePath = request.getScheme() + "://" +
- request.getServerName() + ":" +
- request.getServerPort() +
- request.getContextPath() + "/";
- %>
-
- <html>
- <head>
- <title>Titletitle>
-
- <base href="<%=basePath%>" />
-
- head>
这时再看index.jsp页面
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
- <%--base标签, 是html语言中的标签;表示当前页面中访问地址的基地址--%>
- <%--页面中所有没有 "/" 开头的地址,都是以base标签中的地址为参考地址--%>
- <%--使用base中的地址 + menu/some.do 组成访问地址--%>
- <%
- String basePath = request.getScheme() + "://" +
- request.getServerName() + ":" +
- request.getServerPort() +
- request.getContextPath() + "/";
- %>
-
- <html>
- <head>
- <title>Titletitle>
-
- <base href="<%=basePath%>" />
-
- head>
- <body>
- <p>SpringMVC项目p>
- <p> <a href="menu/some.do">发起menu/some.do请求a> p>
- <br/>
- <p> <a href="/menu/some.do">发起/menu/some.do请求a> p>
- <br/>
- <p> <a href="springmvc-6-path/menu/some.do">发起springmvc-6-path/menu/some.do请求a> p>
- <br/>
- <p> <a href="/springmvc-6-path/menu/some.do">发起/springmvc-6-path/menu/some.do请求a> p>
- <br/>
- <%-- 如果资源不能访问: 加入${pageContext.request.contextPath}--%>
- <a href="${pageContext.request.contextPath}/menu/some.do">发起${pageContext.request.contextPath}/menu/some.do请求a>
-
- body>
- html>