springMVC Model、 ModelMap 和 ModelAndView的区别
2017-12-05 15:18
609 查看
简单来说:
Model是包含四个addAttribute 和一个 merAttribute方法的接口。
ModelMap :实现了Map接口,包含Map方法。视图层通过request找到ModelMap中的数据。
ModelAndView:是包含ModelMap 和视图对象的容器。正如名字暗示的一样既包含模型也包含视图,而ModelMap只是包含模型的信息。
ModelAndView的例子,台后
[java] view
plain copy
public class CarListController implements Controller {
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
CarManager carManager = new CarManager();
ModelAndView modelAndView = new ModelAndView("carList");
modelAndView.addObject("carList", carManager.getCarList());
return modelAndView;
}
}
ModelAndView的例子,前台view
[html] view
plain copy
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h1>Car List</h1>
<c:forEach items="${carList}" var="car">
${car.brand.name} ${car.model}: ${car.price}
<br />
</c:forEach>
</body>
<html>
ModelMap的例子:
[java] view
plain copy
public String testMethod(String someparam,ModelMap model)
{
//省略方法处理逻辑若干
//将数据放置到ModelMap对象model中,第二个参数可以是任何java类型
model.addAttribute("key",someparam);
...... //返回跳转地址
return "test/test";
}
或者直接使用接口:
[java] view
plain copy
public String toProvinceView(Model model, HttpSession session,) {
model.addAttribute("colModel", colModel);
model.addAttribute("colNames", colNames);
model.addAttribute("buttonName", buttonName);
return "statistic/StatisticChart";
Model是包含四个addAttribute 和一个 merAttribute方法的接口。
ModelMap :实现了Map接口,包含Map方法。视图层通过request找到ModelMap中的数据。
ModelAndView:是包含ModelMap 和视图对象的容器。正如名字暗示的一样既包含模型也包含视图,而ModelMap只是包含模型的信息。
ModelAndView的例子,台后
[java] view
plain copy
public class CarListController implements Controller {
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
CarManager carManager = new CarManager();
ModelAndView modelAndView = new ModelAndView("carList");
modelAndView.addObject("carList", carManager.getCarList());
return modelAndView;
}
}
ModelAndView的例子,前台view
[html] view
plain copy
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h1>Car List</h1>
<c:forEach items="${carList}" var="car">
${car.brand.name} ${car.model}: ${car.price}
<br />
</c:forEach>
</body>
<html>
ModelMap的例子:
[java] view
plain copy
public String testMethod(String someparam,ModelMap model)
{
//省略方法处理逻辑若干
//将数据放置到ModelMap对象model中,第二个参数可以是任何java类型
model.addAttribute("key",someparam);
...... //返回跳转地址
return "test/test";
}
或者直接使用接口:
[java] view
plain copy
public String toProvinceView(Model model, HttpSession session,) {
model.addAttribute("colModel", colModel);
model.addAttribute("colNames", colNames);
model.addAttribute("buttonName", buttonName);
return "statistic/StatisticChart";
相关文章推荐
- springMVC Model ModelMap 和 ModelAndView的区别
- springMVC Model、 ModelMap 和 ModelAndView的区别
- springMVC Model ModelMap 和 ModelAndView的区别
- SpringMVC中model,modelMap和modelAndView的区别
- SpringMVC中返回数据Model,ModelAndView,ModelMap的区别
- SpringMVC 中Model、ModelMap和ModelAndView的区别和用法
- springMVC Model、 ModelMap 和 ModelAndView的区别
- SpringMVC Model、ModelMap和ModelAndView的区别和用法
- springmvc Model ModelMap 和 ModelAndView的区别
- springMVC Model、 ModelMap 和 ModelAndView的区别
- SpringMVC框架中ModelAndView、Model、ModelMap的区别与使用
- Spring框架中ModelAndView、Model、ModelMap区别
- SpringMVC-Model和ModelAndView的区别
- SPRING框架中ModelAndView、Model、ModelMap区别【转】
- SpringMVC Model、ModelMap和ModelAndView的区别和用法
- SpringMVC中Model/ModelMap/ModelAndView
- ModelAndView 和 Model ModelMap 返回值区别
- SPRING框架中ModelAndView、Model、ModelMap区别
- SpringMVC中JSP取不到ModelAndView,ModelMap的数据原因
- springmvc Model和ModelAndView的区别