Passing arraylist of objects from jsp to action class in struts 1.3

Passing ArrayList of objects from action class to Jsp is easy. Array List of Objects passed to the JSP can be displayed using struts iterate tag. Now these values may be changed in the JSP . Now the question is how to get the updated list of Objects in Action class OR How to pass the ArrayList of Objects with updated values to Action . Developers may struggle to get solution to the above question . One way to the changed values is to use request.getParameterNames / request.getParameterMap() . But this is not the right solution because we are not getting the updated values as ArrayList. Another solution may be using session object to store the ArrayList. Now third and best solution is as follows.

To explain the concept , I am creating Employee class which has the fields empCode , empName ,dob, email and one ArrayList attribute (empDetails in our example) is created in EmpForm ( ActionForm). Now one or more Employee objects are created and stored in a ArrayList and final arraylist are set in the empDetails (arrayList) attribute of EmpForm. Initially loadEmpDetails method of the action class (EmpAction) is called . Then loadEmpDetails method of employee data access object (EmpDAO) is called which is used to load the employee details from database and Employee object is created and stored in the ArrayList. Now the ArrayList has one or more employee objects . This ArrayList resides in the Form is passed to JSP (using empForm.setEmpDetails(empList) ) and updated with new values.

ActionForm (EmpForm) Code:

 

Employee (JavaBean) class :

Action Class (EmpAction) loadEmpDetails method Code

Code for empDao.loadEmpDetails() (Partly)

View (empDetails.jsp) code : (Part of the code which is to display & change arrayList of Employee Objects ) . Here we have to use logic:iterate tag with Index

When you submit the form (JSP) , the ArrayList with updated Employee objects has to be passed to Action class. In the above JSP code employee represents the Employee class (JavaBean) inside the arrayList which has the attributes like empCode , empName, dob, email to be displayed in the jsp. To retain values , in Action Form (EmpForm) , you have to define a getter method as such.
Code in ActionForm (EmpForm) (Merge this code with Above ActionForm Code)

Finally processEmpetails method is executed when tou submit the jsp. In our example , we are just displaying the updated employee objects attributes . You can write code to update this values in the database.

Code of ProcessEmpDetails in the Action Class

When you run the above program , you will get the following screen loading the initial values from database.

Now you can edit any values like empName, dob , email , then submit. In our example dob & email is updated .

The changed values will be printed as follows.
[9/7/11 1:32:29:062 IST] 6b652665 SystemOut O Emp List size2
[9/7/11 1:32:29:062 IST] 6b652665 SystemOut O 1000 K.Paul 01/01/1967 test1@gmail.com
[9/7/11 1:32:29:062 IST] 6b652665 SystemOut O 1001 E.Loyds 01/01/1976 abc536@gmail.com

You may also like

Leave a Reply