SpringMVC域对象共享数据
使用servletAPI向request域对象共享数据
controller如下:
package com.lalala.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
//控制器类
@Controller
public class HelloController {
@RequestMapping("/")
public String index(){
return "index";
}
@RequestMapping("/testRequestByServletAPI")
public String testRequestByServletAPI(HttpServletRequest request){
request.setAttribute("testRequestScope", "hello, servletAPI");
return "success";
}
}
success.html如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
success<br>
<p th:text="${testRequestScope}"></p>
</body>
</html>
使用ModelAndView向request域对象共享数据
controller如下:
package com.lalala.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
//控制器类
@Controller
public class HelloController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
ModelAndView modelAndView = new ModelAndView();
//处理模型数据,即向请求域request共享数据
modelAndView.addObject("testRequestScope", "hello,ModelAndView");
//设置视图名称
modelAndView.setViewName("success");
//必须返回modelAndView
return modelAndView;
}
}
success.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
success<br>
<p th:text="${testRequestScope}"></p>
</body>
</html>
使用Model向request域对象共享数据
controller如下:
package com.lalala.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletRequest;
//控制器类
@Controller
public class HelloController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testRequestScope", "hello model");
return "success";
}
}
使用map向request域对象共享数据
controller如下:
package com.lalala.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
//控制器类
@Controller
public class HelloController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("testRequestScope", "hello,map");
return "success";
}
}
使用ModelMap向request域对象共享数据
controller如下:
package com.lalala.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import java.util.Map;
//控制器类
@Controller
public class HelloController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testRequestScope", "hello,ModelMap");
return "success";
}
}
向session域共享数据
controller如下:
package com.lalala.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
//控制器类
@Controller
public class HelloController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testSession")
public String testSession(HttpSession session) {
session.setAttribute("testSessionScope", "hello, session");
return "success";
}
}
success.html如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
success<br>
<p th:text="${session.testSessionScope}"></p>
</body>
</html>
向application域共享数据
controller如下:
package com.lalala.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
//控制器类
@Controller
public class HelloController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testApplication")
public String testApplication(HttpSession session) {
ServletContext servletContext = session.getServletContext();
servletContext.setAttribute("testApplicationScope", "hello,application");
return "success";
}
}
success.html如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
success<br>
<p th:text="${application.testApplicationScope}"></p>
</body>
</html>