【Webwork】Webwork验证框架(含自定义验证规则)

Action
package ch13.example1;

import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.Validateable;

public class Test extends ActionSupport{

private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute() throws Exception {
return SUCCESS;
}
}

 

JavaBean
package ch13.example1;

public class User {
private double salary;
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}




}

 

JSP

 

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding
="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="webwork" prefix="ww"%>
<%@ page import="java.util.*" %>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<ww:form action="testValidate">

<ww:textfield label="salary" name="user.salary"/><br>
<ww:textfield label="name" name="user.name"/><br>
<ww:textfield label="age" name="user.age"/><br>
<ww:submit value="update user"/>
</ww:form>

</body>
</html>

 


 

准备工作已经做好了,下面我们要做的就是对name字段进行验证,

验证分两种情况
(1)不能为空,使用webwork预置验证规则
(2)输入内容必须为"gaoxiang" 这部分使用自定义规则

首先,在Test Action包内建立验证规则,命名为Test-validation.xml

其中resquiredstring规则为webwork预置,另一个是自定义规则

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//openSymphony Group//XWork Validator 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"
>
<validators>
<field name="user.name">


<field-validator type="requiredstring">
<message>name required</message>
</field-validator>


<field-validator type="myValidator">
<message>name not equal gaoxiang</message>
</field-validator>

</field>
</validators>

 

在classpath下建立validation.xml

需要说明的是,webwork的jar包中已经包含了validation.xml(2.2.4版本中在xwork.jar中,叫default,xml)

我们并不需要额外建立validation.xml就可以使用这些规则,但我们自定义的规则必须建立validation.xml并在其中

定义,但如果classpath下有了我们自己建立的validation.xml,webwor预置的那些规则就失效了,所以,我们要把webowork预置的那些规则复制到我们建立的validation,xml中,代码如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<validators>
<validator name="myValidator" class="ch13.example1.myValidator"/>
<validator name="required" class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator" />
<validator name="requiredstring" class="com.opensymphony.xwork.validator.validators.RequiredStringValidator" />
<validator name="int" class="com.opensymphony.xwork.validator.validators.IntRangeFieldValidator" />
<validator name="double" class="com.opensymphony.xwork.validator.validators.DoubleRangeFieldValidator" />
<validator name="date" class="com.opensymphony.xwork.validator.validators.DateRangeFieldValidator" />
<validator name="expression" class="com.opensymphony.xwork.validator.validators.ExpressionValidator" />
<validator name="fieldexpression" class="com.opensymphony.xwork.validator.validators.FieldExpressionValidator" />
<validator name="email" class="com.opensymphony.xwork.validator.validators.EmailValidator" />
<validator name="url" class="com.opensymphony.xwork.validator.validators.URLValidator" />
<validator name="visitor" class="com.opensymphony.xwork.validator.validators.VisitorFieldValidator" />
<validator name="conversion" class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator" />
<validator name="stringlength" class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator" />
<validator name="regex" class="com.opensymphony.xwork.validator.validators.RegexFieldValidator" />
</validators>

 

验证类ch13.exmaple1.myValidator

继承FiledValidatorSupport并覆盖validate方法即可,出现错误用addFieldError设置相关错误信息,如果不进行addFieldError,则验证通过

 

package ch13.example1;

import com.opensymphony.xwork.validator.ValidationException;
import com.opensymphony.xwork.validator.validators.FieldValidatorSupport;

public class myValidator extends FieldValidatorSupport {

public void validate(Object obj) throws ValidationException {
String name
=getFieldName();
String value
=(String)getFieldValue(name,obj);
if(!value.equals("gaoxiang")&&!value.equals("")){
addFieldError(name,obj);
}

}

}

 

运行页面,首先不输入name,系统会提示必须输入name required,输入非"gaoxiang",系统会提示name not equal gaoxiang

 

 

 

 

 

 

 

posted @ 2010-06-03 08:58  Me疯子_(~  阅读(165)  评论(0编辑  收藏  举报