SpringMVC 域對象共享數據的實現示例

使用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>

到此這篇關於SpringMVC 域對象共享數據的實現示例的文章就介紹到這瞭,更多相關SpringMVC 域對象共享數據內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: