Struts2 ModelDriven Example


In this example, we will see how to use the ModelDriven interface to pass parameters to action through a Java object (Model).


1. Technologies used

  • Struts 2.3.16
  • Maven 3.0.5
  • Eclipse 4.4
  • JDK 1.6
  • Tomcat 6.0


2. Project structure



3. Struts2 dependency

File : pom.xml


4. Model class

It is a simple Java class with some attributes and their setter/getter methods.

File : User


5. Action class

It should implement the ModelDriven interface and override a getModel() method which returns the action’s model, in our case it is User object.

The User object must be initialized.

File : UserAction


6. JSP pages

Login page : login.jsp

The form properties must match the User object attributes.


Welcome page : welcome.jsp


7. Struts config

File : struts.xml


8. Test It

URL : http://localhost:8080/Struts2-Model-Driven-Example/login.action



We enter some login and password (other than Walid/111) and submit.

While we press the submit button, the form data will automatically be set to the user properties and then transferred to the UserAction through user object.



We enter Walid/111 and click the submit button



Download source code