论坛首页 Java企业应用论坛

struts_validation 框架使用

浏览 1625 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2008-07-18  

validation.xml 编写


<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

<form-validation>
	<formset>	
		<form name="loginForm">
		
			<field property="username" depends="required">
				<arg0 key="error.name" resource="true"/>				
			</field>
			
			<!-- 验证密码不能为空.必须是整数,最小长度为六位.最大8 -->
			<field property="pwd" depends="required,integer,minlength,maxlength">
				<arg0 key="error.pwd" resource="true"/>
				<arg1 key="error.pwd" resource="true"/>
				
				<arg2 key="error.pwd" name="minlength" resource="true"/>
				<arg3 key="${var:minlength}" name="minlength" resource="false"/>
				<var>
					<var-name>minlength</var-name>
					<var-value>6</var-value>
				</var>
				
				<!-- arg 这个参数可以重复使用在配置文件中 他会根据你传过去的内容显示不同的信息 -->
				<arg2 key="error.pwd" name="maxlength" resource="true"/>
				<arg3 key="${var:maxlength}" name="maxlength" resource="false"/>
				<var>
					<var-name>maxlength</var-name>
					<var-value>8</var-value>					
				</var>
				
			</field>			
		</form>
		
		<!-- 用户注册 -->
		<form name="/reg">
			<field property="username" depends="required">
				<arg0 key="error.name" resource="true"/>				
			</field>
			
			<!-- 验证密码不能为空.必须是整数,最小长度为六位.最大8 -->
			<field property="pwd" depends="required,integer,minlength,maxlength">
				<arg0 key="error.pwd" resource="true"/>
				<arg1 key="error.pwd" resource="true"/>
				
				<arg2 key="error.pwd" name="minlength" resource="true"/>
				<arg3 key="${var:minlength}" name="minlength" resource="false"/>
				<var>
					<var-name>minlength</var-name>
					<var-value>6</var-value>
				</var>
				
				<!-- arg 这个参数可以重复使用在配置文件中 他会根据你传过去的内容显示不同的信息 -->
				<arg2 key="error.pwd" name="maxlength" resource="true"/>
				<arg3 key="${var:maxlength}" name="maxlength" resource="false"/>
				<var>
					<var-name>maxlength</var-name>
					<var-value>8</var-value>					
				</var>				
			</field>
			
			<field property="email" depends="required,email">
			
				<arg0 key="error.email" resource="true"/>						
				<arg0 key="error.email" name="email" resource="true"/>
				
			</field>
		</form>
	</formset>
</form-validation>



struts_config.xml 编写

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans>
  	<!-- 用户登录使用DynaValidatorForm -->
  	<form-bean name="loginForm" type="org.apache.struts.validator.DynaValidatorForm">
  		<form-property name="username" type="java.lang.String"></form-property>
  		<form-property name="pwd" type="java.lang.String"></form-property>
  	</form-bean>
  	
  	<!-- 用户注册使用DynaValidatorActionForm -->
  	<form-bean name="regForm" type="org.apache.struts.validator.DynaValidatorActionForm">
  		<form-property name="username" type="java.lang.String"></form-property>
  		<form-property name="pwd" type="java.lang.String"></form-property>
  		<form-property name="email" type="java.lang.String"></form-property>  		
  	</form-bean>
  	
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings>
  
  <!-- 登录的Action配置 -->

  <action name="loginForm" path="/login" input="/login.jsp"
  	type="com.dd.struts.action.LoginAction" validate="true" />

  <!-- 注册的Action配置 -->
  <action name="regForm" path="/reg" input="/reg.jsp"
  	type="com.dd.struts.action.RegAction" validate="true"/>
  

  </action-mappings>
  
  
  <message-resources parameter="com.dd.struts.ApplicationResources" />
  
  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
  	<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
  </plug-in>
  
</struts-config>


论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics