SpringMVC 域对象共享数据的实现示例

网友投稿 544 2022-12-14

SpringMVC 域对象共享数据的实现示例

SpringMVC 域对象共享数据的实现示例

目录使用ModelAndView向request域对象共享数据使用Model向request域对象共享数据使用map向request域对象共享数据使用ModelMap向request域对象共享数据Model、ModelMap、Map的关系向session域共享数据向application域共享数据

使用ModelAndView向request域对象共享数据

index.html

使用ModelAndView

控制器

/**

* ModelAndView有Model和View的功能

* Model主要用于向请求域共享数据

* View主要用于设置视图,实现页面跳转

*/

@RequestMapping("/testModelAndView")

public ModelAndView success(){

ModelAndView modelAndView = new ModelAndView();

modelAndView.addObject("username","gonghr"); //向请求域共享数据

modelAndView.setViewName("success"); //设置视图名称,实现页面跳转

return modelAndView; //返回

}

success.html

sucess

使用Model向request域对象共享数据

index.html

使用Model

控制器

@RequestMapping("/testModel")

public String testModel(Model model){

model.addAttribute("company","JLU");

return "success";

}

success.html

sucess

使用map向request域对象共享数据

index.html

使用Map

控制器

@RequestMapping("/testMap")

public String testMap(Map map){

map.put("age","Nineteen");

return "success";

}

sucess.html

sucess

使用ModelMap向request域对象共享数据

index.html

使用ModelMap

控制器

@RequestMapping("/testModelMap")

public String testModelMap(ModelMap modelMap){

modelMap.addAttribute("major","software engineering");

return "success";

}

success.html

Model、ModelMap、Map的关系

经过测试发http://现:除了ModelAndView的实现类是Modelhttp://AndView,Model、Map和ModelMap 的实现类都是BindingAwareModelMap。

Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的

class of ModelAndView: class org.springframework.web.servlet.ModelAndView

class of Model: class org.springframework.validation.support.BindingAwareModelMap

class of Map: class org.springframework.validation.support.BindingAwareModelMap

class of ModelMap: class org.springframework.validation.support.BindingAwareModelMap

阅读ModeAndView的源码可以发现,ModeAndView和ModelMap是组合关系。下面是ModeAndView的部分源码。

public class ModelAndView {

@Nullable

private ModelMap model;

public ModelAndView() {

}

public ModelMap getModelMap() {

if (this.model == null) {

this.model = new ModelMap();

}

return this.model;

}

public ModelAndView addObject(String attributeName, @Nullable Object attributeValue) {

this.getModelMap().addAttribute(attributeName, attributeValue);

return this;

}

当ModeAndView调用addObject()方法时其实是调用ModelMap的addAttribute()方法,本质上与ModelMap是一样的。

各个类之间的关系如下:

public interface Model{}

public class ModelMap extends LinkedHashMap {}

public class ExtendedModelMap extends ModelMap implements Model {}

public class BindingAwareModelMap extends ExtendedModelMap {}

四种方式本质上都是调用的Model接口中的addAttribute方法

向session域共享数据

index.html

使用Session

控制器

@RequestMapping("/testSession")

public String testSession(HttpSession session){

session.setAttribute("message","session scope");

return "success";

}

success.html

<p th:text="${session.message}">

向application域共享数据

index.html

使用Application

控制器

@RequestMapping("/testApplication")

public String testApplication(HttpSession session){

ServletContext application = session.getServletContext();

application.setAttribute("testApplication","hello,application");

return "success";

}

success.html

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:logback输出日志屏蔽quartz的debug等级日志方式
下一篇:JDK8中Optional类巧用之判空操作
相关文章

 发表评论

暂时没有评论,来抢沙发吧~