springMVC域对象共享数据
1、使用ServletAPI向request域对象共享数据
//1、使用ServletAPI向request域对象共享数据
@RequestMapping("/textBySerlvetApi")
public String textByServletApi(HttpServletRequest request){
request.setAttribute("text","hello servletAPI");
return "succes";
2、使用ModelAndView向request域对象共享数据
//2、使用ModelAndView向request域对象共享数据
@RequestMapping("/testModelAndView")
public ModelAndView testModeiAndView(){
/**
* ModelAndView有Model和View的功能
* Model主要用于向请求域共享数据
* View主要用于设置视图,实现页面跳转
*/
ModelAndView mav=new ModelAndView();
//向请求域共享数据
mav.addObject("text","hello modelandview");
//设置视图,实现页面跳转
mav.setViewName("succes");
return mav;
}
3、使用Model向request域对象共享数据
//2、使用Model向request域对象共享数据
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("text","hello model");
return "succes";
}
4、使用map向request域对象共享数据
//4、使用Map向request域对象共享数据
@RequestMapping("/testMap")
public String testMap(Map<String,Object> map){
map.put("text","hello map");
return "succes";
}
5、使用ModelMap向request域对象共享数据
//5、使用ModelMap向request域对象共享数据
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("text","hello modelmap");
return "succes";
}
6、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 {}
7、向session域共享数据
//6 使用servletAPI向session域共享数据
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("text","hello session");
return "succes";
}
8、向application域共享数据
//7 使用servletAPI向application域共享数据
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application= session.getServletContext();
application.setAttribute("test","hello application");
return "succes";
}
html5
index.html5
<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>succes</h1>
<a th:href="@{/textBySerlvetApi}">使用ServletAPI向request域对象共享数据</a><br>
<a th:href="@{/testModelAndView}">使用ModelAndView向request域对象共享数据</a><br>
<a th:href="@{/testModel}">使用Model向request域对象共享数据</a><br>
<a th:href="@{/testMap}">使用Map向request域对象共享数据</a><br>
<a th:href="@{/testModelMap}">使用ModelMap向request域对象共享数据</a><br>
<a th:href="@{/testSession}">使用servletAPI向session域对象共享数据</a><br>
<a th:href="@{/testApplication}">使用servletAPI向application域对象共享数据</a><br>
</body>
</html>
succes.html5
<!DOCTYPE html>
<html lang="en">
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--12345-->
<p th:text="${text}"></p>
<!--6-->
<p th:text="${session.text}"></p>
<!--7-->
<p th:text="${application.test}"></p>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY