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.
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.
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;
}
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);
}
}
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.
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.