Spring MVC 中,控制器在接收到 DispatcherServlet 分发过来的请求后,会继续调用 Model 层对请求进行处理。Model 层处理完请求后的结果被称为模型数据,会将模型数据返回给 Controller。
Controller 在接收到 Model 层返回的模型数据后,下一步就是将模型数据通过域对象共享的方式传递给 View 视图进行渲染,最终返回给客户端展示。
域对象是服务器在内存上创建的一块存储空间,主要用不同动态资源之间的数据传递和数据共享。在 Spring MVC 中,常用的域对象有 request 域对象、session 域对象、application 域对象等。
Spring MVC 提供了多种域对象共享数据的方式,其中最常用的方式如下:
- 使用 Servlet API 向 request 域对象中共享数据
- 使用 ModelAndView 向 request 域对象中共享数据
- 使用 Model 向 request 域对象中共享数据
- 使用 Map 向 request 域对象中共享数据
- 使用 ModelMap 向 request 域对象中共享数据
- 使用 Servlet API 向 session 域中共享数据
- 使用 Servlet API 向 application 域中共享数据
下面我们就一一对这几种域对象共享数据的方式进行介绍。
之前笔记:https://www.cnblogs.com/xiaobaibailongma/p/17035102.html
package org.example.controller.requestparam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@RestController
public class ShareValueController {
// http://localhost:8080/shareValue001
/**
* 域共享数据——request只在一次请求中有效,不能跨请求,转发过程也是一次请求,也是有效的
* @param request
* @return
*/
@RequestMapping("/shareValue001")
public String test1(HttpServletRequest request) {
request.setAttribute("a","b");
Object ac = request.getAttribute("a");
System.out.println(ac);
request.removeAttribute("a");
Object acb = request.getAttribute("a");
System.out.println(acb);
return "cheng_gong";
}
@RequestMapping(value = "/shareValue002")
public void test2(HttpServletRequest request, HttpServletResponse response)
{
HttpSession session = request.getSession();
session.setAttribute("msg","session-hello");
}
@RequestMapping(value = "/shareValue003")
public void test3(HttpServletRequest request,HttpServletResponse response)
{
HttpSession session = request.getSession();
Object as = session.getAttribute("msg");
System.out.println(as);
}
}
之前笔记:https://www.cnblogs.com/xiaobaibailongma/p/17048145.html
package org.example.controller.requestparam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@RestController
public class ShareValueController {
// http://localhost:8080/shareValue001
/**
* 域共享数据——request只在一次请求中有效,不能跨请求,转发过程也是一次请求,也是有效的
* @param request
* @return
*/
@RequestMapping("/shareValue001")
public String test1(HttpServletRequest request) {
request.setAttribute("a","b");
Object ac = request.getAttribute("a");
System.out.println(ac);
request.removeAttribute("a");
Object acb = request.getAttribute("a");
System.out.println(acb);
return "cheng_gong";
}
@RequestMapping(value = "/shareValue002")
public void test2(HttpServletRequest request, HttpServletResponse response)
{
HttpSession session = request.getSession();
session.setAttribute("msg","session-hello");
}
@RequestMapping(value = "/shareValue003")
public void test3(HttpServletRequest request,HttpServletResponse response)
{
HttpSession session = request.getSession();
Object as = session.getAttribute("msg");
System.out.println(as);
}
}
笔记:https://www.cnblogs.com/xiaobaibailongma/p/17045120.html
package org.example.controller.requestparam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@RestController
public class ShareValueController {
// http://localhost:8080/shareValue001
/**
* 域共享数据——request只在一次请求中有效,不能跨请求,转发过程也是一次请求,也是有效的
* @param request
* @return
*/
@RequestMapping("/shareValue001")
public String test1(HttpServletRequest request) {
request.setAttribute("a","b");
Object ac = request.getAttribute("a");
System.out.println(ac);
request.removeAttribute("a");
Object acb = request.getAttribute("a");
System.out.println(acb);
return "cheng_gong";
}
@RequestMapping(value = "/shareValue002")
public void test2(HttpServletRequest request, HttpServletResponse response)
{
HttpSession session = request.getSession();
session.setAttribute("msg","session-hello");
}
@RequestMapping(value = "/shareValue003")
public void test3(HttpServletRequest request,HttpServletResponse response)
{
HttpSession session = request.getSession();
Object as = session.getAttribute("msg");
System.out.println(as);
}
@RequestMapping(value = "/shareValue004")
public void test4(HttpServletRequest request,HttpServletResponse response)
{
HttpSession session = request.getSession();
ServletContext application = session.getServletContext();
application.setAttribute("testApplicationScope", "hello,application");
}
@RequestMapping(value = "/shareValue005")
public void test5(HttpServletRequest request,HttpServletResponse response)
{
HttpSession session = request.getSession();
ServletContext application = session.getServletContext();
Object ad = application.getAttribute("testApplicationScope");
System.out.println(ad);
}
}