I am trying to update a List of Person Objects via jsp. I have one
Action Class used in the DispatchAction pattern.
I begin the exercise with method1 which creates a new peopleList and
populates each Person object in the list with a firstname.
*public*String method1() *throws*Exception {
System./out/.println("Just Called method1 method");
peopleList= *new*ArrayList<Person>();
*for*(*int*i = 0; i < 10; i++) {
Person thisOne = *new*Person();
thisOne.setFirstname("FirstName"+ i);
peopleList.add(thisOne);
}
*return*/SUCCESS/;
}
I direct to JSP1.jsp and it displays fine:
<%@page language=/"java"/ contentType=/"text/html; charset=UTF-8"/
pageEncoding=/"UTF-8"/%>
<%@taglib prefix=/"s"/ uri=/"/struts-tags"/%>
<html>
<head>
<title>JSP1</title>
</head>
<body>
<tableborder=/"1"/ "width=/"100%"/>
<tr>
<td>_firstname_</td>
<td>_lastname_</td>
<td>number</td>
</tr>
<tr>
<s:formaction=/"method2test"/>
<s:iteratorvalue=/"peopleList"/>
<tr>
<td><s:propertyvalue=/"firstname"/ /></td>
<td><s:propertyvalue=/"lastname"/ /></td>
<td><s:propertyvalue=/"number"/ /></td>
<s:hiddenvalue=/"dn"//>
</tr>
</s:iterator>
<s:submit/>
</s:form>
</tr>
</table>
</body>
</html>
I submit from this JSP1.jsp and call method2. Here peopleList is now
empty. _Why doesn't JSP2.jsp submit peopleList to method2?_
Below are some of the important code snippets I believe are needed to
help resolve my misunderstanding.
struts.xml below:
<?xmlversion=/"1.0"/ encoding=/"UTF-8"/ ?>
<!DOCTYPEstruts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constantname=/"struts.enable.DynamicMethodInvocation"/ value=/"false"/ />
<constantname=/"struts.devMode"/ value=/"false"/ />
<constantname=/"struts.ui.theme"/ value=/"simple"/ />
<packagename=/"default"/ namespace=/"/"/ extends=/"struts-default"/>
<default-action-refname=/"index"/ />
<global-results>
<resultname=/"error"/>/error._jsp_</result>
</global-results>
<global-exception-mappings>
<exception-mappingexception=/"java.lang.Exception"/ result=/"error"//>
</global-exception-mappings>
<actionname=/"index"/>
<resulttype=/"redirectAction"/>
<paramname=/"actionName"/>method1test</param>
<paramname=/"namespace"/>/example</param>
</result>
</action>
</package>
<includefile=/"example.xml"//>
<!-- Add packages here -->
</struts>
example.xml below:
<?xmlversion=/"1.0"/ encoding=/"UTF-8"/ ?>
<!DOCTYPEstruts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename=/"example"/ namespace=/"/"/ extends=/"default"/>
<actionname=/"method1test"/ class=/"example.Action1"/ method=/"method1"/>
<result>/example/JSP1._jsp_</result>
</action>
<actionname=/"method2test"/ class=/"example.Action1"/method=/"method2"/>
<resultname=/"input"/>/example/JSP1._jsp_</result>
<result>/example/JSP2._jsp_</result>
</action>
<actionname=/"method3test"/ class=/"example.Action1"/method=/"method3"/>
<resultname=/"input"/>/example/JSP2._jsp_</result>
<result>/example/JSP3._jsp_</result>
</action>
</package>
</struts>
Action1.java
*package*example;
*import*java.util.ArrayList;
*import*java.util.List;
*import*com.opensymphony.xwork2.ActionSupport;
*public**class*Action1 *extends*ActionSupport {
*private**static**final**long*/serialVersionUID/= -8528061245644461238L;
*private*List<Person> peopleList;
*public*List<Person> getPeopleList() {
*return*peopleList;
}
*public**void*setPeopleList(List<Person> peopleList) {
*this*.peopleList= peopleList;
}
*public*String method1() *throws*Exception {
System./out/.println("Just Called method1 method");
peopleList= *new*ArrayList<Person>();
*for*(*int*i = 0; i < 10; i++) {
Person thisOne = *new*Person();
thisOne.setFirstname("FirstName"+ i);
peopleList.add(thisOne);
}
*return*/SUCCESS/;
}
*public*String method2() *throws*Exception {
System./out/.println("Just Called method2 method");
System./out/.println("Is peopleList null?"+ peopleList== *null*);
*if*(peopleList!= *null*) {
List<Person> newPeopleList = *new*ArrayList<Person>();
*for*(*int*i = 0; peopleList!= *null*&& i < peopleList.size(); i++) {
Person thisOne = peopleList.get(i);
thisOne.setLastname("LastName"+ i);
newPeopleList.add(thisOne);
}
peopleList.clear();
peopleList.addAll(newPeopleList);
}
*return*/SUCCESS/;
}
}
Person.java:
*package*example;
*public**class*Person {
String firstname, lastname, number, dn;
*public*String getDn() {
*return*dn;
}
*public**void*setDn(String dn) {
*this*.dn= dn;
}
*public*String getFirstname() {
*return*firstname;
}
*public**void*setFirstname(String firstname) {
*this*.firstname= firstname;
}
*public*String getLastname() {
*return*lastname;
}
*public**void*setLastname(String lastname) {
*this*.lastname= lastname;
}
*public*String getNumber() {
*return*number;
}
*public**void*setNumber(String number) {
*this*.number= number;
}
}
JSP1.jsp and JSP2.jsp are identical except for
<s:formaction=/"method2test"/>, which simple calls method1test and
method2test respectively:
<%@page language=/"java"/ contentType=/"text/html; charset=UTF-8"/
pageEncoding=/"UTF-8"/%>
<%@taglib prefix=/"s"/ uri=/"/struts-tags"/%>
<html>
<head>
<title>JSP1</title>
</head>
<body>
<tableborder=/"1"/ "width=/"100%"/>
<tr>
<td>firstname</td>
<td>lastname</td>
<td>number</td>
</tr>
<tr>
<s:formaction=/"method2test"/>
<s:iteratorvalue=/"peopleList"/>
<tr>
<td><s:propertyvalue=/"firstname"/ /></td>
<td><s:propertyvalue=/"lastname"/ /></td>
<td><s:propertyvalue=/"number"/ /></td>
<s:hiddenvalue=/"dn"//>
</tr>
</s:iterator>
<s:submit/>
</s:form>
</tr>
</table>
</body>
</html>