在SpringMVC中转发和重定向的方式分别为
请求转发:请求可以有多次,参数可以进行多次传递
例如:return "forward:test.action";
重定向:参数不可多次传递
例如:return "redirect:test.action";
在SpringMVC中的返回类型有
1.String
返回为字符串,实际上跳转到一个地址
2.void
当返回类型为void的时候,可以采用Servlet的API与Servlet的操作一样
3.ModelAndView
返回的是数据以及视图
视图返回的方式为ModelAndView mv=new ModelAndView(); 例如mv.setViewName("result");
数据的返回方式为 例如:mv.addObject("data", "201807");
在SpringMVC中的数据回显
1.request
例如:request.setAttribute("request", "this is request back");
2.session
例如:session.setAttribute("session", "this is session back");
3.ServletContext
例如:request.getServletContext().setAttribute("ServletContext", "this is ServletContext back");
4.ModelAndView
例如:mv.setViewName("result");
以下为完整的一个测试,其流程为运行login.jsp,然后跳转到LoginController找到对应的地址,执行相应的方法,最后跳转到结果页面
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login</title>
</head>
<body>
<!--此页面用来测试数据回显,以及转发和重定向 -->
<form action="login6.action" method="post">
姓名 :<input type="text" name="name"/>
<button type="submit">提交</button>
</form>
</body>
</html>
LoginController.java
package controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LoginController {
/**
* login1-test为测试返回的是请求转发还是重定向
* 如果是请求转发,那么test中输出的值不为空,如果是重定向,那么test中输出的值为空
* @param request
* @return
*/
@RequestMapping("/login1.action")
public String login1(HttpServletRequest request) {
String name=request.getParameter("name");
//代表下一个跳转:重定向
return "redirect:test.action";
}
@RequestMapping("/login2.action")
public String login2(HttpServletRequest request) {
String name=request.getParameter("name");
//代表下一个跳转:请求转发
return "forward:test.action";
}
@RequestMapping("/test.action")
public String test(HttpServletRequest request) {
String name=request.getParameter("name");
System.out.println("输出从请求中获取的值: "+name);
return "result";
}
/**
* login3-login4为测试返回为void的时候,操作与servlet一样
* @throws IOException
* @throws ServletException
*/
@RequestMapping("/login3.action")
public void login3(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//根据请求对象和响应对象原生的返回响应
request.getRequestDispatcher("result.jsp").forward(request, response);
}
@RequestMapping("/login4.action")
public void login4(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//根据请求对象和响应对象原生的返回响应
response.sendRedirect("result.jsp");
}
/**
* login5是为了测试ModelAndView这个类型,既可以返回视图,也能返回参数
* @return
*/
@RequestMapping("/login5.action")
public ModelAndView login5(){
//返回视图
ModelAndView mv=new ModelAndView();
mv.setViewName("result"); //实际上会返回到result.jsp去
//传递参数
mv.addObject("data", "201807"); //类似于request.setAttribute();
return mv;
}
/**
* 以下为数据回显的测试,针对request,response,ServletContext,ModelAndView的数据传递显示
*/
@RequestMapping("/login6.action")
public ModelAndView login6(HttpServletRequest request,HttpSession session) {
ModelAndView mv=new ModelAndView();
mv.addObject("mv", "this is mv back");
request.setAttribute("request", "this is request back");
session.setAttribute("session", "this is session back");
request.getServletContext().setAttribute("ServletContext", "this is ServletContext back");
mv.setViewName("result");
return mv;
}
}
result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>result</title>
</head>
<body>
<h1>结果页面</h1>
${data }
${mv }
${request }
${session }
${ServletContext }
</body>
</html>