Validator框架

参照:[url]http://www.blogjava.net/nokiaguy/archive/2009/02/12/254421.html[/url]
【第1步】建立ValidatorExamForm类


package com.yourcompany.struts.form;

import org.apache.struts.action.ActionForm;
import org.apache.struts.validator.ValidatorForm;

/**
* MyEclipse Struts
* Creation date: 04-19-2010
*
* XDoclet definition:
* @struts.form name="validatorExamForm"
*/
public class ValidatorExamForm extends ValidatorForm {//必须从ValidatorForm继承
/*
* Generated fields
*/

/** email property */
private String email;

/** age property */
private String age;

/** name property */
private String name;

/*
* Generated Methods
*/

/**
* Returns the email.
* @return String
*/
public String getEmail() {
return email;
}

/**
* Set the email.
* @param email The email to set
*/
public void setEmail(String email) {
this.email = email;
}

/**
* Returns the age.
* @return String
*/
public String getAge() {
return age;
}

/**
* Set the age.
* @param age The age to set
*/
public void setAge(String age) {
this.age = age;
}

/**
* Returns the name.
* @return String
*/
public String getName() {
return name;
}

/**
* Set the name.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}
}


【第2步】建立ValidatorExamAction类

* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yourcompany.struts.form.ValidatorExamForm;

/**
* MyEclipse Struts
* Creation date: 04-19-2010
*
* XDoclet definition:
* @struts.action path="/validatorExam" name="validatorExamForm" input="/form/validatorExam.jsp" scope="request" validate="true"
*/
public class ValidatorExamAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ValidatorExamForm validatorExamForm = (ValidatorExamForm) form;// TODO Auto-generated method stub
return null;
}
}


【第3步】配置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 >
<data-source key="datasource" type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="password" value="" />
<set-property property="minCount" value="1" />
<set-property property="maxCount" value="10" />
<set-property property="username" value="root" />
<set-property property="driverClassName" value="org.gjt.mm.mysql.Driver" />
<set-property property="description" value="for struts attack aolution" />
<set-property property="url" value="jdbc:mysql://localhost:3306/cytest" />
<set-property property="readOnly" value="false" />
<set-property property="autoCommit" value="true" />
</data-source>
</data-sources>

<form-beans >
<form-bean name="AttackSolutionForm" type="com.yourcompany.struts.form.AttackSolutionForm" />
<form-bean name="showExceptionForm" type="com.yourcompany.struts.form.ShowExceptionForm" />
[b]<form-bean name="validatorExamForm" type="com.yourcompany.struts.form.ValidatorExamForm" /> </form-beans>[/b]

<action-mappings >
<action
attribute="AttackSolutionForm"
input="/form/attackSolution.jsp"
name="AttackSolutionForm"
path="/attackSolution"
scope="request"
type="com.yourcompany.struts.action.AttackSolutionAction">
<set-property property="cancellable" value="true" />
<forward name="success" path="/form/attackSolution.jsp" />
</action>
<action
attribute="showExceptionForm"
input="/form/showException.jsp"
name="showExceptionForm"
path="/showException"
scope="request"
type="com.yourcompany.struts.action.ShowExceptionAction">
<set-property property="cancellable" value="true" />
<exception
key="usernameException"
path="/form/showException.jsp"
type="com.yourcompany.exception.UserNameException" />
</action>
[b]<action
attribute="validatorExamForm"
input="/form/validatorExam.jsp"
name="validatorExamForm"
path="/validatorExam"
scope="request"
type="com.yourcompany.struts.action.ValidatorExamAction">
<set-property property="cancellable" value="true" />
</action>[/b]
</action-mappings>

<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
[b]<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validator.xml" /></plug-in>[/b]

</struts-config>



【第4步】建立validatorExam.jsp


<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<html>
<head>
<title>JSP for ValidatorExamForm form</title>
</head>
<body>
<html:form action="/validatorExam">
email : <html:text property="email"/><html:errors property="email"/><br/>
age : <html:text property="age"/><html:errors property="age"/><br/>
name : <html:text property="name"/><html:errors property="name"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>


【第5步】配置validator.xml文件


<?xml version="1.0" encoding="GBK" ?>
<!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="validatorExamForm">
<field property="name" depends="required,maxlength,minlength,mask">
<msg name="required" key="error.name.blank" />
<msg name="minlength" key="error.name.minLength" />
<msg name="maxlength" key="error.name.maxLength" />
<msg name="mask" key="error.name.alphanum" />

<arg name="minlength" key="${var:minlength}" position="0" resource="false" />
<arg name="maxlength" key="${var:maxlength}" position="0" resource="false" />
<var>
<var-name>minlength</var-name>
<var-value>5</var-value>
</var>
<var>
<var-name>maxlength</var-name>
<var-value>10</var-value>
</var>
<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z0-9]*$</var-value>
</var>
</field>
<field property="age" depends="required,integer,intRange">
<msg name="required" key="error.age.blank" />
<msg name="integer" key="error.age.integer" />
<msg name="intRange" key="error.age.intRange" />

<arg name="intRange" key="${var:min}" position="0" resource="false" />
<arg name="intRange" key="${var:max}" position="1" resource="false" />
<var>
<var-name>min</var-name>
<var-value>18</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>60</var-value>
</var>
</field>
<field property="email" depends="required,email">
<msg name="required" key="error.email.blank" />
<msg name="email" key="error.email.invalid" />
</field>
</form>
</formset>
</form-validation>


【第6步】在ApplicationResources.properties文件中添加错误信息
[table]
|error.name.blank = 姓名不能为空
|
| error.name.minLength = 姓名的长度不能小于{0}
|
| error.name.maxLength = 姓名的长度不能大于{0}
|
| error.name.alphanum = 姓名必须由字母和数字组成
|
| error.age.blank = 年龄不能为空
|
| error.age.integer = 年龄必须为数字
|
| error.age.intRange = 年龄必须在{0}和{1}之间|
[/table]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值