簡體   English   中英

我可以在Spring MVC中的ModelAndView中返回兩個模型

[英]Can i return two models in ModelAndView in Spring MVC

如果我有一個型號,假設我有以下代碼

MyBean bean = new MyBean();
bean.setName("Mike");
bean.setMessage("Meow!");

return new ModelAndView("welcomePage","model",bean);

但如果我有兩個或三個模型

假設在我的一個視圖中,我希望模型具有用戶詳細信息,購物車詳細信息和歷史記錄詳細信息

我如何使用ModelAnd View返回2-3個模型

您可以通過多種方式實現此目標,但也許最簡單的方法是使用地圖

Map<String, Object> model = new HashMap<String, Object>();
model.put("bean", bean);
model.put("userdetails", userdetails);
//and so on
return new ModelAndView("welcomePage", "model", model);

然后,在您的頁面中,您只需在訪問它時添加額外的級別

User's name is ${ model.userdetails.username }

或者,您也可以將處理程序簽名更改為此類型

public String handleRequest(Model model){
   //arbitrary handling code
   model.addAttribute("bean", bean);
   model.addAttribute("userdetails", userdetails);
   //etc
   return "welcomePage";
 }

當你這樣做時,你實際上不必返回模型,因為Spring在你收到它之前會保留它,然后可以訪問它。 我個人認為這種方法更好一點,因為它使單元測試更容易。 您所要做的就是檢查字符串返回值並使用Spring模型(或您自己的模擬對象實現Model接口)。

編輯要解決評論:

此源提供了一些示例,並討論了一些支持的方法簽名。 具體來說,請查看第15.3.2.3節,了解可以傳遞給處理程序方法的參數。

基本上,Spring使用@RequestMapping注釋來確定應根據給定請求調用哪些方法。 然后,Spring可以在調用方法之前檢查方法簽名並生成適當的參數。 在返回ModelAndView對象的情況下,根據您提供的參數調用構造函數時,將創建Model 如果未提供任何模型對象,則會創建空模型。 但是,當您指定應將模型作為處理程序方法的參數接收時,Spring會為您創建一個Model對象的實例並將其傳遞給您的方法。 Spring保留對該模型的引用,並且當您的方法返回時,將該模型傳遞給Web視圖(例如JSP解析器)。

它實際上與返回一個ModelAndView對象實際上是相同的,除了它使得單元測試變得更加容易和坦率地說,IMO使得實現更清晰,更優雅。

注意:請記住, Model實際上只是一個特殊的Map對象(因此Spring支持在方法簽名中交替使用ModelMap )。 還有一些其他方法,它還支持隱式屬性命名。 例如,如果您只是簡單地傳遞一個對象而沒有給它命名,那么Model對象將根據對象類型等確定對象的名稱。但是,如果您總是為添加的對象提供“鍵”對於模型,它的行為與Map完全相同。

是的,您可以通過將任意數量的模型屬性放入Map來返回它們:

Map<String, Object> model = new HashMap<String, Object>();
model.put("model", bean);
model.put("userdetails", ...);
model.put("shoppingcart", ...);
return new ModelAndView("welcomePage", model);

注意術語 - 模型是一個映射,它由模型屬性(單個對象)組成, new ModelAndView("welcomePage","model",bean)是一個方便的構造函數,用於創建具有單個屬性的模型。

給出了很好的例子。 只是為了增加組合,我真的成為了基於注釋的方法的粉絲。 我喜歡它,因為它提供了一種非常干凈的實現方式。 這是一個例子......

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@Scope("prototype")
@RequestMapping("/testing")
public class TestController {

    @Autowired
    TestFactory factory;

    @RequestMapping(value = "/test1", method = RequestMethod.POST)
    public void test1(String something, String something2, Model model) {
        List<Map<String, String>> results = 
             factory.getSomethingCool(something1, something2);

        model.addAttribute("something1", something1);
        model.addAttribute("something2", something2);
        model.addAttribute("results", results);
    }

    @RequestMapping(value = "/test2", method = RequestMethod.POST)
    public void test1(String something1, String something2, Model model) {
        List<String> results = 
            factory.getSomethingElseCool(something1, something2);

        model.addAttribute("something1", something1);
        model.addAttribute("something2", something2);
        model.addAttribute("results", results);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM