日期:2014-05-17  浏览次数:20725 次

struts2 能否从html标签获取值, struts2的list属性如何从表单获得的值
问题1,struts2 能否从html标签获取值,如:<input type="text" name="testperson.personNo"/> 结论:可以
问题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>