为什么模型驱动的操作优于对象支持的 bean 属性 [英] why is model-driven action preferred over object backed bean properties

查看:17
本文介绍了为什么模型驱动的操作优于对象支持的 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?

是不是和view层需要提引用名有关

does the reason have something to do with the need to mention reference name in the view layer

推荐答案

在处理多个属性的情况下,本书提倡使用一个对象来保存这些属性,而不是将它们直接放在操作上以使事情变得更容易为你.考虑以下示例:

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:

public class CreateNewWidgetAction extends ActionSupport {
    private String property1;
    private String property2;
    private Long property3;
    ...

    public String execute() throws Exception {
        Widget widget = new Widget();
        // set properties on widget
    }

    // getters and setters for properties here
}

public class CreateNewWidgetAction extends ActionSupport {
    private Widget widget;

    public String execute() throws Exception {
        // sub properties for widget were already set, less work to do here
    }

    // getter and setter for widget here (or the
    // getModel method if you are using the Model Driven approach)
}

在第二个示例中,我们直接在 Widget 上设置属性(假设 Widget 具有 property1、property2 和 property3).

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.

更新:如果您选择实现 ModelDriven,那么您可以在表单中将上述属性引用为 property1property2property3 等.此外,由于您的操作是由单个模型驱动的,因此所有表单参数都被视为该模型的子项.

Updated: If you choose to implement ModelDriven, then you would reference the properties above in your form as property1, property2, property3, etc. Additionally, since your action is driven by a single model, all form parameters are considered to be children of the model.

如果您选择不实现 ModelDriven,那么您可以在表单中将上述属性引用为 widget.property1widget.property2widget.property3 等.这种方法的好处是您可以在操作上拥有与小部件上的属性不对应的其他属性.

If you choose not to implement ModelDriven then you would reference the properties above in your form as widget.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:

与对象支持的 JavaBeans 属性一样,ModelDriven 操作也允许我们使用复杂的 Java 对象来接收我们的数据.这两种方法之间的差异很小,选择一种方法不会产生功能上的影响.

Like the object-backed JavaBeans property, the ModelDriven action also allows us to use a complex Java object to receive our data. The differences between these two methods are slight, and there are no functional consequences to choosing one over the other.

- Struts 2 实战,第 3 章.使用 Struts 2 动作 >将数据传输到对象上 - Pg.62

这篇关于为什么模型驱动的操作优于对象支持的 bean 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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