Struts2输入校验(XML方式)
Posted 临渊启明
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2输入校验(XML方式)相关的知识,希望对你有一定的参考价值。
主要介绍struts2的XML配置方式输入校验,结合实例进行说明。
代码结构:
关键代码:
RegistAction.javapackage com.alfred.regist.action; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class RegistAction extends ActionSupport { private static final long serialVersionUID = 1L; private String username; private String password; private String repassword; private Integer age; private Date birthday; private Date graduation; public String toRegist() { System.out.println("run toRegist"); return SUCCESS; } @Override public void validate() { System.out.println("run validate"); } public void validateToRegist() { if (this.hasFieldErrors()) { System.out.println("already has field error"); } System.out.println("run validateToRegist"); if (age != null && (age < 20 || age > 100)) { this.addFieldError("age", "(code)年龄错误"); } } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRepassword() { return repassword; } public void setRepassword(String repassword) { this.repassword = repassword; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Date getGraduation() { return graduation; } public void setGraduation(Date graduation) { this.graduation = graduation; } }
regist.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <!-- 校验规则: username 不为空 2~5位 password 不为空 3~6位 repassword 与password一致 age 介于20~100岁 birthday 不会空 大于2000-01-01年 graduation 不会空 大于2010-01-01年 --> <s:actionmessage cssStyle="color:red" /> <s:actionerror cssStyle="color:green;" /> <s:fielderror cssStyle="color:blue;" /> =============== <br /> <s:form action="regist"> <s:textfield label="username" id="username" name="username"></s:textfield> <s:password label="password" id="password" name="password"></s:password> <s:password label="repassword" id="repassword" name="repassword"></s:password> <s:textfield label="age" id="age" name="age"></s:textfield> <s:textfield label="birthday" id="birthday" name="birthday"></s:textfield> <s:textfield label="graduation" id="graduation" name="graduation"></s:textfield> <s:submit value="submit"></s:submit> </s:form> </body> </html>
struts.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" extends="struts-default"> <action name="regist" class="com.alfred.regist.action.RegistAction" method="toRegist"> <result name="success">WEB-INF/jsp/main.jsp</result> <result name="input">regist.jsp</result> </action> </package> </struts>
使用XML方式检验需创建一个XML配置文件,位置在对应的Action同级目录下,命名规则为:Action名-validatioin.xml。
校验文件可以有两种写法,一种是字段校验,一种是非字段校验。字段校验就是以字段为主,在对应字段配置下添加该字段的校验器,非字段校验就是先定义好校验器,在校验器下添加字段。如下:
RegistAction-validation.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <validators> <field name="username"> <field-validator type="requiredstring"> <message>(XML)username is not blank</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">2</param> <param name="maxLength">5</param> <message> (XML)username is ${minLength}~${maxLength} </message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <message>(XML)password is not blank</message> </field-validator> <field-validator type="stringlength"> <param name="minLength">3</param> <param name="maxLength">6</param> <message> (XML)password is ${minLength}~${maxLength} </message> </field-validator> </field> <field name="repassword"> <field-validator type="fieldexpression"> <param name="expression"> <![CDATA[(password==repassword)]]> </param> <message>(XML)two password should be the same!</message> </field-validator> </field> <field name="age"> <field-validator type="required"> <message>(XML)age is not blank</message> </field-validator> <field-validator type="int"> <param name="min">20</param> <param name="max">100</param> <message>(XML)age is ${min}~${max}</message> </field-validator> </field> <field name="birthday"> <field-validator type="required"> <message>(XML)birthday is not blank</message> </field-validator> <field-validator type="date"> <param name="min">2000-01-01</param> <message> (XML)birthday is date and greater than ${min} </message> </field-validator> </field> <field name="graduation"> <field-validator type="required"> <message>graduation is not blank</message> </field-validator> <field-validator type="date"> <param name="min">2010-01-01</param> <message> (XML)graduation is date and greater than ${min} </message> </field-validator> </field> </validators>
RegistAction-validation2.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd"> <validators> <validator type="requiredstring"> <param name="fieldName">username</param> <message>(XML2)username is not blank</message> </validator> <validator type="requiredstring"> <param name="fieldName">password</param> <message>(XML2)password is not blank</message> </validator> <validator type="required"> <param name="fieldName">age</param> <message>(XML2)age is not blank</message> </validator> <validator type="required"> <param name="fieldName">birthday</param> <message>(XML2)birthday is not blank</message> </validator> <validator type="required"> <param name="fieldName">graduation</param> <message>(XML2)graduation is not blank</message> </validator> <validator type="stringlength"> <param name="fieldName">username</param> <param name="minLength">2</param> <param name="maxLength">5</param> <message>(XML2)username is ${minLength}~${maxLength}</message> </validator> <validator type="stringlength"> <param name="fieldName">password</param> <param name="minLength">3</param> <param name="maxLength">6</param> <message>(XML2)password is ${minLength}~${maxLength}</message> </validator> <validator type="fieldexpression"> <param name="fieldName">repassword</param> <param name="expression"> <![CDATA[(password==repassword)]]> </param> <message>(XML2)two password should be the same!</message> </validator> <validator type="int"> <param name="fieldName">age</param> <param name="min">20</param> <param name="max">100</param> <message>(XML2)age is ${min}~${max}</message> </validator> <validator type="date"> <param name="fieldName">birthday</param> <param name="min">2000-01-01</param> <message> (XML2)birthday is date and greater than ${min} </message> </validator> <validator type="date"> <param name="fieldName">graduation</param> <param name="min">2010-01-01</param> <message> (XML2)graduation is date and greater than ${min} </message> </validator> </validators>
同时字段校验和非字段校验这两种方法也可以混搭使用,即在同一个校验配置文件中配置这两种。
校验器名词type可选值可以通过查找xwork的jar包下的default.xml,位置在:展开xwork-core-2.3.24.1.jar->com.opensymphony.xwork2.validator.validators.default.xml。
default.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator Definition 1.0//EN" "http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd"> <!-- START SNIPPET: validators-default --> <validators> <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/> <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/> <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/> <validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/> <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/> <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/> <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/> <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/> <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/> <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/> <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/> <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/> <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/> <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/> <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/> <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/> </validators> <!-- END SNIPPET: validators-default -->
文件中name就是校验器名称,对应的就是type可选值,class是校验器对应的实现类,校验器配置的param参数就是实现类中的属性。
以下是实例程序运行结果:
校验的执行顺序:内置类型转换->XML校验器校验->validateToRegist->validate->toRegist
校验文件中message的国际化方式:
在Action同级目录下添加一个ActionName.properties文件,定义key-value,然后,在message标签的key属性中填充key值,例如:
结果如下:
这时可以给定义多个ActionName.properties文件以适应浏览器使用的不同语言包,例如:ActionName_en_US.properties、ActionName_zh_CN.properties,这样程序就会根据浏览器选择的语言包自适应提示信息,如果找不到对应语言包的properties文件,将使用默认ActionName.properties的配置。
运行结果:
原理解析:
校验器基类FieldValidatorSupport,是所有校验器的父类,提供了fieldName和type属性。
FieldValidatorSupport.class(转)struts2:数据校验,通过XWork校验框架实现(validation.xml)