Spring MVC 系列:域对象共享数据
目录
一、使用ServletAPI向request域对象共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
}
二、使用ModelAndView向request域对象共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
<a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("testScope","hello,ModelAndView");
modelAndView.setViewName("success");
return modelAndView;
}
}
三、使用Model向request域对象共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
<a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
<a th:href="@{/testModel}">测试Model--->/testModel</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("testScope","hello,ModelAndView");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
}
四、使用Map向request域对象共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
<a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
<a th:href="@{/testModel}">测试Model--->/testModel</a>
<a th:href="@{/testMap}">测试Map--->/testMap</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("testScope","hello,ModelAndView");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map){
map.put("testScope", "hello,Map");
return "success";
}
}
五、使用ModelMap向request域对象共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
<a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
<a th:href="@{/testModel}">测试Model--->/testModel</a>
<a th:href="@{/testMap}">测试Map--->/testMap</a>
<a th:href="@{/testModelMap}">测试ModelMap--->/testModelMap</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("testScope","hello,ModelAndView");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map){
map.put("testScope", "hello,Map");
return "success";
}
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.put("testScope", "hello,ModelMap");
return "success";
}
}
六、Model、ModelMap、Map的关系
Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的
public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}
七、向session域共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
<a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
<a th:href="@{/testModel}">测试Model--->/testModel</a>
<a th:href="@{/testMap}">测试Map--->/testMap</a>
<a th:href="@{/testModelMap}">测试ModelMap--->/testModelMap</a>
<a th:href="@{/testSession}">测试Session--->/testSession</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("testScope","hello,ModelAndView");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map){
map.put("testScope", "hello,Map");
return "success";
}
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.put("testScope", "hello,ModelMap");
return "success";
}
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "hello,Session");
return "success";
}
}
八、向application域共享数据
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index</h1>
<a th:href="@{/testServletAPI}">测试ServletAPI--->/testServletAPI</a>
<a th:href="@{/testModelAndView}">测试ModelAndView--->/testModelAndView</a>
<a th:href="@{/testModel}">测试Model--->/testModel</a>
<a th:href="@{/testMap}">测试Map--->/testMap</a>
<a th:href="@{/testModelMap}">测试ModelMap--->/testModelMap</a>
<a th:href="@{/testSession}">测试Session--->/testSession</a>
<a th:href="@{/testApplication}">测试Application--->/testApplication</a>
</body>
</html>
java
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* ClassName: ScopeController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/5 - 11:06 AM
* @Version: v1.0
*/
@Controller
public class ScopeController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope","hello,servletAPI");
return "success";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("testScope","hello,ModelAndView");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map){
map.put("testScope", "hello,Map");
return "success";
}
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.put("testScope", "hello,ModelMap");
return "success";
}
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "hello,Session");
return "success";
}
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext servletContext = session.getServletContext();
servletContext.setAttribute("testApplicationScope","hello,application");
return "success";
}
}
微信:17873041739
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?