假设我有以下代码,如果我有一个模型
MyBean bean = new MyBean();
bean.setName("Mike");
bean.setMessage("Meow!");
return new ModelAndView("welcomePage","model",bean);但是如果我有两三个模型,比如
假设在我的一个视图上,我想要包含用户详细信息、购物车详细信息和历史详细信息的模型
如何使用ModelAnd视图返回2-3个模型
发布于 2011-02-22 01:09:05
您可以使用多种方法来完成此操作,但最简单的方法可能是使用地图
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 mock模型(或者您自己的实现Model接口的mock对象)。
编辑以处理注释:
This source给出了一些例子,并讨论了一些支持的方法签名。具体地说,请查看15.3.2.3节,了解可以传递给处理程序方法的参数的讨论。
基本上,Spring使用@RequestMapping注释来确定应该根据给定的请求调用哪些方法。Spring随后能够检查方法签名,并在调用方法之前生成适当的参数。在返回ModelAndView对象的情况下,将根据您提供的参数在调用构造函数时创建Model。如果不提供任何模型对象,则会创建一个空模型。但是,当您指定应将模型作为处理程序方法的参数接收时,Spring会为您创建一个Model对象的实例,并将其传递给您的方法。Spring保留对该模型的引用,当您的方法返回时,将该模型传递给web视图(例如JSP解析器)。
它实际上和返回一个ModelAndView对象是一样的,只是它使得单元测试变得容易得多,坦率地说,IMO使得实现更干净、更优雅。
注意:请记住,Model实际上只是一个特殊的Map对象(这就是为什么Spring支持在方法签名中交替使用Model或Map )。还有一些额外的方法,它还支持隐式属性命名。例如,如果您只是传递一个对象,而不给它一个名称,那么Model对象将根据对象类型确定如何命名该对象,等等。但是,如果您总是为添加到模型中的对象提供一个“键”,那么它的行为与Map完全相同。
发布于 2011-02-22 01:08:36
可以,您可以通过将模型属性放置到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)是一个方便的构造器,用于创建具有单个属性的模型。
发布于 2011-02-22 01:24:33
这里有一些很好的例子。为了补充一下,我真的成为了基于注释的方法的粉丝。我喜欢它,因为它提供了一种非常干净的实现方式。这里有一个例子..。
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);
}
}https://stackoverflow.com/questions/5068892
复制相似问题