本文介绍了Struts2 ModelDriven 接口如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个疑问.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?

推荐答案

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

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.

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

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

示例

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

这篇关于Struts2 ModelDriven 接口如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 20:44