About the Authors
Sharath is working as Software Engineer in Software Engineering and Technology Labs (SETLABs),the research wing of Infosys Technologies Ltd. Manoj and Vijayan both work as Software Engineers in Communication Service Providers and Delivery department of Infosys. They have been working with J2EE and their area of specialization includes Struts. (Images at bottom)
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.

Data Retention in JSPs using Struts

This article is intended for developers who have some working knowledge in the struts framework. The content of the whole article is purely based on what we have learned from our work experience.

Retention of Data in JSPs

Retaining data is one of the typical problems that a developer faces even after working in web applications for a considerable amount of time. Mostly we face such problem when some validation error happens through jsp, or user is submitting to the same jsp (or action path). The complexity lies in the fact that all of the fields that we need to retain are in the jsps, whether it is an array of html fields or just simple html fields.

We have identified the following ways by which we can retain data. Each method depends upon the object that we have to retain.

Case 1

a) Retaining simple object such as string into an actionform
Sample portion of code

<html:text name=”actionForm” property=”name” />
or
<input type=”text” name=”name” value=”<bean:write name=’actionForm’ property=’name’ />

b) Retaining data of a javabean residing in form

<html:text name=”actionForm” property=”beanObj.name” />
or
<input type=”text” name=”beanObj.name” value=”<bean:write name=’beanFromForm’ property=’name’ />” />

The beanObj is the JavaBeanClass in the form bean and the name is a property of the JavaBeanClass. “beanFromForm” is an instance of the JavaBeanClass in the form bean.(It can be obtained from a <bean:define> tag for the JavaBeanClass) In the form you will have a getter-setter as such

// if string object
Public void setName(String string){
	this.name=name;
}
Public String getName(){
	return name;
}
//if bean inside form
Public void setBeanObj(JavaBeanClass bean)
{
	this.beanObj=bean
}

Public JavaBeanClass getBeanObj()
{
	return beanObj;
}

Case 2

Retaining an array of Objects (lets take the case of arrayList)

//Iterating over the arraylist to display individual properties.

<logic:iterate id=”beanObj” name=”actionForm” property=”list” indexId=”index” >
<html:select name=”actionForm” property=”beanObj[<bean:write name=’index’ />].selectedValue“ >
<%for(int i=0; i < 10;i++) {%>
    <html:option value=”<%=String.valueOf(i)%>”><displayValue></html:option>
<%}%>
</html:select>
</logic:iterate>

Here list represents the arraylist object residing in form and we are iterating through it to show array of combo boxes in jsp. "beanObj" represents the JavaClassBean inside the arrayList which has the required attribute to be displayed in the combo.

In form it will be:

Public void setBeanObj(int index,JavaBeanClass bean)
{
	list.set(index,bean);
}

Public JavaBeanClass getBeanObj(int index)
{
	int listObjSize = list.size();
	// check if object exists at specified index
	if ((i + 1) > listObjSize)
	{
		//add objects
		for (int j = listObjSize; j < i + 1; j++)
		{
			JavaBeanClass beanObj =
				new JavaBeanClass ();
			list.add(j, beanObj);
		}
	}
	// get and return object at this index
	return (JavaBeanClass) list.get(i);
}
//Sample code snippet of JSP to help retain data after validation error in the //page
<logic:iterate id=”employeeObj” name=”EmployeeForm” property=”employeeList” indexed=”index” >
<tr>
 <td>
<input type=”text” name=”employeeObj[<bean:write name=’index’/>].empName” value=”<bean:write name=’employeeObj’ property=’empName’/>” />
</td>
<td>
<html:select name=’EmployeeForm’ property=’employeeObj[<%=index.intValue()%>].designation’ >
		<html:option value=”1”>Software Engineer</html:option>
		<html:option value=”2”>Programmer Analyst</html:option>
<html:option value=”3”>Project Leader</html:option>
<html:option value=”4”>Delivery Manager</html:option>
</html:select>
</td>
</tr>
</logic:iterate>
//Sample Code snippet of Action Form

Public EmployeeForm extends ActionForm
{
	Private ArrayList employeeList = new ArrayList();

	Public ArrayList getEmployeeList()
	{
		Return employeeList;
	}

 Public void setEmployeeList(ArrayList list)
{
	employeeList = list;
}

Public void setEmployeeObj(int index,Employee empObj)
{
	employeeList.set(index,empObj);
}

Public Employee getEmployeeObj(int i)
{
int listObjSize = employeeList.size();
	// check if object exists at specified index
	if ((i + 1) > listObjSize)
	{
		//add objects
		for (int j = listObjSize; j < i + 1; j++)
		{
			Employee beanObj =
				new Employee();
			employeeList.add(j, beanObj);
		}
	}
	// get and return object at this index
	return (Employee) employeeList.get(i);
}

}

Tips to Remember

  1. It will be easier if user populates the same object on submit that he has used to populate the jsp. In the given example we are doing the same. Jsp is populated from the arraylist and submitting it into same arrayList. Otherwise he needs to write the logic in jsp where he needs to compare both objects which will degrade the performance a little and too much of jsp code.
  2. You can use hidden parameter to get the value of labels that are not submitted. User has to take note of the security violation that input type hidden causes.

Conclusion

This article discusses about how to retain the values in jsp using Struts framework. We have given this a major priority because most of them face this problem during their development of web application. We will publish some more articles covering usage of other struts tags and libraries.

Bios

Sharath is currently working as a Software Engineer in Software Engineering and Technology Labs (SETLABs) the research wing of Infosys Technologies Ltd. My area of specialisation is in J2EE technology and proactively involved as a member of J2EE centre of excellence in the organisation. My current research areas include Performance Analysis of J2EE applications.

Both Manoj and Vijayan are working as Software Engineers in Communication Service Providers and Delivery department of Infosys Technologies Ltd. We have been working in J2EE technology and area of specialization includes Struts framework.