Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In Struts2, when you need to repopulate the list of bean items, you need to refer them through indices. Please refer the example below:</p> <p>Bean class:</p> <pre><code>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; } } </code></pre> <p>Action class:</p> <pre><code>public class PersonAction extends ActionSupport { private List&lt;Person&gt; persons; public List&lt;Person&gt; getPersons() { return persons; } public void setPersons(List&lt;Person&gt; persons) { this.persons = persons; } //Initial Load method @Override public String execute() { persons = new ArrayList&lt;Person&gt;(); int alpha = 65; for(int i = 0; i &lt; 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; } } </code></pre> <p>Page:</p> <pre><code>&lt;s:form action="doUpdate"&gt; &lt;s:iterator value="persons" status="stat" var="person"&gt; &lt;s:textfield value="%{#person.name}" name="persons[%{#stat.count}].name"/&gt;&lt;br/&gt; &lt;/s:iterator&gt; &lt;s:submit value="Submit"/&gt; &lt;/s:form&gt; </code></pre> <p>When you submit the above form, the url would look like <code>doUpdate?persons[0].name=A1&amp;persons[1].name=B1&amp;persons[2].name=C1</code>. Similarly if you need to update id of the first person object, you will append <code>persons[0].id=3</code> to the url using form. In the <code>&lt;s:textfield value="%{#person.name}" name="persons[%{#stat.count}].name"/&gt;</code>, you tell that the predefined value is person's name, for each object. The <code>name</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload