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
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">
<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>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
<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>
<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>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
</beans>