效验器三类:

编程式——Java代码

声明式——xml

注释法——@

注解验证可以修饰属性的getter方法,
也可以修饰执行方法
Action中校验失败时,返回input逻辑视图

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>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<package name="p1" namespace="/" extends="struts-default">
<action name="*Action" class="org.ah.s2.C1" method="{1}">
<result name="success" type="dispatcher">
/inputMsg.jsp
</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>

Action:(没有:C1-validation.xml)

package org.ah.s2;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.CustomValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
import com.opensymphony.xwork2.validator.annotations.Validations; public class C1 extends ActionSupport {
private String uname;
private String pwd;
private String pwd2;
private Date birth;
private int weight; public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} public String getPwd2() {
return pwd2;
} public void setPwd2(String pwd2) {
this.pwd2 = pwd2;
} public Date getBirth() {
return birth;
} public void setBirth(Date birth) {
this.birth = birth;
} public int getWeight() {
return weight;
} public void setWeight(int weight) {
this.weight = weight;
} public void setUname(String uname) {
this.uname = uname;
} // 不能写在Model上
// set方法不能丢
// parameters是个数组,用{}包含
@RequiredStringValidator(trim = true, message = "必须输入名字")
@CustomValidator(type = "cn", parameters = { @ValidationParameter(name = "model", value = "all") }, message = "要中文")
public String getUname() {
return uname;
} @Validations(
  requiredStrings
= { @RequiredStringValidator(fieldName = "pwd", message = "密码必须输入") },
  intRangeFields = { @IntRangeFieldValidator(fieldName = "weight", min = "1", max = "300", message = "体重必须在${min}和${max}之间") }
  )
public String m1() {
return "success";
}
}

自定义效验器:

package org.ah.s2;

import java.util.regex.Matcher;
import java.util.regex.Pattern; import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport; public class MyValidator extends FieldValidatorSupport {
private String model; public String getModel() {
return model;
} public void setModel(String model) {
this.model = model;
} @Override
public void validate(Object obj_i) throws ValidationException {
// 字段名
String fieldName = getFieldName();
// 字段值
Object fieldValue = getFieldValue(fieldName, obj_i); if (fieldValue instanceof String) {
String strV = (String) fieldValue;
/*
* 中文unicode none:无中文 all :全中文
*/
if ("none".equals(model)) {
// compile:编译
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(strV); if (m.find()) {// 找到中文
// 效验失败(显示不出来,随便写写,不写不报错)
addFieldError(fieldName, obj_i);
}
} else if ("all".equals(model)) {
if (!Pattern.matches("[\u4e00-\u9fa5]+", strV)) {
// 不是全中文
addFieldError(fieldName, obj_i);// 效验失败
}
}
}
} }

src下配置自定义的效验器:

validators.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">
<validators>
<validator name="cn" class="org.ah.s2.MyValidator" />
</validators>

表示层:

index.jsp

    <s:form action="m1Action" method="post">
<s:textfield name="uname" label="用户名" />
<s:textfield name="pwd" label="密码" />
<s:textfield name="pwd2" label="确认密码" />
<s:textfield name="birth" label="生日" />
<s:textfield name="weight" label="体重" />
<s:submit value="提交"></s:submit>
</s:form>

inputMsg.jsp 略

运行结果:

Struts2:效验器——注解的更多相关文章

  1. Struts2:效验器——声明式

    就是用xml配置的方式,而不是Java代码的形式,那个是“编程式” Action: package org.ah.s2; import com.opensymphony.xwork2.ActionSu ...

  2. Struts2 拦截器配置以及实现

    @(Java ThirdParty)[Struts|Interceptor] Struts2 拦截器配置以及实现 Struts2的拦截器应用于Action,可以在执行Action的方法之前,之后或者两 ...

  3. Struts2框架之-注解开发

    Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...

  4. 基于SSH2框架Struts2拦截器的登录验证实现(转)

        大象在这里假设你已经弄清楚了Struts2拦截器的基本概念,可以进入实际运用了.那么我们在之前的基础上只需要做下小小的改变,就可以使用Struts2的拦截器机制实现登录的验证.     修改数 ...

  5. Springmvc_validation 效验器

    springmvc-validation效验器的使用介绍 对于任何一个应用来说,都会做数据的有效性效验,但是只在前端做并不是很安全,考虑到安全性這个时候会要求我们在服务端也对数据进行有效验证,spri ...

  6. SpringMVC—Struts2拦截器学习网址整理

    引自:http://blog.csdn.net/wp1603710463/article/details/49982683 SpringMVC—Struts2拦截器学习网址整理 最近项目中遇到权限相关 ...

  7. struts2动态调用+Servlet过滤器+struts2拦截器

    周末真的是懒到心慌...... 本文是在完整s2sh项目基础上添加的,不太了解s2sh项目构建的朋友可以先参考一下这几篇文章: eclipse环境下基于tomcat-7.0.82构建struts2项目 ...

  8. struts2 拦截器

    拦截器:对Action的访问.可以拦截到Action中某个方法.与过滤器不同,过滤器过滤的是请求.过滤JSP.html.但是拦截器不能拦截jsp.html的访问. Struts2 拦截器在访问某个 A ...

  9. Struts2拦截器的使用 (详解)

    Struts2拦截器的使用 (详解) 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈default ...

随机推荐

  1. jade转化为html

    网址:http://www.html2jade.org/ 刚到一家新的公司 ,上一个前段PHP写的代码基本都是jade写的,看的一脸懵逼,第一次遇到jade代码,并且我一直用的是atom开发工具,安装 ...

  2. linux5个搜索命令

    概要 linux中主要有5个文件查找命令:find.locate.whereis.which.type.find最为强大,但耗时较长.locate可看做find的精简版,但是它的速度非常快.where ...

  3. h5移动端-1

    iphone3 : 设备分辨率 : 320*480 屏幕分辨率 : 320*480 iphone4 : 设备分辨率 : 320*480 屏幕分辨率 : 640*960 iphone5 : 设备分辨率 ...

  4. java读取properties文件工具

    public class PropertiesUtil { public static String get(String filePath, String key) { String val = n ...

  5. Windows 10 安装TA-Lib python库

    由于需要和朋友比对一个结果,需要在Windows 10中安装TA-Lib库,写点简单的python代码. 本来以为就简单的执行下pip install TA-Lib就OK了. 然后,安装失败: fat ...

  6. [leetcode]Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  7. 强大的矩阵奇异值分解(SVD)及其应用

    版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...

  8. js调用页面打印

    ----------------------调用页面打印-------------------------------- <body> <div id="divPrint& ...

  9. Thread基本介绍

    1.Thread类介绍 Class Thread java.lang.Object java.lang.Thread All Implemented Interfaces: Runnable Dire ...

  10. 系统安全:Nessus Home版安装使用

    1.安装  下载地址:http://www.tenable.com/products/nessus/select-your-operating-system#tos 安装命令:rpm -ivh  Ne ...