首页
<a th:href="@{/testServletAPI}">测testServletAPIa><br>
success.html
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p>success!p>
<p th:text="${testScope}">标签体原始值p>
body>
html>
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope", "hello,servletAPI");
return "success";
}
<a th:href="@{/testModelAndView}">测testServletAPIa><br>
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
/**
* ModelAndView有Model和View的功能
* Model主要用于向请求域共享数据
* View主要用于设置视图,实现页面跳转
*/
ModelAndView mav = new ModelAndView();
//向请求域共享数据
mav.addObject("testScope", "hello,ModelAndView");
//设置视图,实现页面跳转
mav.setViewName("success");
return mav;
}
succss.html不变
<a th:href="@{/testModel}">测/testModela><br>
succss.html 不变
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
succss.html 不变
<a th:href="@{/testMap}">测/testMapa><br>
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("testScope", "hello,Map");
return "success";
}
succss.html 不变
<a th:href="@{/testModelMap}">测/testModelMapa><br>
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "hello,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 {}
succss.html
注意session 里面的数据要加session.
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p>success!p>
<p th:text="${session.testScope}">标签体原始值p>
body>
html>
<a th:href="@{/testSession}">测/testSessiona><br>
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testScope", "hello,session");
return "success";
}
<a th:href="@{/testApplication}">测/testApplicationa><br>
success.html
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p>success!p>
<p th:text="${application.testScope}">标签体原始值p>
body>
html>
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("testApplicationScope", "hello,application");
return "success";
}