In my earlier posts for Struts 2 for Beginners and Struts 2 Annotation Example, you will notice that the java bean properties are part of Action classes. Actually this is not a good design because most of the times, we would want to have bean classes to hold the application elements data and we want to use them across the application.

Struts 2 provide two different approaches through which we can separate java bean properties from action classes and thus reuse the same java bean with different action classes, business logic or in different JSP pages. These approaches are – Object-backed action classes and ModelDriven action classes.

We will understand both the approaches with simple struts 2 web application. Our final project will look like below image.


I am using annotations to create action classes, that’s why there is no struts.xml file. Also I have converted the dynamic web project to maven and added struts 2 dependencies in pom.xml, that’s why you don’t see any struts jar in lib directory.

Before we move on to action classes for both approaches, we will look into the common components of the web application.



Common Java Bean with some properties and getter-setter methods.

Now let’s move to the implementation of Object-backed and ModelDriven action classes.

  1. Object-backed Action Class

    For object-backed action classes, we need change in action classes as well as request and response HTML/JSP pages. In action class, we have to add a class level variable for the java bean.

    Above action class is self explanatory, the only point to note is the variable name of User bean “user”. We have to use the same name in request and response pages. Let’s look at the request and response JSP pages.


    Notice the textfield names are user.userID and user.password. It’s clear that the first part is same as User variable in action class and second part is same as User class variables.

    Did you noticed that in action class execute() method, we are setting userName variable value, we will use it in response JSP page.


    Now when we execute above action, we get following response pages.

  2. Struts2-action-object-backed-jsp-450x207


    That’s all for object-driven action classes implementation, let’s move to the implementation of Modeldriven action classes.

  4. ModelDriven Action Class

    For ModelDriven action classes, we have to implement com.opensymphony.xwork2.ModelDriven interface. This interface is type-parameterized using Java Generics and we need to provide the type as our bean class. ModelDriven interface contains only one method getModel() that we need to implement and return the bean object.

    The other important point to note is that action class should have a class level variable for the bean and we need to instantiate it. Our ModelDriven action class looks like below.

    Our request and result pages look like below code.



    Notice that in JSP pages, we don’t need to prefix the HTML tags name with action class bean variable name. However we need to make sure that names match with the java bean class variable name.

    When we execute this action, we get following response pages.



    Thats all for implementation of ModelDriven action classes.

If you look into both the approaches, ModelDriven approach is easy to implement and maintain as it doesn’t require change in the JSP pages.

Further Reading: You know there are four different ways to create Action classes in Struts 2, learn about them as Struts 2 Action.

By admin

Leave a Reply

%d bloggers like this: