Thursday, March 31, 2011

Post List of objects from jsp to Struts action

How to pass objects list from jsp to Struts?
How to post objects list from jsp to Struts?
Struts 2 framework have provided very powerful features using OGNL to post list of Objects from jsp to Struts 2 Action class.

Here is the example of passing list of objects from jsp to Struts.

We must have Struts 2 framework with OGNL script.
Following example will allows you to add multiple user list at the same time from jsp.
Code of required POJO, Acion Class and Jsp to post list of Person Object from jsp to action class are written below.

Person.Java Pojo
public class Person 
{    
    int person_id;
    String name;
    String address;
    public int getPerson_id() {
        return person_id;
    }
    public void setPerson_id(int personId) {
        person_id = personId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}
Struts 2 Action Class
@ParentPackage("default")
public class ManageUsers extends ActionSupport  
{
    List<Person> personList ;
    @Action(value="addUserList",results={
            @Result(name=ActionSupport.SUCCESS,location="addPersonList",type="tiles")})
    public String execute() throws Exception
    {      
        if(personList==null)
        {         
            personList = new ArrayList<Person>();
        }      
        Person p = new Person();
        p.setName("NAME");
        personList.add(p);      
        return SUCCESS;
    }
    public List<Person> getPersonList() {
        return personList;
    }
    public void setPersonList(List<Person> personList) {
        this.personList = personList;
    }
}
JSP
<s:if test="personList != null && personList.size > 0">
        <tr>
            <th width="50%">Person Name</th>
            <th width="50%">Address</th>
        </tr>
    <s:iterator value="personList"  status="p">
        <tr>
            <td><s:textfield name="personList[%{#p.index}].name" /></td>
             <td><s:textfield name="personList[%{#p.index}].address" /></td>
        </tr>
    </s:iterator>
    </s:if>




7 comments:

  1. Hi,
    I want to change the name of checkbox dynamically in jsp.and then the values of this checkbox should be accessed in struts2.0 action file
    How can I??
    Waiting for reply...

    ReplyDelete
  2. I think, in jsp you can have all the checkboxes with same name and in action class you can have a variable in String[] array. In I have not understood you question please explain in detail.

    ReplyDelete
  3. Hi, I have a list populated on JSP. I need to modify this list and send it to an other action(not the action from where the list is being populated from).

    This is what I have in JSP:








    I have to send these to an other action. Can you please help

    ReplyDelete
  4. Hi Anand, implementation logic is already written in above example. However, repeat following step for your solution.
    Suppose you want to maintain list of Person from Action1-jsp-Action2. where you want to modify value of person in jsp as well.

    Step 1: Action1 must have instance variable list of person like eg: List personList. Implement setter and getter of personList variable.
    Step 2: Display data in jsp after forwarded by Action2 as below.
    ---
    See jsp code example in this post above in JSP section
    If you want to use same above example, You must have Person pojo with name and address variable with its getter and setters.
    ---
    Step 4: Place a button in jsp that submit the form to Action2(not same action).
    Step 5: Define same variable of person list in Action2 like List personList. Implement setter & getting of personList variable.

    ---DONE---
    Now value of name or address change in jsp must be available variable(personList) in Action2.


    Thanks
    Biren

    ReplyDelete
  5. I want to ask one question. I have two kind of address in my jsp where there is a class for both. I have to identifying which address for this fields. SO I have to define in jsp with like corAddress.AddressLine1 for correspondence Address and perAddress.AddressLine1 for Permenant Address. I create two object of type Address in AddressAction. And then I have called save method twice for this different address.

    Its completely working fine.

    But is it another better way for above situation.???

    and ya.. How to pass list of attributes from jsp to Action that i am not understand.. Can you exaplin logical way??

    ReplyDelete
  6. Hi Pradip, answering to your first question, it seems like OK. But its unclear that what's the table structure of those address? if you are using same table then you must have to call save twice to add two different address in two rows. However, if you have one table and there is separate column for permanent and corresponding address, then you might have to save once only.

    Regarding to your second question,
    Suppose you want to update name of many students from single jsp in one screen(just clicking save button only one time), in this case above solution is useful.
    Like, you have to display student names in table with editable option in jsp,here you don't know how many student so you cant create specific number of student's object is action.
    To solve this scenario, List of student variable should be defined in action which should be mapped with jsp as explain in above code example. Above code is properly working, you can test it.
    Thanks

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete