strut2 学习之验证框架六属性验证示例

前面讲了这么多,下面我们看一下struts2的自带示例.

先看一下截图


strut2 学习之验证框架六属性验证示例_第1张图片

当我不填任何内容的时候,直接提交,则会被验证框架拦截,并返回到我们的输入界面,并显示出验证消息.

下面就让我们一步一步来实现这个功能吧.

1.用myeclipse 新建web工程,并引入struts2的jar包,这个过程相信大家都已经很熟悉了,所以这里就省略了.

2.新建一个类FieldValidatorsExampleAction 并继承ActionSupport

代码如下:省略了相应的set与get方法.

public class FieldValidatorsExampleAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2962790485482352503L;
	private String requiredValidatorField;
	private String requiredStringValidatorField;
	private Integer integerValidatorField;
	private Date dateValidatorField;
	private String emailValidatorField;
	private String urlValidatorField;
	private String stringLengthValidatorField;
	private String regexValidatorField;
	private String fieldExpressionValidatorField;
}

3. 在FieldValidatorsExampleAction 类的同一个目录新建FieldValidatorsExampleAction-validation.xml文件.

内容如下:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE validators PUBLIC 
  		"-//Apache Struts//XWork Validator 1.0//EN"
  		"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
  		
<validators>
	<field name="requiredValidatorField">
		<field-validator type="required">
			<message><![CDATA[ required ]]></message>
		</field-validator>
	</field>
	<field name="requiredStringValidatorField">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message><![CDATA[ required and must be string ]]></message>
		</field-validator>
	</field>
	<field name="integerValidatorField">
		<field-validator type="int">
			<param name="min">1</param>
			<param name="max">10</param>
			<message><![CDATA[ must be integer min 1 max 10 if supplied ]]></message>
		</field-validator>
	</field>
	<field name="dateValidatorField">
		<field-validator type="date">
			<param name="min">01/01/1990</param>
			<param name="max">01/01/2000</param>
			<message><![CDATA[ must be a min 01-01-1990 max 01-01-2000 if supplied ]]></message>
		</field-validator>
	</field>
	<field name="emailValidatorField">
		<field-validator type="email">
			<message><![CDATA[ must be a valid email if supplied ]]></message>
		</field-validator>
	</field>
	<field name="urlValidatorField">
		<field-validator type="url">
			<message><![CDATA[ must be a valid url if supplied ]]></message>
		</field-validator>
	</field>
	<field name="stringLengthValidatorField">
		<field-validator type="stringlength">
			<param name="maxLength">4</param>
			<param name="minLength">2</param>
			<param name="trim">true</param>
			<message><![CDATA[ must be a String of a specific greater than 1 less than 5 if specified ]]></message>
		</field-validator>
	</field>
	<field name="regexValidatorField">
		<field-validator type="regex">
			<param name="expression">.*\.txt</param>
			<message><![CDATA[ regexValidatorField must match a regexp (.*\.txt) if specified ]]></message>
		</field-validator>
	</field>
	<field name="fieldExpressionValidatorField">
		<field-validator type="fieldexpression">
			<param name="expression">(fieldExpressionValidatorField == requiredValidatorField)</param>
			<message><![CDATA[ must be the same as the Required Validator Field if specified ]]></message>
		</field-validator>
	</field>
</validators>


4..在struts2.xml 中配置我们的action.

   代码如下:

		<action name="validation-input">
 <result>/validate-input.jsp</result>
 </action>	
		<action name="validation" class="action.main.FieldValidatorsExampleAction">
			<result name="input">/validate-input.jsp</result>
		</action>
5..在根目录下,新建validate-input.jsp 文件.

主要代码如下: 


	<s:form action="submitFieldValidatorsExamples" namespace="/validation" method="POST" theme="xhtml">
            <s:textfield label="Required Validator Field" name="requiredValidatorField" />
            <s:textfield label="Required String Validator Field" name="requiredStringValidatorField" />
            <s:textfield label="Integer Validator Field" name="integerValidatorField" />
            <s:textfield label="Date Validator Field" name="dateValidatorField" />
            <s:textfield label="Email Validator Field" name="emailValidatorField" />
            <s:textfield label="URL Validator Field" name="urlValidatorField" />
            <s:textfield label="String Length Validator Field" name="stringLengthValidatorField" />
            <s:textfield label="Regex Validator Field" name="regexValidatorField"/>
            <s:textfield label="Field Expression Validator Field" name="fieldExpressionValidatorField" />
            <s:submit label="Submit" />
        </s:form>

别忘了引入strust2的标签库

<%@taglib prefix="s" uri="/struts-tags" %>


6,经过上面几步,基本上就可以运行咱们的代码了.

发布到tomcat 下之后,进入表单提交页面,然后我们也是直接提交,运行结果如下:


有的朋友会说,怎么跟示例不一样呀,因为我们没有使用<s:fielderror />这个标签,那为什么还能出现错误提示呢.因为struts2的表单标签默认会有提示信息.

欢迎大家阅读,这次就写到这里了.


你可能感兴趣的:(strut2 学习之验证框架六属性验证示例)