Action中使用List对象。要在程序中添加一个.properties的配置文件,将List对象指向实体类
1.VisitorListAction.java
package action;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class VisitorListAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 7604066627566749728L;
private List<User> user;
public List<User> getUser() {
return user;
}
public void setUser(List<User> user) {
this.user = user;
}
@Override
public String execute() throws Exception {
System.out.println("asd");
System.out.println(user.size());
return SUCCESS;
}
}
2.User.java
package action;
public class User {
private String userName;
private int userAge;
private String userTelephone;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
public String getUserTelephone() {
return userTelephone;
}
public void setUserTelephone(String userTelephone) {
this.userTelephone = userTelephone;
}
}
3.User-visitorContext-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="userName">
<field-validator type="requiredstring">
<message>姓名不能为空</message>
</field-validator>
</field>
<field name="userAge">
<field-validator type="int">
<param name="min">10</param>
<param name="max">100</param>
<message>年龄为10-100之间!</message>
</field-validator>
</field>
</validators>
4.VisitorListAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<!-- 校验字段 -->
<field name="user">
<!-- 校验器 -->
<field-validator type="visitor">
<!-- 复合类型属性的上下文校验名称 -->
<param name="context">visitorContext</param>
<!-- 校验信息中输入前缀信息 -->
<param name="appendPrefix">true</param>
<message>联系人</message>
</field-validator>
</field>
</validators>
5.VisitorListAction-conversion.properties
Element_user=action.User
其中user表示所定义的List对象,action.User表示实体User类所在的路劲
6.visitorList.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>集合类型校验</title>
</head>
<STYLE type="text/css">
ul.regul,li.regli {
list-style: none;
float: left;
}
ul.regul {
width: 550px;
}
li.regli {
width: 100px;
}
</STYLE>
<body>
<jsp:include page="index.html"></jsp:include>
<ul class="regul">
<li class="regli">姓名</li>
<li class="regli">年龄</li>
<li class="regli">电话</li>
</ul>
<s:fielderror></s:fielderror>
<s:form action="visitorListAction" theme="simple">
<s:iterator value="new int[2]" status="st">
<ul class="regul">
<li class="regli"><s:textfield
name="%{'user['+#st.index+'].userName'}" cssStyle="width:100px;" />
</li>
<li class="regli"><s:textfield
name="%{'user['+#st.index+'].userAge'}" cssStyle="width:100px;" />
</li>
<li class="regli"><s:textfield
name="%{'user['+#st.index+'].userTelephone'}"
cssStyle="width:100px;" /></li>
</ul>
</s:iterator>
<br>
<ul>
<li style="text-align: left;"><s:submit value="提交"></s:submit></li>
</ul>
</s:form>
</body>
</html>