Pages Navigation Menu

Coding is much easier than you think

Dynamically add, remove list of objects from jsp Using Struts2

 
In this article we shall learn on how to dynamically add, remove list of objects from jsp Using Struts2.
 
File : User.java (Model Class)
 

package com.simplecode.action;

import java.util.Collection;

public class User
{
	int regNo;
	String name;
	Collection
addresses; public Collection
getAddresses() { return addresses; } public void setAddresses(Collection
addresses) { this.addresses = addresses; } public int getRegNo() { return regNo; } public void setRegNo(int regNo) { this.regNo = regNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

 
File : Bean Class
 

package com.simplecode.action;

public class Address
{
	private int houseNo;
	private String street;
	private String city;
	private String country;

	public int getHouseNo()
	{
		return houseNo;
	}

	public String getStreet()
	{
		return street;
	}

	public void setHouseNo(int houseNo)
	{
		this.houseNo = houseNo;
	}

	public void setStreet(String street)
	{
		this.street = street;
	}

	public String getCountry()
	{
		return country;
	}

	public void setCountry(String country)
	{
		this.country = country;
	}

	public String getCity()
	{
		return city;
	}

	public void setCity(String city)
	{
		this.city = city;
	}
}

 
In order to implement the above functionality via ajax, do read the article on CRUD Operations in Struts 2 using jTable jQuery plugin via Ajax
 
Do read :

 File : UserAction (Action Class)
 

package com.simplecode.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction implements Action, ModelDriven
{
	User user = new User();
	
	public User getUser()
	{
		return user;
	}
	
	public void setUser(User user)
	{
		this.user = user;
	}
	
	public String execute()
	{
		return SUCCESS;
	}
	
	public User getModel()
	{
		return user;
	}
}

 
In the above article I have used the concept of ModelDriven interface in struts2, if you are not aware of what model driven interface is, then please read the article on modeldriven interface here
 

Jsp Pages

 
File : index.jsp



Add/Remove dynamic rows




Reg No      :
Name      :

Addresses
House NoStreetCityCountry

 
File : result.jsp

<%@taglib prefix="s" uri="/struts-tags"%>



User Details


	

User information

ID :
Name :
Addresses :
Reg No :Street :City :Country :

 
Recommended reading :

 

Struts.xml

 





    
        
            result.jsp
        
    

 

Run it

 
On running the application
 
Collection values
 
Input
 
Input collection
 
Output
 
Collection output
 
** UPDATE: Struts 2 Complete tutorial now available here.
 

29 Comments

  1. Any thought how to do with database connection, jdbc ?

  2. or may i only get part of auto complete multiple field in jsp as in invoice system . i really need it , this is my interface, but i just want to know the process of auto complete based on values that already registered in my DB,

  3. excuse me, may I ask something, i already got the source code from gmail, but then, can I convert this code into jsp, or you have another source code abt invoice in jsp files , please help me :(

  4. can u include the database inserstion of this form???
    plsese help its urgent .i stuck on this

  5. i want add button along with each row.when i click button in row add one row below.
    how can do this ? help me

  6. how to write code for update and delete opeation in struts 2 …. thank u sir

  7. please also provide dynamically delete row

  8. i have list of objects such as id,name and salary and this list is appended to a combo box which is returned from a action, when i select a option name from the combo i should get salary of particular value. Can i do this using struts

  9. great work thank you sir for the help

  10. Jamil,
    I am working on struts2 application. From a jsp seereports.jsp that contains a form. On submit action is called and i want to return on same jsp and action returns a list and i want to iterate the list on the same jsp. Is that possible without making ajax call. Its returning to seereports.jsp but no data is displayed even after writing the iterate code. Please help.

  11. Hi Jamil,
    I have a requirement to create a user menu in tree structure (say root, level1, level2, level3) on left panel of browser and their content should be displayed on right upon clicking them.
    Now under the level2 node, I need to create sub-menus by taking sub-menu name as input from the user. Can you please guide me to achieve this functionality ?

  12. hi,
    Can you plz upload the war or zip file as i m getting an error or mail me …thanks in advance :)

  13. Hi jamil,
    In my jsp page is not working for dynamic generated rows..And not adding values from dynamic rows to multiple rows into table..help me

  14. i am facing an error
    Exception starting filter struts2
    java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class com.simplecode.action.UserAction)

    • i am using jdk1.7 and its compiling with out any problem while starting the server i got the error

    • I HAVE SOLVED THE PROBLEM BRO..THANKS

  15. Hi ,
    How to write code for ModelDriven and Action

  16. How would you also dynamically add the name of the textbox over? Like along side the textbox or above it?

  17. hi, I am also working on dynamically adding table row using jsp, but i am getting one exception in that : whenever in a table there is only one row and i submit, it trough’s an error , but works fine when i add row that means when there’s total 2 row in a table.
    can anybody help me resolve this issue …

  18. First Thank you…its working for me…Now I want to iterate the collection on server side. I already did a bit, but the problem is when i delete a element the collection display before to the elements i delete.. please help me up..

  19. Hi,
    I had the same need (add a row in the jsp and after submitting, add the element to a list), I thought of the same solution (renaming the fields added), but I got a nullpointerexception when I try to dispay the elements of my collection… For example, in your case, you would have a nullpointerexception in the following line:
    for(Adresse a: user.getAdresses()) System.out.println(a.getStreet());…

    Any idea about what I’m doing wrong please?

    Thanks

    • Hi Yousfi,

      Code works fine.. the thing if you have used any string inside list element, and if you have not not entered any values in jsp, then it will obviously leads to NullPointer exception, you just need to handle the same in java side.

  20. Muchas Gracias! :)

    • Spanish Translated – Thank you very much :)

      – You are welcome :)