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"%>
http://www.w3.org/TR/html4/loose.dtd";>
<%@taglib uri="/struts-tags" prefix="s" %>
Update Person
PersonAction
updatePersonResult.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
http://www.w3.org/TR/html4/loose.dtd";>
<%@taglib uri="/struts-tags" prefix="s" %>
Update Person Result
Name :
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 persons;
public PersonAction () {
persons = new ArrayList();
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 getPersons() {
return persons;
}
public void setPersons(List 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:
pages/updatePerson.jsp
pages/updatePersonResult.jsp