struts2--详细了解struts2框架的校验器

和拦截器一样,struts2提供了内置校验器。在/com/opensymphony/xwork2/validator/validators/default.xml下。具体如:

default.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-config-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 -->
1、转换校验器(conversion):

字段校验风格:

<validators>
    <field name = "user.age">
    <field-validator type = "conversion">
       <param name = "repoplateField">true</param><!--类型转化失败,仍保留原来的错误输入-->
       <message>你的年龄必须是一个整数</message>
    </field-validator> 
  </field>
</validators>    


非字段校验器配置风格:

<validators>
    <validator type = "conversion">
       <param name = "fieldName">user.age</param>
       <param name = "repoplateField">false</param>
       <message>你的年龄必须是一个整数</message>
  </validator>
</validators>   

2、邮件地址校验器(email):

    <field name = "user.email">
    <field-validator type = "email">
       <message>您的邮件格式不正确</message>
    </field-validator> 
  </field>

3、网址校验器(url):

    <field name = "字段名">
    <field-validator type = "url">
       <message>提示错误信息</message>
    </field-validator> 
  </field>
4、表达式校验器(expression):(不可用在字段校验确配置风格中)

表达式校验器的名字:expression,它是一个非字段校验器, 不可在字段校验器中使用它,该表达式要求OGNL表达式返回TRUE;当返回TRUE时,该校验通过,否则就算该校验没有通过!

 

参数:

  1. expression: 该参数指定一个逻辑表达式,该逻辑表达式基于ValueStack进行求值,最后返回一个Boolean值,当返回TRUE时校验通过,否则校验失败
  <validator type = "expression">
     <param name = "expression">……</param>
     <message>提示错误信息</message>
  </validator>


5、必填校验器(required):检查字段必须有值(null)

	<field name="user.password">
		<field-validator type="required">
			<message>请输入密码</message>
		</field-validator>
	</field>
由于是和null作比较,而表单提交的是字符串,当不填写任何内容时为"",导致无法达到效果。当表单提交没有此字段时为null,如:
  <body>
  <s:debug/>
    <s:form action = "login" method = "post">
     <s:textfield name = "user.username" label = "用户名"/>
    <!-- <s:password name = "user.password" label = "密码"/> --> 
     <s:textfield name = "user.age" label = "年龄"/>
     <s:textfield name = "user.tel" label = "电话"/>
     <s:textfield name = "user.birthday" label = "生日"/>
     <s:submit value = "提交"/>
    </s:form>
  </body>
debug看结果:


6、字符串长度校验器(requiredstring):

	<field name="user.username">
		<field-validator type="requiredstring">
			<param name="doTrim">true</param>      <!--doTrim除去字符串前后空格-->
			<message>用户名不能为空</message>
		</field-validator>
	</field>

7、整数校验器(int):

   <field name = "user.age">
    <field-validator type = "int">
       <param name = "min">1</param>
       <param name = "max">100</param>
       <message>输入的年龄必须在1-100之间</message>
    </field-validator> 
  </field>

8、日期校验类型(date)
   <field name = "user.birthday">
    <field-validator type = "date">
       <param name = "min">1990-01-01</param>
       <param name = "max">2020-01-01</param>
       <message>请正确输入生日</message>
     </field-validator> 
  </field>


9、字段表达式校验器(fieldexpression)

	<field name="user.repassword">
		<field-validator type="fieldexpression">
		    <param name = "expression"><![CDATA[user.password==user.repassword]]></param>
			<message>两次密码不一致</message>
		</field-validator>
	</field>

10、正则表达式校验器(regex)

  <field name = "user.password">
    <field-validator type = "regex">
       <param name = "expression"><![CDATA[(\w{6})]]></param>
       <message>${getText("user.password.regex")}</message>
    </field-validator> 
  </field>

11、visitor校验器:

该属性用于检测Action里的复合属性(如User对象等)以User对象为例说明:

LoginAction.java:

package com.action;

import com.bean.User;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
 private User user;
 public String execute() {
	return SUCCESS;
}
public User getUser() {
	return user;
}

public void setUser(User user) {
	this.user = user;
}
 
}


LoginAction-validation.xml配置文件:

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

<validators>

  <field name = "user"> 
     <field-validator type = "visitor">
     <!-- 指定校验规则的文件context -->
         <param name = "context">userContext</param>
         <!-- 指定校验失败后的提示信息是否添加下面的前缀 -->
         <param name = "appendPrefix">true</param><!-- 默认为true -->
         <message>用户的:</message>
     </field-validator>
  </field>
</validators>      
为User属性配置单独的校验文件,默认的文件名应为User-validation.xml,因为在LoginAction-validation.xml文件中指明了visitor的context属性userContext,因此校验文件名应为User-userContext-validation.xml:

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE validators PUBLIC
  		"-//OpenSymphony Group//XWork Validator 1.0.3//EN"
  		"http://www.opensymphony.com/xwork/xwork-validator-1.0.3.dtd">
  		
<validators>
 	<field name="username">
		<field-validator type="requiredstring">
			<param name="doTrim">true</param>
			<message>用户名不能为空</message>
		</field-validator>
	</field>
</validators>
该配置文件需要和User.java在同一目录下,否则校验器不起作用




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏油

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值