为什么模型驱动的操作优于对象支持的 bean 属性

发布于 2024-10-03 07:20:33 字数 138 浏览 2 评论 0原文

我正在从《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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野生奥特曼 2024-10-10 07:20:33

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

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)。

希望您可以在处理大量属性的示例中看到这将如何简化您的代码。

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

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

除此之外,没有什么区别。事实上,这本书甚至也这么说:

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

- Struts 2 实践,第 3 章。使用 Struts 2 操作 >将数据传输到对象上 - 第 12 页62

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)
}

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 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.

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:

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 in Action, Chapter 3. Working with Struts 2 actions > Transferring data onto objects - Pg. 62

违心° 2024-10-10 07:20:33

如果您有嵌套属性,那么模型驱动会更好
例如,如果你有 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文