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;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…