• Java--SpringMVC之url-pattern,静态资源;URL相对路径,绝对路径


    url-pattern

    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

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <html>
    3. <head>
    4. <title>Titletitle>
    5. <script type="text/javascript" src="js/jquery-3.4.1.js">script>
    6. head>
    7. <body>
    8. <p>SpringMVC项目p>
    9. <p>url-patten路径p>
    10. <form action="dosome" method="post">
    11. 姓名:<input type="text" name="name"> <br/>
    12. 年龄:<input type="text" name="age"> <br/>
    13. <input type="submit" value="提交参数">
    14. form>
    15. <br/>
    16. <img src="images/image-1.jpeg" alt="我是images/下的静态资源图片,不能显示">
    17. <br/>
    18. <img src="static/images/image-2.jpeg" alt="我是static/images/下的静态资源图片,不能显示">
    19. body>
    20. html>

    (2)web.xml

    1. "1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <servlet>
    7. <servlet-name>mywebservlet-name>
    8. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    9. <init-param>
    10. <param-name>contextConfigLocationparam-name>
    11. <param-value>classpath:springmvc.xmlparam-value>
    12. init-param>
    13. <load-on-startup>1load-on-startup>
    14. servlet>
    15. <servlet-mapping>
    16. <servlet-name>mywebservlet-name>
    17. <url-pattern>/url-pattern>
    18. servlet-mapping>
    19. web-app>

    (3)处理器MyController

    1. @Controller
    2. public class MyController {
    3. @RequestMapping(value = "/dosome")
    4. public ModelAndView doSome(HttpServletRequest request , String name, Integer age){
    5. System.out.println("doSome name="+name+" age="+age);
    6. ModelAndView mv = new ModelAndView();
    7. mv.addObject("myname",name);
    8. mv.addObject("myage",age);
    9. mv.setViewName("show");
    10. // RequestDispatcher rd =request.getRequestDispatcher("/show.jsp");
    11. // rd.forward(request,null);
    12. // request.getRequestDispatcher("/show.jsp").forward(request,null);
    13. return mv;
    14. }
    15. }

    我们在 中设置访问路径 改为 "/",此时访问静态资源(html,js,图片,css)都是404

    默认情况下DispatcherServlet没有处理静态资源的能力。没有控制器对象能处理静态资源的访问,因此静态资源(html,js,图片,css)都是404

    动态资源some.do是可以访问,因为我们程序中有MyController控制器对象,能处理some.do请求

     此时,我们可以打开安装Tomcat的安装路径下的 conf 目录,打开 web.xml文件,看源码

    1. ......
    2. <servlet>
    3. <servlet-name>defaultservlet-name>
    4. <servlet-class>org.apache.catalina.servlets.DefaultServletservlet-class>
    5. <init-param>
    6. <param-name>debugparam-name>
    7. <param-value>0param-value>
    8. init-param>
    9. <init-param>
    10. <param-name>listingsparam-name>
    11. <param-value>falseparam-value>
    12. init-param>
    13. <load-on-startup>1load-on-startup>
    14. servlet>

    DefaultServlet作用:

    【1】处理静态资源

    【2】处理未映射到其它servlet的请求

    对于静态资源的访问,我们有两种处理方式

    (1)加入标签

    springmvc.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/context
    9. https://www.springframework.org/schema/context/spring-context.xsd
    10. http://www.springframework.org/schema/mvc
    11. https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    12. <context:component-scan base-package="com.mycompany.controller" />
    13. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    14. <property name="prefix" value="/WEB-INF/view/" />
    15. <property name="suffix" value=".jsp" />
    16. bean>
    17. <mvc:default-servlet-handler />
    18. <mvc:annotation-driven />
    19. beans>

    (2) 加入标签

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/context
    9. https://www.springframework.org/schema/context/spring-context.xsd
    10. http://www.springframework.org/schema/mvc
    11. https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    12. <context:component-scan base-package="com.mycompany.controller" />
    13. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    14. <property name="prefix" value="/WEB-INF/view/" />
    15. <property name="suffix" value=".jsp" />
    16. bean>
    17. <mvc:resources mapping="/static/**" location="/static/" />
    18. <mvc:annotation-driven />
    19. beans>

    不管上述哪种方式,我们同事访问动态资源和静态资源时,会导致冲突

     因此需要 在 springmvc.xml 配置文件中加入  注解驱动

    1. <mvc:annotation-driven />

    二、路径

    我们启动Tomcat服务之后,发现在浏览器中输入项目中输入 IP、端口、项目名之后就可以访问页面,这里就存在一个访问路径问题,在 index.jsp 页面中 发现带 斜杠"/"  和不带斜杠 "/" 区别很大

    首先先说一下访问路径问题

    访问地址分类:

    1、绝对地址

     带有协议名称的是绝对地址;如: http://www.baidu.com ;ftp://10.122.6.1

    2、相对地址

    没有协议开头的, 例如 menu/some.do,/menu/some.do

    相对地址不能独立使用,必须有一个参考地址;通过参考地址+相对地址本身才能指定资源。

    在jsp , html中使用的地址, 都是在前端页面中的地址,都是相对地址

    我们有如下 index.jsp 页面

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <html>
    3. <head>
    4. <title>Titletitle>
    5. head>
    6. <body>
    7. <p>SpringMVC项目p>
    8. <p> <a href="menu/some.do">发起menu/some.do请求a> p>
    9. <br/>
    10. <p> <a href="/menu/some.do">发起/menu/some.do请求a> p>
    11. <br/>
    12. <p> <a href="springmvc-6-path/menu/some.do">发起springmvc-6-path/menu/some.do请求a> p>
    13. <br/>
    14. <p> <a href="/springmvc-6-path/menu/some.do">发起/springmvc-6-path/menu/some.do请求a> p>
    15. body>
    16. html>

    处理器方法MyController方法如下

    1. @Controller
    2. @RequestMapping("/menu")
    3. public class MyController {
    4. @RequestMapping(value = "/some.do")
    5. public ModelAndView dSome(){
    6. //处理some.do请求了。 相当于service调用处理完成了。
    7. ModelAndView mv = new ModelAndView();
    8. mv.addObject("msg","欢迎使用springmvc做web开发");
    9. mv.addObject("fun","执行的是doSome方法");
    10. mv.setViewName("/index.jsp");
    11. return mv;
    12. }
    13. }

    带 斜杠"/"  和不带斜杠 "/",带项目名和不带项目名区别如下

    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}

    1. <%-- 如果资源不能访问: 加入${pageContext.request.contextPath}--%>
    2. <a href="${pageContext.request.contextPath}/menu/some.do">发起${pageContext.request.contextPath}/menu/some.do请求a>

    2、加入一个base标签(html语言中的标签); 表示当前页面中访问地址的基地址

    页面中所有 没有 斜杠 “/” 开头的地址,都是以base标签中的地址为参考地址

    使用base中的地址 + menu/some.do 组成访问地址

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%--base标签, 是html语言中的标签;表示当前页面中访问地址的基地址--%>
    3. <%--页面中所有没有 "/" 开头的地址,都是以base标签中的地址为参考地址--%>
    4. <%--使用base中的地址 + menu/some.do 组成访问地址--%>
    5. <%
    6. String basePath = request.getScheme() + "://" +
    7. request.getServerName() + ":" +
    8. request.getServerPort() +
    9. request.getContextPath() + "/";
    10. %>
    11. <html>
    12. <head>
    13. <title>Titletitle>
    14. <base href="<%=basePath%>" />
    15. head>

    这时再看index.jsp页面

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%--base标签, 是html语言中的标签;表示当前页面中访问地址的基地址--%>
    3. <%--页面中所有没有 "/" 开头的地址,都是以base标签中的地址为参考地址--%>
    4. <%--使用base中的地址 + menu/some.do 组成访问地址--%>
    5. <%
    6. String basePath = request.getScheme() + "://" +
    7. request.getServerName() + ":" +
    8. request.getServerPort() +
    9. request.getContextPath() + "/";
    10. %>
    11. <html>
    12. <head>
    13. <title>Titletitle>
    14. <base href="<%=basePath%>" />
    15. head>
    16. <body>
    17. <p>SpringMVC项目p>
    18. <p> <a href="menu/some.do">发起menu/some.do请求a> p>
    19. <br/>
    20. <p> <a href="/menu/some.do">发起/menu/some.do请求a> p>
    21. <br/>
    22. <p> <a href="springmvc-6-path/menu/some.do">发起springmvc-6-path/menu/some.do请求a> p>
    23. <br/>
    24. <p> <a href="/springmvc-6-path/menu/some.do">发起/springmvc-6-path/menu/some.do请求a> p>
    25. <br/>
    26. <%-- 如果资源不能访问: 加入${pageContext.request.contextPath}--%>
    27. <a href="${pageContext.request.contextPath}/menu/some.do">发起${pageContext.request.contextPath}/menu/some.do请求a>
    28. body>
    29. html>

  • 相关阅读:
    【AIGC】提示词 Prompt 分享
    关键字volatile用法
    Lua学习笔记:debug.sethook函数
    应急响应-Linux常用应急溯源命令
    分布式概念-理论篇
    Connor学Android - RemoteViews
    架构:微服务网关(SIA-Gateway)简介
    HTML静态网页作业——电影介绍-你的名字 5页 无js 带音乐
    C语言,洛谷题:你的飞碟在这
    Nginx “upstream sent no valid HTTP/1.0 header“错误,以及http status 009分析
  • 原文地址:https://blog.csdn.net/MinggeQingchun/article/details/123683539