Article Directory
Forwarding and Redirection
- Forwarding server behavior, redirect the client behavior.
- Forwarding HTTP request once, the address bar does not change, requested data is not lost domain
redirects HTTP requests at least twice, the address bar changes to display the new address, requesting domain information is lost.
For example:
Forward: Xiao Ming to the class teacher called to ask what time school, but the teacher did not know, the phone will be forwarded to the next headmaster allowed to answer. (Xiaoming side shows teacher's phone, but he and the exchange of data is headmaster, before the information did not hang up)
redirection: Xiao Ming to the class teacher called to ask what time school, but the teacher did not know, to tell him headmaster know, Xiao Ming to hang up the phone to dial the headmaster. (Hang up before the request, a new request again)
The first page request is forwarded ------
- Create a new project, add configuration jar package, write the configuration file, springmvc registered in the web.xml file, create a package structure
在这里插入代码片
- New to write a SpringmvcAction.java, a main.jsp
- In the preparation of the first page jump index.jsp way
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<!--第一种跳转方式:请求转发页面-->
<a href="${pageContext.request.contextPath}/admin/one.action">请求转发</a><br>
</body>
</html>
- Write controller in SpringmvcAction.java
package com.oracle.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller//表明此类是一个控制器
@RequestMapping("/admin")
public class SpringmvcAction {
@RequestMapping("/one")
public String one(){
//请求转发页面(默认转发方式)
System.out.println("这是请求转发页面");
return "main";
}
}
- operation result
- For simplicity tomcat in the project name removed
The second request is forwarded action -----
- Write new way to jump in index.jsp, write new controller in SpringmvcAction.java
<!--第二种跳转方式:请求转发action页面-->
<a href="${pageContext.request.contextPath}/admin/two.action">请求转发action</a><br>
@RequestMapping("/two")
public String two(){
//请求转发action
System.out.println("这是请求转发action");
return "forward:/fore/other.action";
}
- Creating a write controller SpringMvcOther.java
package com.oracle.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("fore")
public class SpringMvcOther {
@RequestMapping("other")
public String other(){
System.out.println("经过了other.action");
return "main";
}
}
- operation result
The third ------ redirects
- Write new way to jump in index.jsp, write new controller in SpringmvcAction.java
<!--第三种跳转方式:重定向页面-->
<a href="${pageContext.request.contextPath}/admin/three.action">重定向页面</a><br>
@RequestMapping("/three")
public String three(){
//重定向页面
System.out.println("这是重定向面");
return "redirect:/main.jsp";
}
- operation result
The fourth ------ redirect action
1. Write a new jump in index.jsp way, the preparation of the new controller and SpringMvcAction.java in SpringmvcAction.java
<!--第四种跳转方式:重定向action-->
<a href="${pageContext.request.contextPath}/admin/four.action">重定向action(+转发)</a><br>
<a href="${pageContext.request.contextPath}/admin/five.action">重定向action显示main.jsp</a><br>
SpringmvcAction.java
@RequestMapping("/four")
public String four(){
//重定向页面
System.out.println("这是重定向action");
return "redirect:/fore/other.action";
}
@RequestMapping("/five")
public String five(){
//重定向页面
System.out.println("这是重定向action");
return "redirect:/fore/others.action";
}
SpringMvcAction.java
@RequestMapping("others")
public String others(){
System.out.println("经过了others.action");
return "redirect:/main.jsp";
}
- Operating results
redirect action + forwarding
redirect action