其实,struts2的表单验证感觉真的好鸡肋,要不是为了系统的整理一下struts2的功能,根本不会去想它,抽取一个官方的例子吧。
struts2的验证分为客户端和服务端,可以通过注解和配置文件的方式去配置,以下是通过配置文件的方式
定义一个Action,自定义的Action必须继承ActionSupport才能使得校验生效
1 package validation;
2
3 import java.sql.Date;
4
5 import com.opensymphony.xwork2.ActionSupport;
6
7
8
9 public class FieldValidatorsExampleAction extends ActionSupport {
10
11 private static final long serialVersionUID = -4829381083003175423L;
12
13 private String requiredValidatorField = null;
14 private String requiredStringValidatorField = null;
15 private Integer integerValidatorField = null;
16 private Date dateValidatorField = null;
17 private String emailValidatorField = null;
18 private String urlValidatorField = null;
19 private String stringLengthValidatorField = null;
20 private String regexValidatorField = null;
21 private String fieldExpressionValidatorField = null;
22
23 public String add(){
24 System.out.println("Add Execute");
25 return "success";
26 }
27
28 public String validateAdd(){
29 System.out.println("validateAdd Execute");
30 return "input";
31 }
32
33 public String submit() throws Exception {
34 return "success";
35 }
36
37 public String input() throws Exception {
38 return "input";
39 }
40
41 //get/set略
42 }
43
之后在Action的同级目录下新增客户端校验规则:FieldValidatorsExampleAction-submitClientSideValidationExample-validation.xml
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2
3 <!DOCTYPE validators PUBLIC
4 "-//Apache Struts//XWork Validator 1.0//EN"
5 "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
6
7 <validators>
8 <field name="requiredValidatorField">
9 <field-validator type="required">
10 <message><![CDATA[ required ]]></message>
11 </field-validator>
12 </field>
13 <field name="requiredStringValidatorField">
14 <field-validator type="requiredstring">
15 <param name="trim">true</param>
16 <message key="i18n.nothing.here"><![CDATA[ required and must be string ]]></message>
17 </field-validator>
18 </field>
19 <field name="requiredStringValidatorField">
20 <field-validator type="requiredstring">
21 <param name="trim">true</param>
22 <message key="i18n.requiredstring"><![CDATA[ required and must be string ]]></message>
23 </field-validator>
24 </field>
25 <field name="integerValidatorField">
26 <field-validator type="int">
27 <param name="min">1</param>
28 <param name="max">10</param>
29 <message><![CDATA[ must be integer min 1 max 10 if supplied ]]></message>
30 </field-validator>
31 </field>
32 <field name="dateValidatorField">
33 <field-validator type="date">
34 <param name="min">01/01/1990</param>
35 <param name="max">01/01/2000</param>
36 <message><![CDATA[ must be a min 01-01-1990 max 01-01-2000 if supplied ]]></message>
37 </field-validator>
38 </field>
39 <field name="emailValidatorField">
40 <field-validator type="email">
41 <message><![CDATA[ must be a valid email if supplied ]]></message>
42 </field-validator>
43 </field>
44 <field name="urlValidatorField">
45 <field-validator type="url">
46 <message><![CDATA[ must be a valid url if supplied ]]></message>
47 </field-validator>
48 </field>
49 <field name="stringLengthValidatorField">
50 <field-validator type="stringlength">
51 <param name="maxLength">4</param>
52 <param name="minLength">2</param>
53 <param name="trim">true</param>
54 <message><![CDATA[ must be a String of a specific greater than 1 less than 5 if specified ]]></message>
55 </field-validator>
56 </field>
57 <field name="regexValidatorField">
58 <field-validator type="regex">
59 <param name="regex">.*\.txt</param>
60 <message><![CDATA[ regexValidatorField must match a regexp (.*\.txt) if specified ]]></message>
61 </field-validator>
62 </field>
63 <field name="fieldExpressionValidatorField">
64 <field-validator type="fieldexpression">
65 <param name="expression">(fieldExpressionValidatorField == requiredValidatorField)</param>
66 <message><![CDATA[ must be the same as the Required Validator Field if specified ]]></message>
67 </field-validator>
68 </field>
69 </validators>
70
还有服务端校验规则:FieldValidatorsExampleAction-submitFieldValidatorsExamples-validation.xml
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2
3 <!DOCTYPE validators PUBLIC
4 "-//Apache Struts//XWork Validator 1.0//EN"
5 "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
6
7 <validators>
8 <field name="requiredValidatorField">
9 <field-validator type="required">
10 <message><![CDATA[ required ]]></message>
11 </field-validator>
12 </field>
13 <field name="requiredStringValidatorField">
14 <field-validator type="requiredstring">
15 <param name="trim">true</param>
16 <message><![CDATA[ required and must be string ]]></message>
17 </field-validator>
18 </field>
19 <field name="integerValidatorField">
20 <field-validator type="int">
21 <param name="min">1</param>
22 <param name="max">10</param>
23 <message><![CDATA[ must be integer min 1 max 10 if supplied ]]></message>
24 </field-validator>
25 </field>
26 <field name="dateValidatorField">
27 <field-validator type="date">
28 <param name="min">01/01/1990</param>
29 <param name="max">01/01/2000</param>
30 <message><![CDATA[ must be a min 01-01-1990 max 01-01-2000 if supplied ]]></message>
31 </field-validator>
32 </field>
33 <field name="emailValidatorField">
34 <field-validator type="email">
35 <message><![CDATA[ must be a valid email if supplied ]]></message>
36 </field-validator>
37 </field>
38 <field name="urlValidatorField">
39 <field-validator type="url">
40 <message><![CDATA[ must be a valid url if supplied ]]></message>
41 </field-validator>
42 </field>
43 <field name="stringLengthValidatorField">
44 <field-validator type="stringlength">
45 <param name="maxLength">4</param>
46 <param name="minLength">2</param>
47 <param name="trim">true</param>
48 <message><![CDATA[ must be a String of a specific greater than 1 less than 5 if specified ]]></message>
49 </field-validator>
50 </field>
51 <field name="regexValidatorField">
52 <field-validator type="regex">
53 <param name="regex"><![CDATA[ [^<>]+ ]]></param>
54 <message><![CDATA[ regexValidatorField must match a regexp (.*\.txt) if specified ]]></message>
55 </field-validator>
56 </field>
57 <field name="fieldExpressionValidatorField">
58 <field-validator type="fieldexpression">
59 <param name="expression">(fieldExpressionValidatorField == requiredValidatorField)</param>
60 <message><![CDATA[ must be the same as the Required Validator Field if specified ]]></message>
61 </field-validator>
62 </field>
63 </validators>
输入页面:fieldValidatorsExample.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <%@taglib prefix="s" uri="/struts-tags" %>
4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5
6 <html>
7 <head>
8 <title>Struts2 Showcase - Validation - Field Validators Example</title>
9 </head>
10 <body>
11
12 <div class="page-header">
13 <h1>Field Validation Examples</h1>
14 </div>
15
16 <div class="container-fluid">
17 <div class="row-fluid">
18 <div class="span12">
19
20 <h3>All Field Errors Will Appear Here</h3>
21 <s:fielderror/>
22 <hr/>
23
24 <h3>Field Error due to 'Required String Validator Field' Will Appear Here</h3>
25 <s:fielderror>
26 <s:param value="%{'requiredStringValidatorField'}"/>
27 </s:fielderror>
28 <hr/>
29
30 <h3>Field Error due to 'String Length Validator Field' Will Appear Here</h3>
31 <s:fielderror>
32 <s:param>stringLengthValidatorField</s:param>
33 </s:fielderror>
34 <hr/>
35
36 <s:form action="submitFieldValidatorsExamples" namespace="/validation" method="POST" theme="xhtml">
37 <s:textfield label="Required Validator Field" name="requiredValidatorField"/>
38 <s:textfield label="Required String Validator Field" name="requiredStringValidatorField"/>
39 <s:textfield label="Integer Validator Field" name="integerValidatorField"/>
40 <s:textfield label="Date Validator Field" name="dateValidatorField"/>
41 <s:textfield label="Email Validator Field" name="emailValidatorField"/>
42 <s:textfield label="URL Validator Field" name="urlValidatorField"/>
43 <s:textfield label="String Length Validator Field" name="stringLengthValidatorField"/>
44 <s:textfield label="Regex Validator Field" name="regexValidatorField"/>
45 <s:textfield label="Field Expression Validator Field" name="fieldExpressionValidatorField"/>
46 <s:submit label="Submit" cssClass="btn btn-primary"/>
47 </s:form>
48
49 <s:form action="addFieldValidatorsExamples" namespace="/validation" method="POST" theme="xhtml">
50 <s:submit value="add"/>
51 </s:form>
52
53 </div>
54 </div>
55 </div>
56 </body>
57 </html>
58
结果页面:successFieldValidatorsExample.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <%@taglib prefix="s" uri="/struts-tags" %>
4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5
6 <html>
7 <head>
8 <title>Struts2 Showcase - Validation - Success Field Validators Example</title>
9 <s:head/>
10 </head>
11 <body>
12
13 <div class="page-header">
14 <h1>Success !</h1>
15 </div>
16
17 <div class="container-fluid">
18 <div class="row-fluid">
19 <div class="span12">
20
21 <table class="table table-striped table-bordered table-hover table-condensed">
22 <tr>
23 <td>Required Validator Field:</td>
24 <td><s:property value="requiredValidatorField" /></td>
25 </tr>
26 <tr>
27 <td>Required String Validator Field:</td>
28 <td><s:property value="requiredStringValidatorField" /></td>
29 </tr>
30 <tr>
31 <td>Integer Validator Field: </td>
32 <td><s:property value="integerValidatorField" /></td>
33 </tr>
34 <tr>
35 <td>Date Validator Field: </td>
36 <td><s:property value="dateValidatorField" /></td>
37 </tr>
38 <tr>
39 <td>Email Validator Field: </td>
40 <td><s:property value="emailValidatorField" /></td>
41 </tr>
42 <tr>
43 <td>String Length Validator Field: </td>
44 <td><s:property value="stringLengthValidatorField" /></td>
45 </tr>
46 <tr>
47 <td>Regex Validator Field: <s:property value="regexValidatorField" /></td>
48 <td>Field Expression Validator Field: <s:property value="fieldExpressionValidatorField" /></td>
49 </tr>
50 </table>
51
52 </div>
53 </div>
54 </div>
55 </body>
56 </html>
57
struts.xml:
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4 "http://struts.apache.org/dtds/struts-2.3.dtd">
5
6 <struts>
7
8 <package name="validationExamples" extends="struts-default" namespace="/validation" >
9
10 <!-- ======================== -->
11 <!-- === Field Validators === -->
12 <!-- ======================== -->
13
14 <action name="showFieldValidatorsExamples" class="validation.FieldValidatorsExampleAction" method="input">
15 <result name="input" type="dispatcher">/fieldValidatorsExample.jsp</result>
16 </action>
17
18 <action name="submitFieldValidatorsExamples" class="validation.FieldValidatorsExampleAction" method="submit">
19 <result name="input" type="dispatcher">/fieldValidatorsExample.jsp</result>
20 <result type="dispatcher">/successFieldValidatorsExample.jsp</result>
21 </action>
22
23 <action name="addFieldValidatorsExamples" class="validation.FieldValidatorsExampleAction" method="add">
24 <result name="input" type="dispatcher">/successFieldValidatorsExample.jsp</result>
25 </action>
26
27 </package>
28 </struts>
29
30
个人感觉这个校验框架做得比较笨重,很多配置,而且必须要配合struts2的标签才可以使用,显得很不灵活。如果需要使用struts2的校验功能,我们或许可以选择他提供的另一种办法,就是针对某个方法的校验,比如方法add,自定义一个方法validateAdd,那么执行add方法之前会自动执行validateAdd方法进行自定义的校验,同理其他方法就需要定义以“validate”+“方法名”(大写首字母)来命名的方法来校验,校验完毕之后你可以选择使用在ActionFieldError中添加错误信息,配合struts2的标签来显示,或者自行处理。