struts2 能否從html標籤獲取值, struts2的list屬性如何從表單獲得的值

問題1,struts2 能否從html標籤獲取值,如: 結論:可以
問題2, struts2的list屬性如何從表單獲得的值, 即input的name屬性寫法.
<input type="text" name="people[0].name" /><input type="text" name="people[0].address" /><input type="text" name="people[1].name" /><input type="text" name="people[1].address" /> 結論:可以,如people
<input type="text" name="tList[0]" /><input type="text" name="tList[1]" /> 結論:不可以,如tList


代碼:
1. ListPropertyTestAction.java

package com.s2.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class ListPropertyTestAction extends ActionSupport {

private List<Person> people;//拿到
private List<String> tList;//拿不到
private String zhu;//拿到
public String getZhu() {
return zhu;
}
public void setZhu(String zhu) {
this.zhu = zhu;
}
public List<Person> getPeople() {
return people;
}
public void setPeople(List<Person> people) {
this.people = people;
}
public List<String> getTList() {
return tList;
}
public void setTList(List<String> list) {
tList = list;
}
@Override
public String execute() {
return SUCCESS;
}

}

2 Person.java
package com.s2.action;

public class Person {
private String name;

private String address;

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

3. 輸入頁面

<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<div style="color:red">
<s:fielderror />
</div>
<s:form action="ListPropertyTestAction" theme="simple">
<table>
<tr style="background-color:powderblue; font-weight:bold;">
<td>
name
</td>
<td>
address
</td>
</tr>
<tr>
<td>
<input type="text" name="people[0].name" />
</td>
<td>
<input type="text" name="people[0].address" />
</td>
</tr>
<tr>
<td>
<input type="text" name="people[1].name" />
</td>
<td>
<input type="text" name="people[1].address" />
</td>
</tr>
<tr>
<td>
<input type="text" name="tList[0]" />
</td>
<td>
<input type="text" name="tList[1]" />
</td>
</tr>
<tr>
<td>
<input type="text" name="zhu" />
</td>
<td>
<s:submit />
</td>
</tr>
</table>
</s:form>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章