• SpringMVC:绝对地址、相对地址(动力)


     

    web.xml:

    1. <?xml version="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. <!-- 声明,注册springmvc的核心对象DispatcherServlet-->
    7. <servlet>
    8. <servlet-name>myweb</servlet-name>
    9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    10. <!-- 自定义springmvc读取的配置文件的位置-->
    11. <init-param>
    12. <!--springmvc的配置文件属性-->
    13. <param-name>contextConfigLocation</param-name>
    14. <!-- 指定自定义文件的位置-->
    15. <param-value>classpath:springmvc.xml</param-value>
    16. </init-param>
    17. <!-- 在tomcat启动后,创建Servlet对象
    18. load-on-startup:表示tomcat启动后创建对象的顺序,它的值是正数,数值越小 tomacat创建对象越早
    19. -->
    20. <load-on-startup>1</load-on-startup>
    21. </servlet>
    22. <!-- 所有的*..do请求交给myweb中央处理器处理-->
    23. <servlet-mapping>
    24. <servlet-name>myweb</servlet-name>
    25. <url-pattern>*.do</url-pattern>
    26. </servlet-mapping>
    27. </web-app>

     

    springmvc.xml:

    1. <?xml version="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. 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">
    6. <!-- 用了注解需要注解扫描器:声明组件扫描器:包扫描-->
    7. <context:component-scan base-package="com.bjpowernode.controller"/>
    8. <!-- 声明springmvc框架中的视图解析器,帮助开发人员设置视图文件的路径-->
    9. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    10. <!-- 前缀:视图文件的路径-->
    11. <property name="prefix" value="/WEB-INF/view/"/>
    12. <!-- 后缀:视图文件的扩展名-->
    13. <property name="suffix" value=".jsp"/>
    14. </bean>
    15. </beans>

     MyController:

    1. package com.bjpowernode.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestMethod;
    5. import org.springframework.web.servlet.ModelAndView;
    6. import javax.servlet.http.HttpServletRequest;
    7. import javax.servlet.http.HttpServletResponse;
    8. import javax.servlet.http.HttpSession;
    9. /*
    10. * @RequestMapping:
    11. * value:所有请求地址的公共部分,叫做模块名称
    12. * 位置:放在类的上面
    13. * 通过加上一个类的访问路径,就可以看出这些功能属于user模块的*/
    14. @Controller
    15. public class MyController {
    16. @RequestMapping(value ="/user/some.do") //value里面可以写多个名字进行访问
    17. public ModelAndView doSome(){ //doSome相当于doGet()---调用Service请求处理
    18. System.out.println("调用Sevice后:");//这里先不调用,简化处理
    19. ModelAndView mv=new ModelAndView();
    20. //添加数据,框架在请求的最后把数据放入到request作用域。
    21. //request.setAttribute("msg","欢迎使用mvc开发")
    22. mv.addObject("mgs","欢迎使用mvc开发");
    23. mv.addObject("fun","执行的是dosome方法");
    24. mv.setViewName("show");
    25. //返回mv
    26. return mv;
    27. }
    28. }

    show.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/6/7
    5. Time: 19:01
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    12. %>
    13. <html>
    14. <head>
    15. <title>Title</title>
    16. <base href="<%=basePath%>">
    17. </head>
    18. <body>
    19. <h3>show.jsp,从request作用域中获取数据</h3>
    20. <h3>msg数据:${mgs}</h3>
    21. <h3>fun数据:${fun}</h3>
    22. </body>
    23. </html>

    index.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/6/7
    5. Time: 17:37
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    12. %>
    13. <html>
    14. <head>
    15. <title>Title</title>
    16. <base href="<%=basePath%>">
    17. </head>
    18. <body>
    19. <h1>hellow word</h1>
    20. <p><a href="user/some.do">发起some.do的get请求</a></p>
    21. </body>
    22. </html>

     

    当访问地址不加 / 时:

    访问的是:

    当发起user/some.do请求后,地址变为:当前页面的地址加上连接的地址

    http://localhost:8888/ch06_path_war_exploded/user/some.do 

     

     

    当访问地址加上 / 时:

    index.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/6/7
    5. Time: 17:37
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    12. %>
    13. <html>
    14. <head>
    15. <title>Title</title>
    16. <base href="<%=basePath%>">
    17. </head>
    18. <body>
    19. <h1>hellow word</h1>
    20. <p><a href="/user/some.do">发起some.do的get请求</a></p>
    21. </body>
    22. </html>

     

     

    地址变为:这个地址缺的是项目名字

    http://localhost:8888/user/some.do

    参考地址是服务器地址:

    可以在访问地址上加上项目名字,就可以正常访问了

    index.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/6/7
    5. Time: 17:37
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    12. %>
    13. <html>
    14. <head>
    15. <title>Title</title>
    16. <base href="<%=basePath%>">
    17. </head>
    18. <body>
    19. <h1>hellow word</h1>
    20. <p><a href="/ch06_path_war_exploded/user/some.do">发起some.do的get请求</a></p>
    21. </body>
    22. </html>

    http://localhost:8888/user/

     

     通过写上项目名字,但是这样不够灵活,这样写死了,如果项目访问地址(项目名字)改变了:导致url访问地址也得改变

     可以通过EL表达式解决:

    代表项目的访问路径:

    ${pageContext.request.contextPath}

    index.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/6/7
    5. Time: 17:37
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    12. %>
    13. <html>
    14. <head>
    15. <title>Title</title>
    16. <base href="<%=basePath%>">
    17. </head>
    18. <body>
    19. <h1>hellow word</h1>
    20. <p><a href="${pageContext.request.contextPath}/user/some.do">发起some.do的get请求</a></p>
    21. </body>
    22. </html>

     base标签

     

     

     改变控制器:MyController:让访问控制器跳转页面还是index.jsp:

    1. package com.bjpowernode.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.RequestMapping;
    4. import org.springframework.web.bind.annotation.RequestMethod;
    5. import org.springframework.web.servlet.ModelAndView;
    6. import javax.servlet.http.HttpServletRequest;
    7. import javax.servlet.http.HttpServletResponse;
    8. import javax.servlet.http.HttpSession;
    9. /*
    10. * @RequestMapping:
    11. * value:所有请求地址的公共部分,叫做模块名称
    12. * 位置:放在类的上面
    13. * 通过加上一个类的访问路径,就可以看出这些功能属于user模块的*/
    14. @Controller
    15. public class MyController {
    16. @RequestMapping(value ="/user/some.do") //value里面可以写多个名字进行访问
    17. public ModelAndView doSome(){ //doSome相当于doGet()---调用Service请求处理
    18. System.out.println("调用Sevice后:");//这里先不调用,简化处理
    19. ModelAndView mv=new ModelAndView();
    20. //添加数据,框架在请求的最后把数据放入到request作用域。
    21. //request.setAttribute("msg","欢迎使用mvc开发")
    22. mv.addObject("mgs","欢迎使用mvc开发");
    23. mv.addObject("fun","执行的是dosome方法");
    24. mv.setViewName("/index.jsp");
    25. //返回mv
    26. return mv;
    27. }
    28. }

    注释视图解析器:不用视图解析器

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀:视图文件的路径-->
        <property name="prefix" value="/WEB-INF/view/"/>
        <!-- 后缀:视图文件的扩展名-->
        <property name="suffix" value=".jsp"/>
    </bean>

     

    index.jsp:假设里面没有,url不加 / 

    <base href="<%=basePath%>">
    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/6/7
    5. Time: 17:37
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    12. %>
    13. <html>
    14. <head>
    15. <title>Title</title>
    16. </head>
    17. <body>
    18. <h1>hellow word</h1>
    19. <p><a href="user/some.do">发起some.do的get请求</a></p>
    20. </body>
    21. </html>

    第一次点击:

     

    第二次点击:

     

     

    index.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/6/7
    5. Time: 17:37
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    12. %>
    13. <html>
    14. <head>
    15. <title>Title</title>
    16. <base href="http://localhost:8888/ch06_path_war_exploded/">
    17. </head>
    18. <body>
    19. <h1>hellow word</h1>
    20. <p><a href="user/some.do">发起some.do的get请求</a></p>
    21. </body>
    22. </html>

     

     

     

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/6/7
    5. Time: 17:37
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    12. %>
    13. <html>
    14. <head>
    15. <title>Title</title>
    16. <base href="<%=basePath%>">
    17. </head>
    18. <body>
    19. <h1>hellow word</h1>
    20. <p><a href="user/some.do">发起some.do的get请求</a></p>
    21. </body>
    22. </html>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    %>
    request.getScheme() 获取协议
    request.getServerName() 获取服务器IP
    request.getServerPort() 获取端口号
    getContextPath() 获取返回路径

    在index页面中,所有没有 / 开头的参考地址都以<%=basePath%>为参考地址

  • 相关阅读:
    HarmonyOS interface router scale pageTransition SlideEffect.Left ArkTS ArkUI
    CentOS安装Docker
    存量时代的面经
    4800包括了路线坐标正反算、竖曲线、超高加宽、边坡放样及断面计算等程序。
    MySQL中的锁机制
    TikTok营销指南 分享TikTok电商商家成功变现的5个技巧
    mongodb 权限配置
    Spring——自动装配
    代码随想录训练营day57
    从零开始学Vue3--井字棋
  • 原文地址:https://blog.csdn.net/dengfengling999/article/details/125424708