为什么模型驱动的操作优于对象支持的 bean 属性
我正在从《struts-2 in action》这本书中学习struts2。他们说,将数据传输到对象时,模型驱动的操作优于对象支持的 bean 属性。
有人可以解释一下为什么他们这么说吗?
原因是否与需要在视图层中提及参考名称有关
i am learning struts2 from the book struts-2 in action. they say that for transferring data onto objects model-driven actions is preferred over object-backed beans properties.
can some1 explain me why they say so?
does the reason have something to do with the need to mention reference name in the view layer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您要处理多个属性,本书提倡使用对象来保存这些属性,而不是将它们直接放在操作上,以使事情变得更容易。考虑以下示例:
在第二个示例中,我们直接在 Widget 上设置属性(假设 Widget 有 property1、property2 和 property3)。
希望您可以在处理大量属性的示例中看到这将如何简化您的代码。
更新:
如果您选择实现
ModelDriven
,那么您将在表单中将上述属性引用为property1
、property2
、property3
> 等。此外,由于您的操作是由单个模型驱动的,因此所有表单参数都被视为该模型的子级。如果您选择不实现
ModelDriven
,那么您将在表单中将上述属性引用为widget.property1
、widget.property2
、widget.property3
等。这种方法的优点是您可以在操作上拥有与小部件上的属性不对应的其他属性。除此之外,没有什么区别。事实上,这本书甚至也这么说:
In cases where you are dealing with several properties, the book advocates using an object to hold those properties, rather than having them directly on the action to make things easier for you. Consider the following examples:
In the second example, we set the properties directly on Widget (assuming that widget has a property1, property2, and property3).
Hopefully you can see how this would simplify your code in examples where you are dealing with a lot of properties.
Updated:
If you choose to implement
ModelDriven
, then you would reference the properties above in your form asproperty1
,property2
,property3
, etc. Additionally, since your action is driven by a single model, all form parameters are considered to be children of the model.If you choose not to implement
ModelDriven
then you would reference the properties above in your form aswidget.property1
,widget.property2
,widget.property3
, etc. The upside to this approach is that you can have other properties on the action which don't correspond to properties on widget.Aside from that, there is no difference. In fact, the book even says as much:
如果您有嵌套属性,那么模型驱动会更好
例如,如果你有 user bean 并且它在 jsp 端有 name 属性 den,你必须执行类似 user.name 的操作,以便 OGNL 可以发现你正在指向 user 对象并在该 name 属性内。
在模型驱动接口的情况下,当请求某个操作时,此接口会将 bean 对象放在值堆栈的顶部,因为此 bean 位于值堆栈的顶部,因此您不需要在 jsp 中执行类似 user.name 的操作。
我希望它能回答你的问题
well model driven is better with respect to if you have nesting property
for example if you have user bean and it has name property den in the jsp side you have to do something like user.name so that OGNL can find out that you are pointing user object and inside that name property.
In case of model driven interface this interface will put the bean object on the top of value stack when a request came for an action since this bean is on the top of value stack you need not to do something like user.name in your jsp.
I hope it will answer your question