SpringMVC重定向转发
1. 新建一个子项目
具体可以参考https://blog.csdn.net/weixin_41549354/article/details/108138004
2. 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">
<!-- 1. 注册DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 关联一个SpringMVC的配置文件:【】 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 启动级别-1-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- / 匹配所有的请求:(不包括.jsp)
/* 匹配所有的请求:包括.jsp-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3. springmvc-servlet.xml
<?xml version="1.0" encoding="UTF8"?>
<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
https://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.controller"/>
<!-- 让SpringMVC不处理静态资源 .css .js .html .img ...这些东西走视图解析器会产生问题-->
<mvc:default-servlet-handler/>
<!-- 支持MVC注解驱动
在Spring中一般采用@RequestMapping来完成映射关系,要想使这个注解生效,必须向IOC中注册
DefaultAnnotationHandlerMapping处理器映射器和一个
AnnotationMethodHandlerAdapter处理器适配器实例,这两个实例
分别在类级别和方法级别处理,下面这个注解,自动帮助我们注册这两个实例-->
<mvc:annotation-driven/>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!-- 前缀后缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4. hello.jsp
在WEB-INF目录下新建一个jsp文件夹,然后在文件夹下面新建该jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/8/21
Time: 0:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
5. RedirectController.java
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RedirectController {
@RequestMapping("t1")
public String test1() {
//转发:对应HttpServletRequest对象的转发方法
return "hello";
}
@RequestMapping("t2")
public String test2(Model model) {
//重定向,对应HttpServletResponse对象的重定向方法
return "redirect:/index.jsp";
}
}
6. 测试
一般普通返回基本都属于转发功能,这里只测试重定向功能:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-e6DZCLO5-1597981414044)(D:\我的文档\Pictures\44.PNG)]
7. 关于转发和重定向
转发:服务器行为。客户浏览器发送http请求----》web服务器接受此请求–》调用内部的一个方法在容器内部完成请求处理和转发动作----》将目标资源发送给客户;在这里,转发的路径必须是同一个web容器下的url,其不能转向到其他的web路径上去,中间传递的是自己的容器内的request。在客户浏览器路径栏显示的仍然是其第一次访问的路径,也就是说客户是感觉不到服务器做了转发的。转发行为是浏览器只做了一次访问请求。
重定向:客户端行为。客户浏览器发送http请求----》web服务器接受后发送302状态码响应及对应新的location给客户浏览器–》客户浏览器发现是302响应,则自动再发送一个新的http请求,请求url是新的location地址----》服务器根据此请求寻找资源并发送给客户。在这里location可以重定向到任意URL,既然是浏览器重新发出了请求,则就没有什么request传递的概念了。在客户浏览器路径栏显示的是其重定向的路径,客户可以观察到地址的变化的。重定向行为是浏览器做了至少两次的访问请求的。
举个例子:
假设你去办理某个执照,
重定向:你先去了A局,A局的人说:“这个事情不归我们管,去B局”,然后,你就从A退了出来,自己乘车去了B局。
转发:你先去了A局,A局看了以后,知道这个事情其实应该B局来管,但是他没有把你退回来,而是让你坐一会儿,自己到后面办公室联系了B的人,让他们办好后,送了过来。