1

i am learning struts2 from the book struts-2 in action. they say that for transferring data onto objects model-driven actions is preferred over object-backed beans properties.

can some1 explain me why they say so?

does the reason have something to do with the need to mention reference name in the view layer

SonOfTheEARTh
  • 1,858
  • 5
  • 21
  • 23

2 Answers2

6

In cases where you are dealing with several properties, the book advocates using an object to hold those properties, rather than having them directly on the action to make things easier for you. Consider the following examples:

public class CreateNewWidgetAction extends ActionSupport {
    private String property1;
    private String property2;
    private Long property3;
    ...

    public String execute() throws Exception {
        Widget widget = new Widget();
        // set properties on widget
    }

    // getters and setters for properties here
}

public class CreateNewWidgetAction extends ActionSupport {
    private Widget widget;

    public String execute() throws Exception {
        // sub properties for widget were already set, less work to do here
    }

    // getter and setter for widget here (or the
    // getModel method if you are using the Model Driven approach)
}

In the second example, we set the properties directly on Widget (assuming that widget has a property1, property2, and property3).

Hopefully you can see how this would simplify your code in examples where you are dealing with a lot of properties.

Updated: If you choose to implement ModelDriven, then you would reference the properties above in your form as property1, property2, property3, etc. Additionally, since your action is driven by a single model, all form parameters are considered to be children of the model.

If you choose not to implement ModelDriven then you would reference the properties above in your form as widget.property1, widget.property2, widget.property3, etc. The upside to this approach is that you can have other properties on the action which don't correspond to properties on widget.

Aside from that, there is no difference. In fact, the book even says as much:

Like the object-backed JavaBeans property, the ModelDriven action also allows us to use a complex Java object to receive our data. The differences between these two methods are slight, and there are no functional consequences to choosing one over the other.

- Struts 2 in Action, Chapter 3. Working with Struts 2 actions > Transferring data onto objects - Pg. 62

Community
  • 1
  • 1
Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
2

well model driven is better with respect to if you have nesting property for example if you have user bean and it has name property den in the jsp side you have to do something like user.name so that OGNL can find out that you are pointing user object and inside that name property.

In case of model driven interface this interface will put the bean object on the top of value stack when a request came for an action since this bean is on the top of value stack you need not to do something like user.name in your jsp.

I hope it will answer your question

Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204