何时在Spring中使用ModelAndView vs Model? [英] When to use ModelAndView vs Model in Spring?

查看:97
本文介绍了何时在Spring中使用ModelAndView vs Model?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 Spring 中的专家来说,这可能听起来很愚蠢,但我不得不问。你如何决定何时使用 ModelAndView 模型

This might sound dumb to the experts in Spring but I have to ask. How do you decide on when to use ModelAndView vs. Model?

毕竟我研究过的最佳答案是这个。它已经提到 ModelAndView 是旧的方式, Model 带有 String 返回是 Spring 中的一种新方法。

After all I have researched the best answer I have found is this one. It has mentioned that the ModelAndView is an old way and the Model with a String returned is a new way in Spring.

我的问题是我们是否应该弃用旧的 ModelAndView 现在我们已经模型手头?或者是否有任何需要使用 ModelAndView 的情况。

My question is shall we deprecate the old ModelAndView now that we have Model in hand? Or is there any cases that you need to use ModelAndView for it.

此外,有谁知道为什么有将 ModelAndView 更改为模型字符串将值更改为查看,有什么好处?

Also, does anyone know why the have to change ModelAndView to Model and String value as View, and what are the benefits?

推荐答案

我总是使用控制器方法返回 ModelAndView 的方法。仅仅因为它倾向于使控制器方法更简洁。方法参数现在严格输入参数。所有输出相关数据都包含在从该方法返回的对象中。

I always use the approach where controller methods return ModelAndView. Simply because it tends to make the controller methods a little more terse. The method parameters are now strictly input parameters. And all output related data is contained in the object returned from the method.

ModelAndView 样式似乎与那些不喜欢将输入参数更新为方法的人产生共鸣。坚持认为这会构成副作用,这是一种危险的模式,因为你无法可靠地预测该方法将要做什么 - 它可以返回返回对象中的数据,或者它可以更新任何输入参数中的任何内容。

The ModelAndView style seems to resonate with people who don't like updating input parameters to a method. Sticking to the belief that this would constitute a side effect, a dangerous pattern because you cannot reliably predict what the method is going to do - It could return data in the returned object, or it could have updated anything in any of the input arguments.

所以有些人仍然会继续选择 ModelAndView

So some people will still continue to prefer ModelAndView.

带有 Model 的新样式作为方法参数,并返回字符串作为视图名称。似乎来自略有不同的设计方法。这里,模型对象被认为是在返回到渲染它们的视图之前传递给多个处理程序的一类事件或项。它提醒我如何在AWT / Swing世界中处理事件。此模型与多个处理程序可以构建在 Model 对象之上的方法更加一致,直到它到达视图。

The new style with Model as method parameter and returned string as view name. Seems to have come from a slightly different design approach. Here the model objects are considered to be sort of events or items that are passed to multiple handlers, before being returned to the view where they are rendered. It reminds me how events are handled in the AWT / Swing world. This model is more coherent with the approach where multiple handlers could build on top of the Model objects, till it reaches a view.

因此,在一天结束时,似乎没有一个明确的理由来批评或推广这两种方法。您应该使用与整体设计理念更加一致的风格。

So at the end of the day, there does not seem to be a definite reason to criticise or promote either approach. You should use the style that feels more consistent to your overall design philosophy.

希望这会有所帮助。

这篇关于何时在Spring中使用ModelAndView vs Model?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆