How the Struts2 ModelDriven Interface Works

I have doubts. How the Modeldriven interface Modeldriven . In my application I used one form. And I placed the setters and getters in the same way as the names of the forms. Is it possible to place multiple Modeldriven objects using setter and getter. If I put it like that, how will he know?

+7
source share
3 answers

Any action that implements the ModelDriven interface must provide a getModel() method that returns an object that represents the action model. Any parameters transferred to the action are considered sub-properties of the model. You can only have one model per action in a ModelDriven action.

For example, suppose we have a model called Profile and an action to edit our profile. In our view, we have a text box for our site. Without using ModelDriven , your action would have getWebsite and setWebsite . Using ModelDriven , the receiver and setter on the model will 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; } } 
+9
source

I agree ... ModelDriven is similar to ActionForm in Struts1 and has similarities, I believe this approach is provided. Even then, if you have your model, with many compositions u will have to follow the ObjectBacked method to set the values ​​of the objects contained in the object in the model.

+3
source

In the case of ModelDriven you can only populate one pojo at a time. You cannot use multiple ModelDriven in the same action class. Because the getModel() method populates the Pojo object specified in ModelDrive<Pojo> . He will try to find a getter in this pojo. The field name must match the names of the forms.

0
source

All Articles