Pages Navigation Menu

Coding is much easier than you think

How to use a JavaBean class as a property in struts 2 action class.

How to use a JavaBean class as a property in struts 2 action class.

 
In this tutorial we will learn how to use a JavaBean class as a property in struts 2 action.
 
Consider a scenario, in which you have written an action class, which in turn has a JavaBean class as its property as shown below.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 

Action class

 
HelloAction.java
 

package com.simplecode.action;

import com.opensymphony.xwork2.Action;
import com.simplecode.bo.User;

public class HelloAction implements Action
{
	private User user;
	
	public User getUser() 
	{
		return user;
	}

	public void setUser(User user) 
	{
		this.user = user;
	}

	public String execute() 
	{
		if (getUser().getUserName().isEmpty()) 
		     return ERROR;
		else 
		     return SUCCESS;
	}
}

 
Transferring the JavaBeans property values to Action class
 

In Struts 2 transferring of data to the Java bean object is automatically done by the params interceptor, all we have to do is to create a JavaBeans object and its corresponding getter and setter methods in the action class.
 
Retrieving the JavaBeans property from action class to Jsp
 
To refer the attributes of User class, we need to first get the User object and then access its properties. For example to access the User’s, userName attribute in the Action class, the following syntax is used.
 

getUser().getUserName();

 

JavaBean class

 
User.java
 

package com.simplecode.bo;

public class User
{
	private String userName;

	public String getUserName() 
	{
		return userName;
	}

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

 

JSP

 
In jsp page the user attributes cannot be directly referenced. Since the attributes we refer in the jsp page belongs to the User object (JavaBean object) we need to go one level deeper to reference the attributes. To refer the userName attribute of User class, the value of the name attribute in the s:property tag should be
 

"user.userName"

 
login.jsp
 

<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Login</title>
</head>
<body>
	<h3>Struts 2 JavaBeans as property</h3>

	<s:form action="hello">
		<s:textfield name="user.userName"  label="User name"/>
		<s:submit name="submit" />
	</s:form>
</body>
</html>

 
success.jsp
 

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome Page</title>
</head>

<body>
	Welcome, <s:property value="user.userName" />
</body>
</html>

 
error.jsp
 

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Error Page</title>
</head>
<body>
Login failed! Please enter a valid user name
</body>
</html>

 

Demo

 

http://localhost:8089/JavaBean/

login
 
On entering the details and clicking the Submit button the following page will be dispalyed.
 
javabean op
 
In struts 2 there is a concept call Model objects, using which we can refer to the JavaBean objects attributes directly, instead of doing a deeper reference as shown in this example above. In our next article we will learn about struts 2 Model Objects which simplifies the amount of code written above via ModelDriven interface
 

dwd2
Zip file – JavaBean.zip

 

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.