1

I am facing a problem in Struts2, In my sample application I have Array of objects (Say person name) , I need to display these names as a editable text fields , I am using Iterators for this , I am successful in diaplying, But When I cange the same value and submit the form. I am getting the entire array which holds null value.

Say for Exaple in my form bean I have a property Name [] names;

In my JSP I have the iterator as

If there are 3 names then I can get these names on the UI if I can initialized them with some dummy value but when I edit and sumbit, then "names" array is not getting updated. Please help me in this regard

user1006080
  • 75
  • 1
  • 4
  • 11

2 Answers2

4

In Struts2, when you need to repopulate the list of bean items, you need to refer them through indices. Please refer the example below:

Bean class:

public class Person {

    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Action class:

public class PersonAction extends ActionSupport {
    private List<Person> persons;

    public List<Person> getPersons() {
        return persons;
    }

    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }

    //Initial Load method
    @Override
    public String execute() {
        persons = new ArrayList<Person>();

        int alpha = 65;
        for(int i = 0; i < 3 ; i++) {
            Person person = new Person();
            person.setId(i);
            person.setName(String.valueOf((char)alpha++));
            persons.add(person);
        }
        return SUCCESS;
    }

    //Function that handles the form submit
    public String updatePerson() {

        for(Person person : persons) {
            System.out.println(person.getId() + ":" + person.getName());
        }

        return SUCCESS;
    }
}

Page:

<s:form action="doUpdate">
         <s:iterator value="persons" status="stat" var="person">
              <s:textfield value="%{#person.name}" name="persons[%{#stat.count}].name"/><br/>
         </s:iterator>
         <s:submit value="Submit"/>
</s:form>

When you submit the above form, the url would look like doUpdate?persons[0].name=A1&persons[1].name=B1&persons[2].name=C1. Similarly if you need to update id of the first person object, you will append persons[0].id=3 to the url using form. In the <s:textfield value="%{#person.name}" name="persons[%{#stat.count}].name"/>, you tell that the predefined value is person's name, for each object. The name attribute is to set the rendered html input element; the name which will be referenced in the url when the form is submitted. You will get a clear idea if you look into the generated html.

James Jithin
  • 10,183
  • 5
  • 36
  • 51
  • thanks for your response. this one works for me. For my case, it is a little bit trickier. I just have another list in person object, it is called state. like below: – user1006080 Oct 25 '11 at 23:18
  • public class State { private int id; private String stateAbbr; ... setter, getter .. the Page below states in Person is null? something wrong with my Page? – user1006080 Oct 25 '11 at 23:42
  • See, when you submit the form, states[%{#stateStatus.count-1}].stateAbbr should match to some person. Otherwise, it is of no use. Hence, make this change: – James Jithin Oct 26 '11 at 04:36
  • I think the count should not be use in this place. If you want to use count, we should use like this. – Kyaw Min Thu L May 07 '16 at 20:46
0

Use the iterator tag of struts2, if list is not empty then use :

<s:iterator value="names" status="namesStatus">
    <s:property/><br/>
</s:iterator>
R B
  • 55
  • 8