Java 我可以在SpringMVC中返回ModelAndView中的两个模型吗
如果我有一个模型,假设我有以下代码Java 我可以在SpringMVC中返回ModelAndView中的两个模型吗,java,spring,spring-mvc,Java,Spring,Spring Mvc,如果我有一个模型,假设我有以下代码 MyBean bean = new MyBean(); bean.setName("Mike"); bean.setMessage("Meow!"); return new ModelAndView("welcomePage","model",bean); 但是如果我有两三个这样的模型 假设我有一个观点 我想与userdetails,shoppingcart的细节和历史细节模型 如何使用ModelAnd View返回2-3个模型是的,您可以通过将模型属性放
MyBean bean = new MyBean();
bean.setName("Mike");
bean.setMessage("Meow!");
return new ModelAndView("welcomePage","model",bean);
但是如果我有两三个这样的模型
假设我有一个观点
我想与userdetails,shoppingcart的细节和历史细节模型
如何使用ModelAnd View返回2-3个模型是的,您可以通过将模型属性放置到
映射中来返回任意数量的模型属性:
Map<String, Object> model = new HashMap<String, Object>();
model.put("model", bean);
model.put("userdetails", ...);
model.put("shoppingcart", ...);
return new ModelAndView("welcomePage", model);
Map model=newhashmap();
model.put(“model”,bean);
model.put(“userdetails”,…);
模型.放置(“购物车”,…);
返回新模型和视图(“welcomePage”,模型);
注意术语-模型是一个映射,它由模型属性(单个对象)组成,newmodelandview(“welcomePage”,“model”,bean)
是一个方便的构造函数,用于创建具有单个属性的模型。您可以通过多种方法实现这一点,但最简单的方法可能是使用映射
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);
或者,您也可以将处理程序签名更改为如下内容
public String handleRequest(Model model){
//arbitrary handling code
model.addAttribute("bean", bean);
model.addAttribute("userdetails", userdetails);
//etc
return "welcomePage";
}
当您这样做时,实际上不必返回模型,因为Spring会在您接收到引用之前保持引用,然后可以在之后访问它。我个人觉得这种方法更好一些,因为它使单元测试更容易。您所要做的就是检查字符串返回值并使用Spring模拟模型(或您自己的实现模型
接口的模拟对象)
编辑以处理评论:
给出了一些示例并讨论了一些支持的方法签名。具体来说,请参阅第15.3.2.3节,了解可以传递给处理程序方法的参数的讨论
基本上,Spring使用@RequestMapping
注释来确定基于给定请求应该调用哪些方法。然后,Spring能够在调用方法之前检查方法签名并生成适当的参数。在返回ModelAndView
对象的情况下,根据您提供的参数调用构造函数时,将创建Model
。如果未提供任何模型对象,则会创建一个空模型。但是,当您指定应该接收模型作为处理程序方法的参数时,Spring会为您创建一个model
对象的实例,并将其传递给您的方法。Spring保留对该模型的引用,当您的方法返回时,将该模型传递给web视图(例如JSP解析器)
它实际上与返回一个ModelAndView
对象是一样的,只是它使单元测试变得更容易,坦率地说,IMO使实现更干净、更优雅
注意:请记住,模型
实际上只是一个特殊的映射
对象(因此,Spring支持在方法签名中交替使用模型
或映射
)。还有一些附加方法,它还支持隐式属性命名。例如,如果只传递一个对象而不给它命名,则模型
对象将根据对象类型等确定对象的名称。但是,如果始终为添加到模型中的对象提供“键”,则它的行为与贴图
的行为完全相同。下面给出了一些很好的示例。更重要的是,我真的很喜欢这种基于注释的方法。我喜欢它,因为它提供了一种非常干净的实现方法。这里有一个例子
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);
}
}
import org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.context.annotation.Scope;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.Model;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
@控制器
@范围(“原型”)
@请求映射(“/testing”)
公共类测试控制器{
@自动连线
测试工厂;
@RequestMapping(value=“/test1”,method=RequestMethod.POST)
public void test1(String something,String something,Model Model){
列表结果=
工厂。得到一些很酷的东西(一些东西1,一些东西2);
addAttribute(“something1”,something1);
addAttribute(“something2”,something2);
model.addAttribute(“结果”,结果);
}
@RequestMapping(value=“/test2”,method=RequestMethod.POST)
公共void test1(字符串something1、字符串something2、模型){
列表结果=
工厂。获得一些东西(一些东西1,一些东西2);
addAttribute(“something1”,something1);
addAttribute(“something2”,something2);
model.addAttribute(“结果”,结果);
}
}
你的备选方案很有趣,但我想我不明白。你能再解释一下或者给我指一些外部资源吗?谢谢,老兄,我写了第一部分,但没能得到第二部分。还有一件事,当我返回模型时,谁会收到that@ChrisThompson你会如何用两种不同的方法添加多个对象除了官方的spring mvc之外,internet上还有更多在controller中进行注释的示例吗?我可以说spring文档非常好。这个网站上有很多例子,只需要做一些挖掘。一个快速的谷歌搜索产生了这个问题,你会如何用两种不同的方法添加多个对象你的用户详细信息是什么?这是一张单子吗?
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);
}
}