validate验证索引属性

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方法,而不管你有没有定义一个私有变量属性.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值