struts-configz 中的FormBean和Actionmapping配置如下:
<form-bean name="testValidatorForm" type="com.xj.struts.form.TestValidatorForm" />
<action
attribute="testValidatorForm"
input="/form/testValidator.jsp"
name="testValidatorForm"
path="/testValidator"
scope="request"
type="com.xj.struts.action.TestValidatorAction" >
<forward name="success" path="/form/validateSuccess.jsp" />
</action>
TestValidatorAction中这样写:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
TestValidatorForm testValidatorForm = (TestValidatorForm) form;// TODO Auto-generated method stub
//testValidatorForm.getGroup();
request.setAttribute("testValidatorForm", testValidatorForm);
return mapping.findForward("success");
}
首先自定义一个AutoArrayList类,防止JSP页面中填写的索引越界:
public class AutoArrayList extends ArrayList
{
private Class itemClass;
public AutoArrayList(Class itemClass) {
super();
this.itemClass = itemClass;
}
public Object get(int index) {
// TODO Auto-generated method stub
try
{
while(index>=this.size())
{
this.add(itemClass.newInstance());
}
}
catch(Exception e)
{
e.printStackTrace();
}
return super.get(index);
}
}
定义一个UserGroup类,有一个groupName属性:
public class UserGroup {
private String groupName;
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
}
然后在TestValidatorForm formbean中加一个索引属性:
public class TestValidatorForm extends ValidatorForm {
private AutoArrayList myphones = new AutoArrayList(UserGroup.class);
public AutoArrayList getMyphones() {
return myphones;
}
public UserGroup getMyphone(int i)
{
return (UserGroup) myphones.get(i);
}
public void setMyphone(int i,UserGroup myphone)
{
myphones.add(myphone);
}
}
然后在输入testValidator.jsp页面中输入索引属性:
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<html>
<head>
<title>JSP for TestValidatorForm form</title>
</head>
<body>
<html:errors />
<html:form action="/testValidator">
Myphone:<br>
<html:text property="myphone[0].groupName" ></html:text>
<html:text property="myphone[1].groupName" ></html:text>
<html:submit />
<html:cancel />
</html:form>
</body>
</html>
然后新建一个validator.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "validator_1_1_3.dtd" >
<form-validation>
<formset>
<form name="testValidatorForm">
<field property="groupName" indexedListProperty="myphones" depends="required">
<msg name="required"
key="error.form.testValidator.phones.groupName.required" />
</field>
</form>
</formset>
</form-validation>
在资源文件中配置错误消息的提示KEY error.form.testValidator.phones.groupName.required:
error.form.testValidator.phones.groupName.required = every groupName is required
最后在struts-config中配置插件:
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validator.xml" />
</plug-in>
然后验证成功后跳到validateSuccess.jsp把输入页面testValidator.jsp中输入的两个索引属性打印出来,这样写:
<%@ page language="java" pageEncoding="gbk"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>validateSuccess.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<logic:present name="testValidatorForm" >
<logic:notEmpty name="testValidatorForm" property="myphones" >
<logic:iterate id="list" name="testValidatorForm" property="myphones" indexId="idx" >
phone[<%= idx %>]: <bean:write name="list" property="groupName" />
</logic:iterate>
</logic:notEmpty>
</logic:present>
</body>
</html:html>
这个例子就完成了,其中有几个注意点:
1.输入页面testValidator.jsp中的索引索引输入框的写法
Myphone:<br>
<html:text property="myphone[0].groupName" ></html:text>
<html:text property="myphone[1].groupName" ></html:text>
myphone[0].groupName在解析的时候会调用FormBean中的
public UserGroup getMyphone(int i)
{
return (UserGroup) myphones.get(i);
}
getMyphone(0)方法,返回一个UserGroup实例,然后再userGroup.getGroupName,取userGroup实例中的groupName属性值.
提交的时候会调用
public void setMyphone(int i,UserGroup myphone)
{
myphones.add(myphone);
}将一个usergroup实例插入到myphones索引属性中去。
注意这里的get/set都是Myphone而不是Myphones为了更容易区分,你可以改成get/setTestphone,这样在testValidator.jsp中也要相应的改成testphone[0].groupName,而FormBean中并没有定义myphone属性.从而得出一个结论:struts标签在执行的时候将你写的property的值的首字母转成大写,然后去FormBean中查找有没有相印的get/set方法,而不管你有没有定义一个私有变量属性.