简体   繁体   English

创建新模型和视图或将模型作为方法参数传递之间有什么区别

[英]What is the difference between creating a new modelandview or passing in a model as method parameter

I got into the habit of doing this, so that in my unit tests I could check what had been added to model: 我养成了这样做的习惯,因此在单元测试中,我可以检查已添加到模型中的内容:

@RequestMapping(value = "/Foo", method = RequestMethod.GET)
public ModelAndView goHome()
{
  ModelandView mav = new ModelAndView("foobar.jsp");
  mav.addObject("bar", new Bar());
  return mav;
}

Is this better: 这是否更好:

@RequestMapping(value = "/Foo", method = RequestMethod.GET)
public String goHome(final Model model)
{
  model.addAttribute("bar", new Bar());
  return "foobar.jsp";
}

The difference is only semantic. 区别仅在于语义。 If you do not create the ModelAndView object Spring will do it for you. 如果不创建ModelAndView对象,Spring将为您完成。

Generally the second approach is preferable since it's a lot easier to unit test, especially if you pass a Map instead of your model. 通常,第二种方法是可取的,因为它更易于进行单元测试,尤其是当您通过Map而不是模型时。


EDIT To clarify on testing (based on jUnit ). 编辑澄清测试(基于jUnit )。 I find the following signature preferable: 我发现以下签名更可取:

@RequestMapping(value = "/Foo", method = RequestMethod.GET)
public String goHome(final Map model) {
    model.addAttribute("bar", new Bar());
    return "foobar.jsp";
}

This allows us to create a test without even knowing Spring is involved 这使我们可以创建测试,甚至不知道涉及Spring

@Test
public void testGoHome() {
    // Setup
    Controller controller = ...
    Map<String, Bar> model = new HashMap<String, Bar>();

    // Test
    assertEquals("foobar.jsp", controller.goHome(model));
    assertNotNull(model.get("bar"));
}

This example is based on a Map , but could also be a ModelMap or even Model if you preferred. 此示例基于Map ,但如果您愿意,也可以是ModelMap甚至是Model

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 春季模型与模型和视图的区别 - Difference between model and modelandview in spring 创建无参数方法和字符串参数的区别? - Difference between creating a method with no parameter and a string parameter? 在Java中将对象和原始数据作为参数传递有什么区别? - what is the difference between passing an object and a primitive data as the parameter in Java? Java作为参数,泛型方法和方法对象之间有什么区别? - Java What is difference between generic method and a method object as a parameter? 使用 new 关键字和使用 clone 方法创建对象的区别 - Difference between creating an object using new keyword and using clone method 在Java中,修改后的方法参数与其副本之间有什么区别? - What's the difference between a modified method parameter and that of a copy of it in Java? 在modelandview内重定向和重定向之间的区别 - difference between redirect and redirect inside modelandview “ Factory方法”和“使用New创建实例”之间有什么区别? - What's the difference between “Factory Method” and “using New to create an instance”? 通过将模型类的实例传递给可比对象来使用可比对象,并将包装器传递给可比对象之间有什么区别? - What is the difference between using comparable by passing instance of our model class to comparable and passing wrapper to comparable? 带类型参数的方法与泛型方法类型参数声明之间的理论区别是什么 - What is the theoretical difference between method with type parameter and generics method type parameter declaration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM