欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2022-03-17 09:53:02|栏目:JAVA代码|点击:

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

index.html

<a th:href="@{/testModelAndView}" rel="external nofollow" >使用ModelAndView</a>


控制器

 /**
     * 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
<p th:text="${username}"></p>

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

index.html

<a th:href="@{/testModel}" rel="external nofollow" >使用Model</a> <br>

控制器

 @RequestMapping("/testModel")
    public String testModel(Model model){
        model.addAttribute("company","JLU");
        return "success";
    }

success.html

sucess
<p th:text="${company}"></p> <br>

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

index.html

<a th:href="@{/testMap}" rel="external nofollow" >使用Map</a> <br>

控制器

  @RequestMapping("/testMap")
    public String testMap(Map<String, Object> map){
        map.put("age","Nineteen");
        return "success";
    }

sucess.html

sucess
<p th:text="${age}"></p> <br>

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

index.html

<a th:href="@{/testModelMap}" rel="external nofollow" >使用ModelMap</a> <br>

控制器

 @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("major","software engineering");
        return "success";
    }

success.html

<p th:text="${major}"></p> <br>

Model、ModelMap、Map的关系

经过测试发现:除了ModelAndView的实现类是ModelAndViewModelMapModelMap 的实现类都是BindingAwareModelMap

ModelModelMapMap类型的参数其实本质上都是 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的源码可以发现,ModeAndViewModelMap是组合关系。下面是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()方法时其实是调用ModelMapaddAttribute()方法,本质上与ModelMap是一样的。

各个类之间的关系如下:

public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}

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

向session域共享数据

index.html

<a th:href="@{/testSession}" rel="external nofollow" >使用Session</a> <br>

控制器

 @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("message","session scope");
        return "success";
    }

success.html

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

向application域共享数据

index.html

<a th:href="@{/testApplication}" rel="external nofollow" >使用Application</a> <br>

控制器

@RequestMapping("/testApplication")
    public String testApplication(HttpSession session){
        ServletContext application = session.getServletContext();
        application.setAttribute("testApplication","hello,application");
        return "success";

    }

success.html

<p th:text="${application.testApplication}"></p> <br>

上一篇:SpringBoot自定义/error路径失效的解决

栏    目:JAVA代码

下一篇:使用@CachePut 更新数据库和更新缓存

本文标题:SpringMVC 域对象共享数据的实现示例

本文地址:http://www.codeinn.net/misctech/196555.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有