Pages Navigation Menu

Coding is much easier than you think

Struts 2 ModelDriven Example

Struts 2 ModelDriven Example

 
In this tutorial we gonna talk a bit about Struts suppport for model objects. The Model object is a pakage of information that needs to be sent between different layers of the application. For example for from your view to business layer or from business layer to data layer. To create a ModelDriven Action our Action class should implement the ModelDriven interface and should include the modelDriven interceptor. The modelDriven interceptor is already included in the default stack.

The next step is to implement the getModel() method in such a way that it returns the application domain object, in our example we return the UserDetails object.

Let see this through an example

 


 

1. Folder Structure

 

Struts 2 Model driven example

 

2. Bean Class(Model Class)

 
Now referring to the previous example , the data members userName , password and and its getter and setter method are moved into separate class as shown below.
 
File : UserDetails.java

package com.simplecode.bo;

public class UserDetails {

	private String userName;
	private String password;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}

 

3. Action Class

 
Action class implements the ModelDriven interface, declared the getModel() method to return the userDetails’€™s object. When the form data is submitted to this action, it will transfers the form data into the userDetail properties automatically.

 

File : LoginAction.java
 

package com.simplecode.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.simplecode.bo.UserDetails;

public class LoginAction extends ActionSupport implements ModelDriven {

	private static final long serialVersionUID = 6677091252031583948L;

	// Need to Initiize a mordel driven object
	UserDetails userdetails = new UserDetails();

	public UserDetails getUserdetails() {
		return userdetails;
	}

	public void setUserdetails(UserDetails userdetails) {
		this.userdetails = userdetails;
	}

	public String execute() {

		return SUCCESS;
	}

	public void validate() {
		if (userdetails.getUserName().equalsIgnoreCase("simplecode")
				&& userdetails.getPassword().equals("struts2")) {
			addActionMessage("Welcome " + userdetails.getUserName()
					+ ", you are logged in successfully !");
		} else {
			addActionError("Login failed,"
          + "Enter user name as simplecode and password as struts2");
		}
	}

	// Modern Deiven class implemented Method
	public UserDetails getModel() {
		return userdetails;
	}
}

 

Note: When using the ModelDriven method we need to initialize the Model object((userdetails)) ourselves, so the framework will automatically transfers the form data into the UserDetails object.   And can use only one model object per action class

 

4. Property file

 
project.properties
 

project.username = Username
project.password = Password
project.submit = Login

 

5. JSP page

 
JSP pages for the ModelDriven demonstration.
 
login.jsp
 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
	<h3>Struts 2 ModelDriven Example</h3>
	<s:if test="hasActionErrors()">

		<div class="errors">
			<br />
			<s:actionerror />
		</div>
	</s:if>

	<s:form action="welcome">
		<s:textfield name="userName" key="project.username" />
		<s:password name="password" key="project.password" />
		<s:submit key="project.submit" name="submit" />
	</s:form>
</body>
</html>

 
success.jsp
 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome Page</title>
</head>

<body>
<h3>Struts 2 ModelDriven Example</h3>

	<s:if test="hasActionMessages()">
		<div class="welcome">
		<br/>
			<s:actionmessage />
		</div>
	</s:if>

</body>
</html>

 

6. struts.xml

 

<struts>

  <constant name="struts.custom.i18n.resources" value="project" /> 
	<package name="default" extends="struts-default" namespace="/jsp">
	<action name="Login">
			<result>/jsp/login.jsp</result>
		</action>
		<action name="welcome" class="com.simplecode.action.LoginAction">
			<result name="success">/jsp/success.jsp</result>
			<result name="input">/jsp/login.jsp</result>
		</action>
	</package>
</struts>

 

7. Demo

 

http://localhost:8089/Struts2_ModelDriven/

 

1w

 
When Username and password is invalid, display error message with <s:actionerror/>

 

Login success

 

When Username and password is valid, display welcome message <s:actionmessage/>

 

login success

 

 

Reference

 

About Jamil


Am currently working as a Struts 2, Webservices Developer in a reputed IT Organisations. I can help you with teaching Core java , Struts 2, Webservices and Site Design.

Struts 2 Custom extension example
Struts 2 <s:textfield> example

Leave a Reply

SimpleCodeStuffs located at 511/67 Huynh Van Banh , Ho Chi Minh, VN . Reviewed by 56 customers rated: 4 / 5