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