DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>successh1><br/>
servletapi:<p th:text="${name}">p><br/>
body>
html>
@RequestMapping("testRequestByServletAPI")
public String testRequestByServletAPI(HttpServletRequest request) {
request.setAttribute("name", "job");
return "success";
}
不管用的是什么方法,最后都会封装成ModelAndView对象。
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>successh1><br/>
servletapi:<p th:text="${name}">p><br/>
body>
html>
@RequestMapping("testRequestByModelAndView")
public ModelAndView testRequestByModelAndView() {
ModelAndView mav = new ModelAndView();
//向域对象中共享数据
mav.addObject("name", "modelAmdView");
//设置视图名称
mav.setViewName("success");
return mav;
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>successh1><br/>
servletapi:<p th:text="${name}">p><br/>
body>
html>
@RequestMapping("testRequestByModel")
public String testRequestByModel(Model model) {
model.addAttribute("name", "model");
return "success";
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>successh1><br/>
servletapi:<p th:text="${name}">p><br/>
body>
html>
@RequestMapping("testRequestByMap")
public String testRequestByMap(Map<String,Object> map) {
map.put("name","map");
return "success";
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>successh1><br/>
servletapi:<p th:text="${name}">p><br/>
body>
html>
@RequestMapping("testRequestByModelMap")
public String testRequestByModelMap(ModelMap modelMap) {
modelMap.addAttribute("name", "modelMap");
return "success";
}
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 {}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>successh1><br/>
request:<p th:text="${name}">p><br/>
session:<p th:text="${session.name}">p><br/>
body>
html>
@RequestMapping("tesSessionByServletApi")
public String tesSessionByServletApi(HttpSession session){
session.setAttribute("name","session");
return "success";
}
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>successh1><br/>
request:<p th:text="${name}">p><br/>
session:<p th:text="${session.name}">p><br/>
application:<p th:text="${application.name}">p><br/>
body>
html>
@RequestMapping("testApplicationByServletApi")
public String testApplicationByServletApi(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("name","application");
return "success";
}