springmvc-210805-01---使用斜杠时路径问题

本文介绍了SpringMVC中相对和绝对路径的使用区别,以及在处理请求时遇到的路径问题。当表单提交时不使用/,路径会基于当前URL拼接;使用/则会从服务器根目录开始,可能导致访问不到路径。解决方案包括使用`${pageContext.request.contextPath}

springmvc-210805-01—使用斜杠时路径问题


相对 / 绝对地址

举例:http://localhost:8080/springmvc_21080501/doSome01.do

绝对地址:http://localhost:8080/springmvc_21080501(带有协议名称的)
相对地址:doSome01.do

请求页面不使用 " / "

在页面中不使用 " / " 
提交表单<form action="user/doSome01.do" method="post">
访问的地址是  ----->  http://localhost:8080/springmvc_21080501/user/doSome01.do

================================================================================================

不使用 " / " 情况下,index.jsp 发起请求,返回index.jsp,下面案例doSome03中,
如果不做任何处理,点击两下按钮,会出现404错误,导致地址路径变成:
	http://localhost:8080/springmvc_21080501/user/user/doSome03.do
	
	解决方案01:
		前面加上${pageContext.request.contextPath}
		
	解决方案02:
		加入html语言中的<base>标签,
		表示页面中没有  “ / ” 开头的地址,都是以base标签中的地址为参考地址,
		也就是  base标签中的地址 + user/doSome03.do  来访问。

请求页面使用 " / "

在页面中使用 " / " 
提交表单<form action="/user/doSome02.do" method="post">
访问的地址是  ----->  http://localhost:8080/user/doSome02.do

    这种方式的参考地址变成了 ------>  http://localhost:8080(这是服务器地址)
    缺少了项目名,所以导致访问不到路径。
    解决方案:
    	加上${pageContext.request.contextPath}
    	---->  <form action="${pageContext.request.contextPath}/user/doSome02.do">
    	
当项目中使用斜杠 “ / ” ,它会替代tomcat中的default。
导致所有的静态资源都交给DispatcherServlet处理,
 默认情况下,DispatcherServlet没有处理静态资源的能力。所以导致访问静态资源(html,js,图片,css等)都是404。

案例演示

index.jsp(只看这个两个就可)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>springmvc</title>
    <script type="text/javascript" src="js/jquery-3.4.1.js"></script>
    <base href="http://localhost:8080/springmvc_21080501/"/>
</head>
<body>
    <form action="user/doSome01.do" method="post">
        <span>测试路径不带斜杠</span>
        <br/>
        <span>姓名:</span><input type="text" name="name"/>
        <br/>
        <span>年龄:</span><input type="text" name="age"/>
        <br/>
        <input type="submit" value="提交"/>
    </form>

    <hr/>

    <form action="${pageContext.request.contextPath}/user/doSome02.do">
        <span>测试路径带斜杠</span>
        <br/>
        <span>姓名:</span><input type="text" name="name"/>
        <br/>
        <span>年龄:</span><input type="text" name="age"/>
        <br/>
        <input type="submit" value="提交"/>
    </form>

    <hr/>

    <!--
        如果访问两次这个路径,也就是点击两次doSome03的按钮,路径就会变成:↓↓↓↓↓
                http://localhost:8080/springmvc_21080501/user/user/doSome03.do
        导致地址找不到了,出现404错误。
    -->
    <form action="user/doSome03.do" method="post">
        <span>测试路径不带斜杠,返回index.jsp页面</span>
        <br/>
        <span>姓名:</span><input type="text" name="name"/>
        <br/>
        <span>年龄:</span><input type="text" name="age"/>
        <br/>
        <input type="submit" value="返回index.jsp页面"/>
    </form>
<%--    <a href="user/doSome03.do">返回index.jsp页面</a>--%>
</body>
</html>

MyController.java(只看这个两个就可)

package com.bgy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
//@RequestMapping("/test")
public class MyController {

    @RequestMapping(value = "/user/doSome01.do")
    public ModelAndView doSome01(String name , Integer age){

        ModelAndView mv = new ModelAndView();
        mv.addObject("u_name",name);
        mv.addObject("u_age",age);
        mv.addObject("message","测试路径不带斜杠");

        mv.setViewName("/WEB-INF/view/show01.jsp");

        System.out.println("doReturnStringView---->"+name+" : "+age);

        return mv;
    }

    @RequestMapping(value = "/user/doSome02.do")
    public ModelAndView doSome02(String name , Integer age){

        ModelAndView mv = new ModelAndView();
        mv.addObject("u_name",name);
        mv.addObject("u_age",age);
        mv.addObject("message","测试路径带斜杠");

        mv.setViewName("/WEB-INF/view/show02.jsp");

        System.out.println("doReturnStringView---->"+name+" : "+age);

        return mv;
    }


    @RequestMapping(value = "/user/doSome03.do")
    public ModelAndView doSome03(String name , Integer age){

        ModelAndView mv = new ModelAndView();
        mv.addObject("u_name",name);
        mv.addObject("u_age",age);
        mv.addObject("message","测试路径带斜杠,返回index页面");

        /**
         * 如果访问两次就会资源找不到,因为它是拼接的
         */
        mv.setViewName("/index.jsp");

        System.out.println("doReturnStringView---->"+name+" : "+age);

        return mv;
    }
}

show01.jsp / show02.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>show</title>
</head>
<body>
    <span>message:${message}</span>
    <br/>
    <span>姓名:${u_name}</span>
    <br/>
    <span>年龄:${u_age}</span>
</body>
</html>

web.xml

<?xml version="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"
         metadata-complete="true">

  <!-- 配置过滤器,解决post请求中文乱码问题 -->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <!-- 设置项目中使用的字符编码 -->
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>

    <!-- 强制请求对象(HttpServletResponse)使用encoding编码的值 -->
    <init-param>
      <param-name>forceResponseEncoding</param-name>
      <param-value>true</param-value>
    </init-param>

    <!-- 强制应答对象(HttpServletRequest)使用encoding编码的值 -->
    <init-param>
      <param-name>forceRequestEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <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>

    <load-on-startup>1</load-on-startup>
  </servlet>


  <servlet-mapping>
    <!--
        当项目中使用斜杠 “ / ” ,它会替代tomcat中的default。
        导致所有的静态资源都交给DispatcherServlet处理,
        默认情况下,DispatcherServlet没有处理静态资源的能力。所以导致访问静态资源(html,js,图片,css等)都是404。
    -->
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

springmvc.xml

<?xml version="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
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 开启组件扫描 -->
    <context:component-scan base-package="com.bgy.controller"></context:component-scan>

    <!-- 声明springmvc框架中的视图解析器,帮助开发人员设置视图文件的路径 -->
<!--    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!--        &lt;!&ndash; 前缀 &ndash;&gt;-->
<!--        <property name="prefix" value="/WEB-INF/view/"></property>-->
<!--        &lt;!&ndash; 后缀 &ndash;&gt;-->
<!--        <property name="suffix" value=".jsp"></property>-->
<!--    </bean>-->

    <!-- 注册驱动 -->
    <mvc:annotation-driven/>

    <!--
        第一种解决静态资源处理方式:
            需要在springmvc配置文件加入<mvc:default-servlet-handler>
            原理:
                加入这个标签后,框架会创建控制器对象DefaultServletRequestHandler(类似我们自己创建的MyController),
                DefaultServletRequestHandler这个对象再把请求转发给tomcat的default这个servlet

        这种方式的好处就是方便,不过它归根还是依赖于tomcat等服务的default的servlet。有局限性。
    -->
    <mvc:default-servlet-handler/>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值