Struts2中支持使用List在頁(yè)面和Act
public class Person {
int id;
String name;
int age;
float height;
}
這是一個(gè)POJO,getter和setting省略了。
act
public class MyAction {
public List getPeopleList() { … }
public void setPeopleList( List peopleList ) { … }
…
}
在我們使用Person類(lèi)之前,需要添加一個(gè)配置文件,MyAction-conversion.properties,把這個(gè)文件和MyAction放在一起。
這個(gè)文件里只有一行內容:
Element_peopleList=Person
前綴Element_是一個(gè)常量,表明等號左邊的表達式中跟在這個(gè)常量后面的是Act
等號右邊的表達式是全類(lèi)名(包含package)
下面是一個(gè)頁(yè)面的代碼片段:
<s:form act
<s:iterator value="peopleList" status="stat">
<s:hidden
name="peopleList[%{#stat.index}].id"
value="%{peopleList[#stat.index].id}"/>
<s:textfield label="Name"
name="peopleList[%{#stat.index}].name"
value="%{peopleList[#stat.index].name}"/>
<s:textfield label="Age"
name="peopleList[%{#stat.index}].age"
value="%{peopleList[#stat.index].age}" />
<s:textfield label="Height"
name="peopleList[%{#stat.index}].height"
value="%{peopleList[#stat.index].height}"/>
<br/>
s:iterator>
<s:submit value="Update"/>
s:form>
使用這段代碼,Struts2會(huì )創(chuàng )建一個(gè)Person類(lèi)的ArrayList,并且用setPersonList這個(gè)方法把頁(yè)面表格中的值傳遞回Act
如果你是想從用戶(hù)界面中動(dòng)態(tài)創(chuàng )建列表值,需要允許Struts2給列表中類(lèi)的實(shí)例。那么在配置文件MyAction-conversion.properties中添加一行:
CreateIfNull_peopleList = true
聯(lián)系客服