I am debugging my test program.
When my test page http://localhost:8080/Struts2Example/updatePerson
appeared in the browser, it displayed
A
B
C
in its own textfield and a Submit button, which is expected.

However, after I edited the textfields and pressed the Submit button.
The following appeared in the browser.
Name :A
Name :Aa
Name :Bb
Name :Cc

I do not understand why an extra row (Name :A) appeared in the top with the
rest of the names which I just entered.

The following source codes is my test program.
updatePerson.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd";>
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Update Person</title>
    </head>
    <body>
      <s:form action="changePerson">
         <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>
    </body>
</html>
PersonAction


updatePersonResult.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd";>
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Update Person Result</title>
    </head>
    <body>
         <s:iterator value="persons" status="stat" var="person">
            <b>Name :</b><s:property value="%{#person.name}" /><br>
         </s:iterator>
    </body>
</html>


Person.java:
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;
    }
}



PersonAction.java:
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

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

    public PersonAction () {
        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);
        }
    }

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

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

    //Initial Load method
    @Override
    public String execute() {
        return SUCCESS;
    }

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

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

        return SUCCESS;
    }
}




struts.xml:
<action name="updatePerson" class="com.common.action.PersonAction">
  <result>pages/updatePerson.jsp</result>
</action>

<action name="changePerson" class="com.common.action.PersonAction">
  <result name="success">pages/updatePersonResult.jsp</result>
</action>

Reply via email to