SpringMVC向域*享数据
向request域*享数据
- 通过原生Servlet方法
- 通过ModelAndView
- 通过Model
- 通过Map
- 通过ModelMap
index.html
<body>
<h1>This is index.html</h1>
<a th:href="@{/testRequestAPI}">使用RequestAPI向request域中传值</a><br>
<a th:href="@{/modelAndView}">使用modelAndView向request域中传值</a><br>
<a th:href="@{/model}">使用model向request域中传值</a><br>
<a th:href="@{/map}">使用map向request域中传值</a><br>
<a th:href="@{/modelMap}">使用modelMap向request域中传值</a><br>
</body>
success.html
<body>
<h2>success</h2>
<br>
<p th:text="${requestScope}"></p>
</body>
@Controller
public class ScopeController {
@RequestMapping("/testRequestAPI")
public String testRequestAPI(HttpServletRequest request){
request.setAttribute("requestScope","hello,RequestAPI");
return "success";
}
@RequestMapping("/modelAndView")
public ModelAndView testModelAndView(){
ModelAndView mav = new ModelAndView();
mav.setViewName("success");
mav.addObject("requestScope","hello,session");
return mav;
}
@RequestMapping("/model")
public String testModel(Model model){
model.addAttribute("requestScope","hello,model");
return "success";
}
@RequestMapping("/map")
public String testMap(Map<String,Object> map){
map.put("requestScope","hello,map");
return "success";
}
@RequestMapping("/modelMap")
public String testMap(ModelMap modelMap){
modelMap.addAttribute("requestScope","hello,modelMap");
return "success";
}
}
以上五种方式,都可以讲数据传输到request域中
model,map,modelMap的关系
我们在三个方法中分别加入一段输出全路径,结果都为org.springframework.validation.support.BindingAwareModelMap,可以得知三者的参数其实本质上都是 BindingAwareModelMap 类型的
@RequestMapping("/model")
public String testModel(Model model){
model.addAttribute("requestScope","hello,model");
System.out.println(model.getClass().getName());
return "success";
}
@RequestMapping("/map")
public String testMap(Map<String,Object> map){
map.put("requestScope","hello,map");
System.out.println(map.getClass().getName());
return "success";
}
@RequestMapping("/modelMap")
public String testMap(ModelMap modelMap){
modelMap.addAttribute("requestScope","hello,modelMap");
System.out.println(modelMap.getClass().getName());
return "success";
}
我们来看一下BindingAwareModelMap的结构
public interface Model{}
public interface Map{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}
五个方法中,不论用哪种方法实现向域中传输数据,最后都是封装成ModelAndView对象来传递
无论哪种方法,都会经过前端控制器,前端控制器中有一个doDispatch方法,里面都会将这些值封装成一个ModelAndView对象
向Session、Application域中传值
SpringMVC提供了注解方法来设置Session域中的值,但不如原生request方法来的简单,而像application域中传值,只需要调用Session即可
@RequestMapping("/session")
public String testSession(HttpSession session){
session.setAttribute("requestScope","hello,session");
return "success";
}
@RequestMapping("/application")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("requestScope","hello,application");
return "success";
}
<body>
<h2>success</h2>
<br>
<p th:text="${requestScope}"></p>
<!--输出Session域中的requestScope值-->
<p th:text="${session.requestScope}"></p>
<!--输出application域中的requestScope值-->
<p th:text="${application.requestScope}"></p>
</body>