How Struts2 ModelDriven interface works
Asked Answered
K

3

9

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?

Klaxon answered 22/12, 2010 at 11:52 Comment(4)
Avoid modelDriven unless your form is a multipage form (and perhaps even then).Charlottetown
@Charlottetown what do you mean by multipage form?Rosemare
@Rosemare I'm thinking from the server side, on the client side they will be separate pages and thus separate forms but on the server side you are using multiple action on a single model and conceptually you are thinking of the whole thing as a single form. That is what I meant, perhaps I could have picked my words better. If things need to be looked up ajax techniques tend to be more intuitive and then a single submission can be done... there are trade offs of course.Charlottetown
I used ModelDriven not long ago. I wanted an object on the top of the stack but didn't know what object that would be, except that it would be a JPA entity. A call would prepare the action and set the correct entity, this way when the ORM entities were updated the service and action layer could remain constant. What the client could call would change however. Anyways the point is I wanted something at the top of the stack but didn't know until run time, in that case I thought it did help.Charlottetown
D
12

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;
    }
}
Donte answered 22/12, 2010 at 15:0 Comment(3)
I kind of think model driven is silly. If your model is called Person why not just have a person property? Struts2 will be able to access deeply nested properties from the form just as we can from the view. With model driven you can only have one model, while done the typical way you can have as many objects as you want. The issue is probably tangled in object acquisition which aught to be solved with DI, then we don't need any Preparable or ModelDriven interfaces.Charlottetown
While I find Preparable quite useful, I agree on ModelDriven, Quaternion. I personally just use objects directly on the action.Donte
Agree with Quaternion. I also didn't get the point of ModelDriven. Besides, when you need to store data in memory, store it in the app's map available through ApplicationContextAware, if you need to store user data (like the profile stuff), store it in the session map available through SessionAware interface. Persist in database always.Hoplite
D
3

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.

Directly answered 19/1, 2011 at 18:20 Comment(0)
P
0

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.

Periotic answered 26/9, 2012 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.