首页 > 其他分享 >Spring MVC入门(四):域对象共享数据

Spring MVC入门(四):域对象共享数据

时间:2022-10-01 13:07:30浏览次数:63  
标签:http 入门 success Spring request 获取数据 MVC ModelAndView 页面

  • 使用ServletAPI向request域对象共享数据
# 后端:向request域对象中添加数据,并转发到success页面
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope", "hello,servletAPI");
return "success";
}

# thymeleaf编写的success页面中获取数据
<p th:text="${testScope}"></p>

# 浏览器访问如下
http://localhost:8080/上下文路径/testServletAPI
  • 使用ModelAndView向request域对象共享数据
# 后端:创建ModelAndView对象,并将对象返回给前端控制器,前端控制器解析对象
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
/**
* ModelAndView有Model和View的功能
* Model主要用于向请求域共享数据
* View主要用于设置视图,实现页面跳转
*/
ModelAndView mav = new ModelAndView();
//向请求域共享数据
mav.addObject("testScope", "hello,ModelAndView");
//设置视图,实现页面跳转
mav.setViewName("success");
return mav;
}

# thymeleaf编写的success页面中获取数据
<p th:text="${testScope}"></p>

# 浏览器访问如下
http://localhost:8080/上下文路径/testModelAndView
  • 使用Model向request域对象共享数据
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}

# thymeleaf编写的success页面中获取数据
<p th:text="${testScope}"></p>

# 浏览器访问如下
http://localhost:8080/上下文路径/testModel
  • 使用map向request域对象共享数据
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("testScope", "hello,Map");
return "success";
}

# thymeleaf编写的success页面中获取数据
<p th:text="${testScope}"></p>

# 浏览器访问如下
http://localhost:8080/上下文路径/testMap
  • 使用ModelMap向request域对象共享数据
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "hello,ModelMap");
return "success";
}

# thymeleaf编写的success页面中获取数据
<p th:text="${testScope}"></p>

# 浏览器访问如下
http://localhost:8080/上下文路径/testModelMap
  • ​Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的​
  • 向session域共享数据
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "hello,session");
return "success";
}

# thymeleaf编写的success页面中获取数据
<p th:text="${session.testSessionScope}"></p>

# 浏览器访问如下
http://localhost:8080/上下文路径/testSession
  • 向application域共享数据
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("testApplicationScope", "hello,application");
return "success";
}

# thymeleaf编写的success页面中获取数据
<p th:text="${application.testApplicationScope}"></p>

# 浏览器访问如下
http://localhost:8080/上下文路径/testApplication



标签:http,入门,success,Spring,request,获取数据,MVC,ModelAndView,页面
From: https://blog.51cto.com/chniny/5728224

相关文章

  • Spring MVC基础(一):HelloWorld案例
    构建1个maven项目pom.xml<packaging>war</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifa......
  • Spring MVC入门(一):入门案例
    mvc简介MVC是一种软件架构的思想,将软件按照模型、视图、控制器来划分M:Model,模型层,指工程中的JavaBean,作用是处理数据JavaBean分为两类:一类称为实体类Bean:专门存储业务数......
  • mqtt入门(四):客户端sdk
    前言该笔记学习自​​mqtt​​起步导入依赖<dependency><groupId>org.eclipse.paho</groupId><artifactId>org.eclipse.paho.client.mqttv3</artifactId><version>1.2......
  • mqtt入门(二):emqx安装
    前言该笔记学习自​​mqtt​​​​参考1​​​​参考2​​简介产品介绍rpm安装emqx​​​链接​​docker安装emqxdockerpullemqx/emqx:v4.0.5dockerrun-tid--nameemqx......
  • gradle构建spring boot项目
    当前​​案例地址​​构建gradle项目配置字符集、注解生效激活、JavaCompiler使用IDE自带的gradle测试打包​​参考​​拉取依赖dependencies{testCompilegroup:'ju......
  • spring boot导出word
    案例一​​参考​​使用这种方式时会报错:​​org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException:Novalidentr​​案例二导出excel导出word导出word表格导......
  • springboot导入(导出)excel
    案例一导入sql​​启动项目访问localhost:8080进入首页​​​​可将数据库中数据导出到本地​​​​将导出本地的excel修改后再次上传,查看数据库​​......
  • ES6简单入门
    前言有关ES6详情​​参考​​ES6是ECMAScript6,js的核心是ECMAScript入门var声明的变量作用域为全局ES6新增了let,声明的变量作用域为代码块,也即是作用域为某个函数,因此在同一......
  • Spring03:展现模型数据、处理及校验表单
    1展现模型数据1.1图解过程在上一节“Spring-02:构建并运行基础Spring应用”中,我们运行的基础应用所展现的视图home.html还未包含任何领域类的信息,如下图:想要构建真实......
  • CSS入门学习笔记
    CSS入门学习笔记一、CSS简介1、什么是CSS?2、为什么使用CSS?3、CSS的作用二、CSS语法1、CSS基础语法2、CSS注释语法3、CSS应用方法三、CSS选择器1、元素选择器2、类选择器3、......