Struts2:效验器——注解
效验器三类:
编程式——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:效验器——注解的更多相关文章
- Struts2:效验器——声明式
就是用xml配置的方式,而不是Java代码的形式,那个是“编程式” Action: package org.ah.s2; import com.opensymphony.xwork2.ActionSu ...
- Struts2 拦截器配置以及实现
@(Java ThirdParty)[Struts|Interceptor] Struts2 拦截器配置以及实现 Struts2的拦截器应用于Action,可以在执行Action的方法之前,之后或者两 ...
- Struts2框架之-注解开发
Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...
- 基于SSH2框架Struts2拦截器的登录验证实现(转)
大象在这里假设你已经弄清楚了Struts2拦截器的基本概念,可以进入实际运用了.那么我们在之前的基础上只需要做下小小的改变,就可以使用Struts2的拦截器机制实现登录的验证. 修改数 ...
- Springmvc_validation 效验器
springmvc-validation效验器的使用介绍 对于任何一个应用来说,都会做数据的有效性效验,但是只在前端做并不是很安全,考虑到安全性這个时候会要求我们在服务端也对数据进行有效验证,spri ...
- SpringMVC—Struts2拦截器学习网址整理
引自:http://blog.csdn.net/wp1603710463/article/details/49982683 SpringMVC—Struts2拦截器学习网址整理 最近项目中遇到权限相关 ...
- struts2动态调用+Servlet过滤器+struts2拦截器
周末真的是懒到心慌...... 本文是在完整s2sh项目基础上添加的,不太了解s2sh项目构建的朋友可以先参考一下这几篇文章: eclipse环境下基于tomcat-7.0.82构建struts2项目 ...
- struts2 拦截器
拦截器:对Action的访问.可以拦截到Action中某个方法.与过滤器不同,过滤器过滤的是请求.过滤JSP.html.但是拦截器不能拦截jsp.html的访问. Struts2 拦截器在访问某个 A ...
- Struts2拦截器的使用 (详解)
Struts2拦截器的使用 (详解) 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈default ...
随机推荐
- javascript实现图片切换、自动走、鼠标移入会停止,移出继续走
要实现以上效果并不难,把功能分开一步一步来实现就变得简单了,录制动态图不流畅,看代码意会吧! <!doctype html> <html lang="en"> ...
- java return
return语句的作用: 1.返回一个值,可以是任意类型的 2.使程序返回到操作系统,或者说是代表"已经做完,离开此方法" ---------------------------- ...
- SQL语句小总结
无论是面试过程中,还是未来工作中,SQL都是一定会考到和用到的.所以,在此对之前看过的一些SQL知识点进行一下总结和记录,算是起到一个笔记本的作用.没有深入学习过SQL的和对SQL印象不太深的朋友可以 ...
- Lua 中string.gsub(sourceString, pattern, replacementString) 返回值有两个
这阵子在学习lua,今天看到string操作,书中描述string.gsub(sourceString, pattern, replacementString)返回一个字符串,但是我在实际操作中却发现 ...
- 简易c语言文法
<程序>→<外部声明>|<程序><外部声明> <外部声明>→<函数定义>|<声明> <函数定义>→< ...
- 随机函数的代码(srand、rand)
#include<stdio.h> int main() int counter; for(counter=0;counter<10;counter++) { srand(count ...
- Contiki学习笔记 第一个程序:Hello World
想来想去,还是得先写一个程序,找下感觉,增强一下自信心,那就国际惯例Hello World吧.先到这个网址下一个Instant Contiki 2.7.之所以没用3.0的,是因为有些问题,我源码是下的 ...
- 图片垂直居中 和 float
//图片垂直居中, display:table-cell; vertical-align:middle; 不能和 css (float)元素共存,可以在元素外面多加一个层 css .th-left ...
- DBA-mysql-字符集
查看支持的字符集:show charset; 查看现使用字符集:status; 1.在[mysqld]下添加 default-character-set=utf8 (mysql 5.1版本) char ...
- gRaphael——JavaScript 矢量图表库:两行代码实现精美图表
gRaphael 是一个致力于帮助开发人员在网页中绘制各种精美图表的 Javascript 库,基于强大的 Raphael 矢量图形库.你只需要编写几行简单的代码就能创建出精美的条形图.饼图.点图和曲 ...