Struts2 ModelDriven 接口如何工作

发布于 2024-10-08 11:33:54 字数 162 浏览 0 评论 0原文

我有一个疑问。 Struts2 Modeldriven 界面如何工作。在我的应用程序中,我使用了单一表单。我将 setter 和 getter 放置得与表单名称相同。是否可以使用 setter 和 getter 放置多个 ModelDriven 对象。如果我这样放置那么它会如何识别?

I have a doubt. How the Struts2 Modeldriven interface works. In my application I used for a single form. And I placed setters and getters as same as form names. Is it possible to place multiple ModelDriven objects with setter and getter. If I placed like that then how it will recognize?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

岁月静好 2024-10-15 11:33:54

任何实现 ModelDriven 接口的操作都必须提供 getModel() 方法,该方法返回表示操作模型的对象。传递给操作的任何参数都被假定为模型的子属性。 ModelDriven 操作中的每个操作只能有一个模型。

例如,假设我们有一个名为 Profile 的模型和一个用于编辑个人资料的操作。在我们的表单中,我们的网站有一个文本字段。如果不使用 ModelDriven,您的操作上就会有 getWebsitesetWebsite 方法。使用 ModelDriven 时,将调用模型上的 getter 和 setter。实际上,getModel().setWebsite("http://stackoverflow.com")

示例

public class EditProfileAction extends ActionSupport implements ModelDriven<Profile> {
    private Profile profile;

    // todo: other methods

    @Override
    public Profile getModel() {
        return profile;
    }
}

public class Profile {
    private String website;

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }
}

Any action implementing the ModelDriven interface must supply a getModel() method which returns the object that represents the action's model. Any parameters passed to the action are assumed to be sub-properties of the model. You may only have one model per action in a ModelDriven action.

For example, lets assume we have a model called Profile and an action to edit our profile. In our form, we have a text field for our website. Without using ModelDriven, you would have getWebsite and setWebsite methods on your action. With ModelDriven, the getter and setter on the model would be called instead. Effectively, getModel().setWebsite("http://stackoverflow.com").

Example

public class EditProfileAction extends ActionSupport implements ModelDriven<Profile> {
    private Profile profile;

    // todo: other methods

    @Override
    public Profile getModel() {
        return profile;
    }
}

public class Profile {
    private String website;

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }
}
酷到爆炸 2024-10-15 11:33:54

同意... ModelDriven 看起来类似于 Struts1 中的 ActionForm,并且具有相似性,我相信提供了这种方法。即使这样,如果您有模型,并且具有许多组合,您也必须遵循 ObjectBacked 方法来设置模型中包含的对象值。

Agree... ModelDriven looks similar to ActionForm in Struts1 and to have the similarity i believe this approach is provided. Even then if u have your model, with many composition u would have to follow the ObjectBacked approach to set the contained object values in the model.

后来的我们 2024-10-15 11:33:54

对于 ModelDriven,您一次只能填充一个 pojo。您不能在单个操作类中使用多个 ModelDriven。因为 getModel() 方法填充了 ModelDrive 中指定的 Pojo 的对象。它会尝试在该 pojo 中查找 getter。字段名称应与表单名称匹配。

In case of ModelDriven, you can populate only one pojo at a time. You can not use multiple ModelDriven in single action class. Because getModel() method populate the Object of the Pojo which is specified in ModelDrive<Pojo>.It will try to find the getter in this pojo. The name of the field should be matched with the form names.

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