- 浏览: 961563 次
- 性别:
- 来自: 江西上饶
文章分类
- 全部博客 (460)
- p.spring (56)
- p.maven (20)
- p.ant (17)
- p.jee (18)
- p.jse (33)
- p.ofbiz (31)
- p.软件工程 (8)
- p.struts2 (5)
- p.hibernate (5)
- linux (25)
- 设计模式 (2)
- p.javascript (11)
- 硬件 (1)
- p.jsp (2)
- p.windows批处理 (1)
- 操作系统问题 (5)
- 算法 (1)
- p.mysql (7)
- p.sql (5)
- p.c (1)
- google产品 (0)
- 内存 (1)
- p.struts (1)
- p.freemarker (7)
- p.css (4)
- p.log4j (10)
- p.html (3)
- 淘宝产品 (0)
- 其他 (3)
- 编译器 (0)
- svn (4)
- p.spring.security (11)
- 图形 (0)
- p.xml (1)
- p.ssh (0)
- p.jquery (4)
- p.jdbc (3)
- p.flex (0)
- p.c++ (0)
- p.c#Net (0)
- p.assembly (0)
- p.sqlserver (0)
- p.其他 (3)
- p.webwork (21)
- p.wap (12)
- p.cglib (1)
- p.jee服务器 (11)
- windows (2)
- p.iphone (1)
- p.java.分布式与集群 (2)
- p.ibatis (16)
- p.eclipse (5)
- 架构 (2)
- http协议 (5)
- 我的个人标准 (2)
- 多线程 (1)
- 奇怪问题 (5)
- p.jira (13)
- p.httpclient (1)
- 服务器.apache (11)
- 安全防范 (1)
- p.PODAM (1)
- p.junit (16)
- fop (2)
- 硬盘安装 (1)
- powerdesigner (0)
- 单元测试 (1)
- apache commons (4)
- tomcat+apache集群 (10)
- 各类诡辩 (1)
- 安卓 (8)
- qvod (1)
- java编程基础知识考试考点及答案 (0)
- 工作总结 (4)
- oracle (0)
- spring的util工具 (3)
- json (2)
- maven (3)
- jms (19)
- p.bat (3)
- hadoop (2)
- git (3)
- nginx (1)
- p.移动开发 (1)
- shiro (3)
- 游戏破解 (1)
- react-native (7)
- ios开发 (1)
- webmagic (6)
- socks5 (1)
最新评论
-
weituotian:
说的不好,没人看的
公司系统中的菜单功能和权限功能 -
石不易:
非常详细的注解~
绑定端口和IP,Listen 与VirtualHost指令 -
spring_springmvc:
spring mvc demo教程源代码下载,地址:http: ...
spring mvc -
liyixing1:
PandaDONG 写道谢谢你啊,我已经下下来了,只是还有很多 ...
jira war安装 -
liyixing1:
PandaDONG 写道谢谢你啊,我已经下下来了,只是还有很多 ...
jira war安装
http://www.opensymphony.com/webwork/wikidocs/J2SE%205%20Support.html
http://www.opensymphony.com/webwork/wikidocs/Validation%20Annotation.html
WEBWORK中需要加入包xwork-tiger
一般有两种注解法
1.直接注在get方法上面,像这种
@RequiredStringValidator(message="Supply password")
public String getPassword() {
return password;
}
2.注在方法上面,like
@Validations(
requiredStrings={
@RequiredStringValidator(fieldName="username",message="用户名不能为空!" key="i18n,國際化功能"),
@RequiredStringValidator(fieldName="telNum",message="电话号码不能为空!")
},
regexFields={@RegexFieldValidator(fieldName="telNum",expression="^(\\+86|0|1)\\d{10,11}$",
message="电话号码格式不正确!")}
)
package com.gumtreenet.company.action;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Timestamp;
import java.util.List;
import com.gumtreenet.company.base.Page;
import com.gumtreenet.company.service.IDdSubService;
import com.gumtreenet.company.service.IDownloadlibraryService;
import com.gumtreenet.company.vo.Downloadlibrary;
import com.gumtreenet.company.vo.Resume;
import com.opensymphony.webwork.dispatcher.json.JSONObject;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork.validator.annotations.Validations;
/**
* 附件action
*
* @author 李义星
* @version 0.1
* @datetime 2011-03-22
*/
public class DownloadlibraryAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private final static String DDMAINNAME = "下载类型";
private Downloadlibrary downlib;
private IDownloadlibraryService downService;
private IDdSubService ddSubService;
private FileInputStream downIn;
private File down;
private String downFileName;
private String downContentType;
private Page page;
private JSONObject jsonObject;
private Timestamp start;
private Timestamp end;
private List<Resume> findDowns;
public Downloadlibrary getDownlib() {
return downlib;
}
public void setDownlib(Downloadlibrary downlib) {
this.downlib = downlib;
}
public IDownloadlibraryService getDownService() {
return downService;
}
public void setDownService(IDownloadlibraryService downService) {
this.downService = downService;
}
public IDdSubService getDdSubService() {
return ddSubService;
}
public void setDdSubService(IDdSubService ddSubService) {
this.ddSubService = ddSubService;
}
public FileInputStream getDownIn() {
return downIn;
}
public void setDownIn(FileInputStream downIn) {
this.downIn = downIn;
}
public File getDown() {
return down;
}
public void setDown(File down) {
this.down = down;
}
public String getDownFileName() {
return downFileName;
}
public void setDownFileName(String downFileName) {
this.downFileName = downFileName;
}
public String getDownContentType() {
return downContentType;
}
public void setDownContentType(String downContentType) {
this.downContentType = downContentType;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public JSONObject getJsonObject() {
return jsonObject;
}
public void setJsonObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
}
public Timestamp getStart() {
return start;
}
public void setStart(Timestamp start) {
this.start = start;
}
public Timestamp getEnd() {
return end;
}
public void setEnd(Timestamp end) {
this.end = end;
}
public List<Resume> getFindDowns() {
return findDowns;
}
public void setFindDowns(List<Resume> findDowns) {
this.findDowns = findDowns;
}
@Validations(requiredStrings = {
@RequiredStringValidator(fieldName = "downFileName", message = "请输入附件标题"),
@RequiredStringValidator(fieldName = "downFileName", message = "请输入附件") })
public String addDown() {
return SUCCESS;
}
@Validations(requiredStrings = {
@RequiredStringValidator(fieldName = "downFileName", message = "请1"),
@RequiredStringValidator(fieldName = "downFileName", message = "") })
public String deleteDown() {
return SUCCESS;
}
}
但无论是哪种注解,默认情况下都是对这个action的所有方法进行验证。
如上面的图,我虽然给deleteDown和addDown两个方法都写了对应的验证注解,但是结果是访问delete的时候,add的验证也起作用了。
在struts2中好解决
<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
struts2中这个验证器是支持按方法进行区分注解的。
只要在
<!--开启方法级别的验证-->
validation的配置上加入validateAnnotatedMethodOnly,值是true
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
<param name="validateAnnotatedMethodOnly">true</param>
</interceptor-ref>
但是webwork是不支持的。
webwork默认是使用xwork1的。而xwork1的注解验证需要xwork-tiger.jar的支持。
我对其进行的跟踪。发现它构建注解验证配置的是在类com.opensymphony.xwork.validator.AnnotationValidationConfigurationBuilder的静态方法
public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(Class aClass) 从参数中可以看出来,它是不支持方法级别的验证的。
因此必须对这个类进行改进。
public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(Class aClass) {
List<ValidatorConfig> result = new ArrayList<ValidatorConfig>();
List<ValidatorConfig> temp = processAnnotations(aClass);
if (temp != null) {
result.addAll(temp);
}
Method[] methods = aClass.getDeclaredMethods();
if ( methods != null ) {
for (Method method : methods) {
temp = processAnnotations(method);
if (temp != null) {
result.addAll(temp);
}
}
}
return result;
}
它的上级调用代码(堆栈)是:
protected List<ValidatorConfig> buildClassValidatorConfigs(Class aClass, boolean checkFile) {
String fileName = aClass.getName().replace('.', '/') + VALIDATION_CONFIG_SUFFIX;
List<ValidatorConfig> result = new ArrayList<ValidatorConfig>(loadFile(fileName, aClass, checkFile));
List<ValidatorConfig> annotationResult = new ArrayList<ValidatorConfig>(AnnotationValidationConfigurationBuilder.buildAnnotationClassValidatorConfigs(aClass));
result.addAll(annotationResult);
return result;
}
两部分都可以看出,它并没有把调用的实际方法传进来,因此可以知道它不支持方法级别的注解。
再往上一级调用堆栈是
private List<ValidatorConfig> buildValidatorConfigs(Class clazz, String context, boolean checkFile, Set checked) {
List<ValidatorConfig> validatorConfigs = new ArrayList<ValidatorConfig>();
if (checked == null) {
checked = new TreeSet<String>();
} else if (checked.contains(clazz.getName())) {
return validatorConfigs;
}
if (clazz.isInterface()) {
Class[] interfaces = clazz.getInterfaces();
for (Class anInterface : interfaces) {
validatorConfigs.addAll(buildValidatorConfigs(anInterface, context, checkFile, checked));
}
} else {
if (!clazz.equals(Object.class)) {
validatorConfigs.addAll(buildValidatorConfigs(clazz.getSuperclass(), context, checkFile, checked));
}
}
// look for validators for implemented interfaces
Class[] interfaces = clazz.getInterfaces();
for (Class anInterface1 : interfaces) {
if (checked.contains(anInterface1.getName())) {
continue;
}
validatorConfigs.addAll(buildClassValidatorConfigs(anInterface1, checkFile));
if (context != null) {
validatorConfigs.addAll(buildAliasValidatorConfigs(anInterface1, context, checkFile));
}
checked.add(anInterface1.getName());
}
validatorConfigs.addAll(buildClassValidatorConfigs(clazz, checkFile));
if (context != null) {
validatorConfigs.addAll(buildAliasValidatorConfigs(clazz, context, checkFile));
}
checked.add(clazz.getName());
return validatorConfigs;
}
这里的参数context就是方法名。
validatorConfigs.addAll(buildClassValidatorConfigs(clazz, checkFile));
这个方法是针对全局的验证配置加载
validatorConfigs.addAll(buildAliasValidatorConfigs(clazz, context, checkFile));这个是针对方法级别的配置加载,但是不支持注解。
由于http://www.opensymphony.com/webwork/wikidocs/J2SE%205%20Support.html
http://www.opensymphony.com/webwork/wikidocs/Validation%20Annotation.html
WEBWORK中需要加入包xwork-tiger
一般有两种注解法
1.直接注在get方法上面,像这种
@RequiredStringValidator(message="Supply password")
public String getPassword() {
return password;
}
2.注在方法上面,like
@Validations(
requiredStrings={
@RequiredStringValidator(fieldName="username",message="用户名不能为空!" key="i18n,國際化功能"),
@RequiredStringValidator(fieldName="telNum",message="电话号码不能为空!")
},
regexFields={@RegexFieldValidator(fieldName="telNum",expression="^(\\+86|0|1)\\d{10,11}$",
message="电话号码格式不正确!")}
)
package com.gumtreenet.company.action;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Timestamp;
import java.util.List;
import com.gumtreenet.company.base.Page;
import com.gumtreenet.company.service.IDdSubService;
import com.gumtreenet.company.service.IDownloadlibraryService;
import com.gumtreenet.company.vo.Downloadlibrary;
import com.gumtreenet.company.vo.Resume;
import com.opensymphony.webwork.dispatcher.json.JSONObject;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork.validator.annotations.Validations;
/**
* 附件action
*
* @author 李义星
* @version 0.1
* @datetime 2011-03-22
*/
public class DownloadlibraryAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private final static String DDMAINNAME = "下载类型";
private Downloadlibrary downlib;
private IDownloadlibraryService downService;
private IDdSubService ddSubService;
private FileInputStream downIn;
private File down;
private String downFileName;
private String downContentType;
private Page page;
private JSONObject jsonObject;
private Timestamp start;
private Timestamp end;
private List<Resume> findDowns;
public Downloadlibrary getDownlib() {
return downlib;
}
public void setDownlib(Downloadlibrary downlib) {
this.downlib = downlib;
}
public IDownloadlibraryService getDownService() {
return downService;
}
public void setDownService(IDownloadlibraryService downService) {
this.downService = downService;
}
public IDdSubService getDdSubService() {
return ddSubService;
}
public void setDdSubService(IDdSubService ddSubService) {
this.ddSubService = ddSubService;
}
public FileInputStream getDownIn() {
return downIn;
}
public void setDownIn(FileInputStream downIn) {
this.downIn = downIn;
}
public File getDown() {
return down;
}
public void setDown(File down) {
this.down = down;
}
public String getDownFileName() {
return downFileName;
}
public void setDownFileName(String downFileName) {
this.downFileName = downFileName;
}
public String getDownContentType() {
return downContentType;
}
public void setDownContentType(String downContentType) {
this.downContentType = downContentType;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public JSONObject getJsonObject() {
return jsonObject;
}
public void setJsonObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
}
public Timestamp getStart() {
return start;
}
public void setStart(Timestamp start) {
this.start = start;
}
public Timestamp getEnd() {
return end;
}
public void setEnd(Timestamp end) {
this.end = end;
}
public List<Resume> getFindDowns() {
return findDowns;
}
public void setFindDowns(List<Resume> findDowns) {
this.findDowns = findDowns;
}
@Validations(requiredStrings = {
@RequiredStringValidator(fieldName = "downFileName", message = "请输入附件标题"),
@RequiredStringValidator(fieldName = "downFileName", message = "请输入附件") })
public String addDown() {
return SUCCESS;
}
@Validations(requiredStrings = {
@RequiredStringValidator(fieldName = "downFileName", message = "请1"),
@RequiredStringValidator(fieldName = "downFileName", message = "") })
public String deleteDown() {
return SUCCESS;
}
}
但无论是哪种注解,默认情况下都是对这个action的所有方法进行验证。
如上面的图,我虽然给deleteDown和addDown两个方法都写了对应的验证注解,但是结果是访问delete的时候,add的验证也起作用了。
在struts2中好解决
<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
struts2中这个验证器是支持按方法进行区分注解的。
只要在
<!--开启方法级别的验证-->
validation的配置上加入validateAnnotatedMethodOnly,值是true
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
<param name="validateAnnotatedMethodOnly">true</param>
</interceptor-ref>
但是webwork是不支持的。
webwork默认是使用xwork1的。而xwork1的注解验证需要xwork-tiger.jar的支持。
我对其进行的跟踪。发现它构建注解验证配置的是在类com.opensymphony.xwork.validator.AnnotationValidationConfigurationBuilder的静态方法
public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(Class aClass) 从参数中可以看出来,它是不支持方法级别的验证的。
因此必须对这个类进行改进。
public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(Class aClass) {
List<ValidatorConfig> result = new ArrayList<ValidatorConfig>();
List<ValidatorConfig> temp = processAnnotations(aClass);
if (temp != null) {
result.addAll(temp);
}
Method[] methods = aClass.getDeclaredMethods();
if ( methods != null ) {
for (Method method : methods) {
temp = processAnnotations(method);
if (temp != null) {
result.addAll(temp);
}
}
}
return result;
}
它的上级调用代码(堆栈)是:
protected List<ValidatorConfig> buildClassValidatorConfigs(Class aClass, boolean checkFile) {
String fileName = aClass.getName().replace('.', '/') + VALIDATION_CONFIG_SUFFIX;
List<ValidatorConfig> result = new ArrayList<ValidatorConfig>(loadFile(fileName, aClass, checkFile));
List<ValidatorConfig> annotationResult = new ArrayList<ValidatorConfig>(AnnotationValidationConfigurationBuilder.buildAnnotationClassValidatorConfigs(aClass));
result.addAll(annotationResult);
return result;
}
两部分都可以看出,它并没有把调用的实际方法传进来,因此可以知道它不支持方法级别的注解。
再往上一级调用堆栈是
private List<ValidatorConfig> buildValidatorConfigs(Class clazz, String context, boolean checkFile, Set checked) {
List<ValidatorConfig> validatorConfigs = new ArrayList<ValidatorConfig>();
if (checked == null) {
checked = new TreeSet<String>();
} else if (checked.contains(clazz.getName())) {
return validatorConfigs;
}
if (clazz.isInterface()) {
Class[] interfaces = clazz.getInterfaces();
for (Class anInterface : interfaces) {
validatorConfigs.addAll(buildValidatorConfigs(anInterface, context, checkFile, checked));
}
} else {
if (!clazz.equals(Object.class)) {
validatorConfigs.addAll(buildValidatorConfigs(clazz.getSuperclass(), context, checkFile, checked));
}
}
// look for validators for implemented interfaces
Class[] interfaces = clazz.getInterfaces();
for (Class anInterface1 : interfaces) {
if (checked.contains(anInterface1.getName())) {
continue;
}
validatorConfigs.addAll(buildClassValidatorConfigs(anInterface1, checkFile));
if (context != null) {
validatorConfigs.addAll(buildAliasValidatorConfigs(anInterface1, context, checkFile));
}
checked.add(anInterface1.getName());
}
validatorConfigs.addAll(buildClassValidatorConfigs(clazz, checkFile));
if (context != null) {
validatorConfigs.addAll(buildAliasValidatorConfigs(clazz, context, checkFile));
}
checked.add(clazz.getName());
return validatorConfigs;
}
这里的参数context就是方法名。
validatorConfigs.addAll(buildClassValidatorConfigs(clazz, checkFile));这部会把整个类的方法的注解也加入配置集合中。基本上没找到可以入手的地方。
注解,既然它是针对注解的,那我就再写一个注解。
解压出xwork-tiger.jar包的源码。
添加注解类
package com.opensymphony.xwork.validator.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 定义方法级别的注解配置
*
* @author ANSHU87
*
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodValidation {
}
这个注解类只能写在class上面。
添加类
package com.opensymphony.xwork.validator;
import java.lang.annotation.Annotation;
public class ConfigState {
private boolean methodValidation = false; //是否开启方法注解
private Annotation[] annotations; //类的注解信息
public boolean isMethodValidation() {
return methodValidation;
}
public void setMethodValidation(boolean methodValidation) {
this.methodValidation = methodValidation;
}
public Annotation[] getAnnotations() {
return annotations;
}
public void setAnnotations(Annotation[] annotations) {
this.annotations = annotations;
}
}
用于保存获取到的class注解配置信息。
修改源码包的类
AnnotationValidationConfigurationBuilder
private static List<ValidatorConfig> processAnnotations(Object o) 方法修改如下
@SuppressWarnings("unchecked")
private static List<ValidatorConfig> processAnnotations(Object o,
String context, ConfigState configState) {
List<ValidatorConfig> result = new ArrayList<ValidatorConfig>();
String fieldName = null;
Annotation[] annotations = null;
if (o instanceof Class) {
Class clazz = (Class) o;
annotations = clazz.getAnnotations();
configState.setAnnotations(annotations);
}
if (o instanceof Method) {
Method method = (Method) o;
// 开启了方法级别验证
if (configState.isMethodValidation()) {
if(context != null && !context.equals("")){
if (!context.equals(method.getName())) {
return result;
}
}
}
fieldName = resolvePropertyName(method);
annotations = method.getAnnotations();
}
if (annotations != null) {
for (Annotation a : annotations) {
// Process collection of custom validations
if (a instanceof Validations) {
processValidationAnnotation(a, fieldName, result);
}
// Process single custom validator
if (a instanceof Validation) {
Validation v = (Validation) a;
if (v.validations() != null) {
for (Validations val : v.validations()) {
processValidationAnnotation(val, fieldName, result);
}
}
}
// Process single custom validator
else if (a instanceof ExpressionValidator) {
ExpressionValidator v = (ExpressionValidator) a;
ValidatorConfig temp = processExpressionValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process single custom validator
else if (a instanceof CustomValidator) {
CustomValidator v = (CustomValidator) a;
ValidatorConfig temp = processCustomValidatorAnnotation(v,
fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process ConversionErrorFieldValidator
else if (a instanceof ConversionErrorFieldValidator) {
ConversionErrorFieldValidator v = (ConversionErrorFieldValidator) a;
ValidatorConfig temp = processConversionErrorFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process DateRangeFieldValidator
else if (a instanceof DateRangeFieldValidator) {
DateRangeFieldValidator v = (DateRangeFieldValidator) a;
ValidatorConfig temp = processDateRangeFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process EmailValidator
else if (a instanceof EmailValidator) {
EmailValidator v = (EmailValidator) a;
ValidatorConfig temp = processEmailValidatorAnnotation(v,
fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process FieldExpressionValidator
else if (a instanceof FieldExpressionValidator) {
FieldExpressionValidator v = (FieldExpressionValidator) a;
ValidatorConfig temp = processFieldExpressionValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process IntRangeFieldValidator
else if (a instanceof IntRangeFieldValidator) {
IntRangeFieldValidator v = (IntRangeFieldValidator) a;
ValidatorConfig temp = processIntRangeFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process DoubleRangeFieldValidator
else if (a instanceof DoubleRangeFieldValidator) {
DoubleRangeFieldValidator v = (DoubleRangeFieldValidator) a;
ValidatorConfig temp = processDoubleRangeFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process RequiredFieldValidator
else if (a instanceof RequiredFieldValidator) {
RequiredFieldValidator v = (RequiredFieldValidator) a;
ValidatorConfig temp = processRequiredFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process RequiredStringValidator
else if (a instanceof RequiredStringValidator) {
RequiredStringValidator v = (RequiredStringValidator) a;
ValidatorConfig temp = processRequiredStringValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process StringLengthFieldValidator
else if (a instanceof StringLengthFieldValidator) {
StringLengthFieldValidator v = (StringLengthFieldValidator) a;
ValidatorConfig temp = processStringLengthFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process UrlValidator
else if (a instanceof UrlValidator) {
UrlValidator v = (UrlValidator) a;
ValidatorConfig temp = processUrlValidatorAnnotation(v,
fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process VisitorFieldValidator
else if (a instanceof VisitorFieldValidator) {
VisitorFieldValidator v = (VisitorFieldValidator) a;
ValidatorConfig temp = processVisitorFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process RegexFieldValidator
else if (a instanceof RegexFieldValidator) {
RegexFieldValidator v = (RegexFieldValidator) a;
ValidatorConfig temp = processRegexFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process StringRegexValidator
else if (a instanceof StringRegexValidator) {
StringRegexValidator v = (StringRegexValidator) a;
ValidatorConfig temp = processStringRegexValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// 判断是否开启了方法级别
else if (a instanceof MethodValidation) {
configState.setMethodValidation(true);
}
}
}
return result;
}
public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(
Class aClass)方法修改如下
@SuppressWarnings("unchecked")
//添加context方法,存放调用的上下文
public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(
Class aClass, String context) {
List<ValidatorConfig> result = new ArrayList<ValidatorConfig>();
ConfigState configState = new ConfigState(); //保存配置信息
List<ValidatorConfig> temp = processAnnotations(aClass, context,
configState);
if (temp != null) {
result.addAll(temp);
}
Method[] methods = aClass.getDeclaredMethods();
if (methods != null) {
for (Method method : methods) {
temp = processAnnotations(method, context, configState);
if (temp != null) {
result.addAll(temp);
}
}
}
return result;
}
修改源码包的
AnnotationActionValidatorManager类,把所有调用public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(
Class aClass)方法的代码,改成调用新的方法,public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(
Class aClass, String context)。
测试
刚才的action 在class前面添加注解
@MethodValidation
public class DownloadlibraryAction extends ActionSupport
4个文件代码
http://dl.iteye.com/topics/download/4142f2ff-2d36-3c9e-a004-ba35d3d0e275
http://www.opensymphony.com/webwork/wikidocs/Validation%20Annotation.html
WEBWORK中需要加入包xwork-tiger
一般有两种注解法
1.直接注在get方法上面,像这种
@RequiredStringValidator(message="Supply password")
public String getPassword() {
return password;
}
2.注在方法上面,like
@Validations(
requiredStrings={
@RequiredStringValidator(fieldName="username",message="用户名不能为空!" key="i18n,國際化功能"),
@RequiredStringValidator(fieldName="telNum",message="电话号码不能为空!")
},
regexFields={@RegexFieldValidator(fieldName="telNum",expression="^(\\+86|0|1)\\d{10,11}$",
message="电话号码格式不正确!")}
)
package com.gumtreenet.company.action;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Timestamp;
import java.util.List;
import com.gumtreenet.company.base.Page;
import com.gumtreenet.company.service.IDdSubService;
import com.gumtreenet.company.service.IDownloadlibraryService;
import com.gumtreenet.company.vo.Downloadlibrary;
import com.gumtreenet.company.vo.Resume;
import com.opensymphony.webwork.dispatcher.json.JSONObject;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork.validator.annotations.Validations;
/**
* 附件action
*
* @author 李义星
* @version 0.1
* @datetime 2011-03-22
*/
public class DownloadlibraryAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private final static String DDMAINNAME = "下载类型";
private Downloadlibrary downlib;
private IDownloadlibraryService downService;
private IDdSubService ddSubService;
private FileInputStream downIn;
private File down;
private String downFileName;
private String downContentType;
private Page page;
private JSONObject jsonObject;
private Timestamp start;
private Timestamp end;
private List<Resume> findDowns;
public Downloadlibrary getDownlib() {
return downlib;
}
public void setDownlib(Downloadlibrary downlib) {
this.downlib = downlib;
}
public IDownloadlibraryService getDownService() {
return downService;
}
public void setDownService(IDownloadlibraryService downService) {
this.downService = downService;
}
public IDdSubService getDdSubService() {
return ddSubService;
}
public void setDdSubService(IDdSubService ddSubService) {
this.ddSubService = ddSubService;
}
public FileInputStream getDownIn() {
return downIn;
}
public void setDownIn(FileInputStream downIn) {
this.downIn = downIn;
}
public File getDown() {
return down;
}
public void setDown(File down) {
this.down = down;
}
public String getDownFileName() {
return downFileName;
}
public void setDownFileName(String downFileName) {
this.downFileName = downFileName;
}
public String getDownContentType() {
return downContentType;
}
public void setDownContentType(String downContentType) {
this.downContentType = downContentType;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public JSONObject getJsonObject() {
return jsonObject;
}
public void setJsonObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
}
public Timestamp getStart() {
return start;
}
public void setStart(Timestamp start) {
this.start = start;
}
public Timestamp getEnd() {
return end;
}
public void setEnd(Timestamp end) {
this.end = end;
}
public List<Resume> getFindDowns() {
return findDowns;
}
public void setFindDowns(List<Resume> findDowns) {
this.findDowns = findDowns;
}
@Validations(requiredStrings = {
@RequiredStringValidator(fieldName = "downFileName", message = "请输入附件标题"),
@RequiredStringValidator(fieldName = "downFileName", message = "请输入附件") })
public String addDown() {
return SUCCESS;
}
@Validations(requiredStrings = {
@RequiredStringValidator(fieldName = "downFileName", message = "请1"),
@RequiredStringValidator(fieldName = "downFileName", message = "") })
public String deleteDown() {
return SUCCESS;
}
}
但无论是哪种注解,默认情况下都是对这个action的所有方法进行验证。
如上面的图,我虽然给deleteDown和addDown两个方法都写了对应的验证注解,但是结果是访问delete的时候,add的验证也起作用了。
在struts2中好解决
<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
struts2中这个验证器是支持按方法进行区分注解的。
只要在
<!--开启方法级别的验证-->
validation的配置上加入validateAnnotatedMethodOnly,值是true
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
<param name="validateAnnotatedMethodOnly">true</param>
</interceptor-ref>
但是webwork是不支持的。
webwork默认是使用xwork1的。而xwork1的注解验证需要xwork-tiger.jar的支持。
我对其进行的跟踪。发现它构建注解验证配置的是在类com.opensymphony.xwork.validator.AnnotationValidationConfigurationBuilder的静态方法
public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(Class aClass) 从参数中可以看出来,它是不支持方法级别的验证的。
因此必须对这个类进行改进。
public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(Class aClass) {
List<ValidatorConfig> result = new ArrayList<ValidatorConfig>();
List<ValidatorConfig> temp = processAnnotations(aClass);
if (temp != null) {
result.addAll(temp);
}
Method[] methods = aClass.getDeclaredMethods();
if ( methods != null ) {
for (Method method : methods) {
temp = processAnnotations(method);
if (temp != null) {
result.addAll(temp);
}
}
}
return result;
}
它的上级调用代码(堆栈)是:
protected List<ValidatorConfig> buildClassValidatorConfigs(Class aClass, boolean checkFile) {
String fileName = aClass.getName().replace('.', '/') + VALIDATION_CONFIG_SUFFIX;
List<ValidatorConfig> result = new ArrayList<ValidatorConfig>(loadFile(fileName, aClass, checkFile));
List<ValidatorConfig> annotationResult = new ArrayList<ValidatorConfig>(AnnotationValidationConfigurationBuilder.buildAnnotationClassValidatorConfigs(aClass));
result.addAll(annotationResult);
return result;
}
两部分都可以看出,它并没有把调用的实际方法传进来,因此可以知道它不支持方法级别的注解。
再往上一级调用堆栈是
private List<ValidatorConfig> buildValidatorConfigs(Class clazz, String context, boolean checkFile, Set checked) {
List<ValidatorConfig> validatorConfigs = new ArrayList<ValidatorConfig>();
if (checked == null) {
checked = new TreeSet<String>();
} else if (checked.contains(clazz.getName())) {
return validatorConfigs;
}
if (clazz.isInterface()) {
Class[] interfaces = clazz.getInterfaces();
for (Class anInterface : interfaces) {
validatorConfigs.addAll(buildValidatorConfigs(anInterface, context, checkFile, checked));
}
} else {
if (!clazz.equals(Object.class)) {
validatorConfigs.addAll(buildValidatorConfigs(clazz.getSuperclass(), context, checkFile, checked));
}
}
// look for validators for implemented interfaces
Class[] interfaces = clazz.getInterfaces();
for (Class anInterface1 : interfaces) {
if (checked.contains(anInterface1.getName())) {
continue;
}
validatorConfigs.addAll(buildClassValidatorConfigs(anInterface1, checkFile));
if (context != null) {
validatorConfigs.addAll(buildAliasValidatorConfigs(anInterface1, context, checkFile));
}
checked.add(anInterface1.getName());
}
validatorConfigs.addAll(buildClassValidatorConfigs(clazz, checkFile));
if (context != null) {
validatorConfigs.addAll(buildAliasValidatorConfigs(clazz, context, checkFile));
}
checked.add(clazz.getName());
return validatorConfigs;
}
这里的参数context就是方法名。
validatorConfigs.addAll(buildClassValidatorConfigs(clazz, checkFile));
这个方法是针对全局的验证配置加载
validatorConfigs.addAll(buildAliasValidatorConfigs(clazz, context, checkFile));这个是针对方法级别的配置加载,但是不支持注解。
由于http://www.opensymphony.com/webwork/wikidocs/J2SE%205%20Support.html
http://www.opensymphony.com/webwork/wikidocs/Validation%20Annotation.html
WEBWORK中需要加入包xwork-tiger
一般有两种注解法
1.直接注在get方法上面,像这种
@RequiredStringValidator(message="Supply password")
public String getPassword() {
return password;
}
2.注在方法上面,like
@Validations(
requiredStrings={
@RequiredStringValidator(fieldName="username",message="用户名不能为空!" key="i18n,國際化功能"),
@RequiredStringValidator(fieldName="telNum",message="电话号码不能为空!")
},
regexFields={@RegexFieldValidator(fieldName="telNum",expression="^(\\+86|0|1)\\d{10,11}$",
message="电话号码格式不正确!")}
)
package com.gumtreenet.company.action;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Timestamp;
import java.util.List;
import com.gumtreenet.company.base.Page;
import com.gumtreenet.company.service.IDdSubService;
import com.gumtreenet.company.service.IDownloadlibraryService;
import com.gumtreenet.company.vo.Downloadlibrary;
import com.gumtreenet.company.vo.Resume;
import com.opensymphony.webwork.dispatcher.json.JSONObject;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork.validator.annotations.Validations;
/**
* 附件action
*
* @author 李义星
* @version 0.1
* @datetime 2011-03-22
*/
public class DownloadlibraryAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private final static String DDMAINNAME = "下载类型";
private Downloadlibrary downlib;
private IDownloadlibraryService downService;
private IDdSubService ddSubService;
private FileInputStream downIn;
private File down;
private String downFileName;
private String downContentType;
private Page page;
private JSONObject jsonObject;
private Timestamp start;
private Timestamp end;
private List<Resume> findDowns;
public Downloadlibrary getDownlib() {
return downlib;
}
public void setDownlib(Downloadlibrary downlib) {
this.downlib = downlib;
}
public IDownloadlibraryService getDownService() {
return downService;
}
public void setDownService(IDownloadlibraryService downService) {
this.downService = downService;
}
public IDdSubService getDdSubService() {
return ddSubService;
}
public void setDdSubService(IDdSubService ddSubService) {
this.ddSubService = ddSubService;
}
public FileInputStream getDownIn() {
return downIn;
}
public void setDownIn(FileInputStream downIn) {
this.downIn = downIn;
}
public File getDown() {
return down;
}
public void setDown(File down) {
this.down = down;
}
public String getDownFileName() {
return downFileName;
}
public void setDownFileName(String downFileName) {
this.downFileName = downFileName;
}
public String getDownContentType() {
return downContentType;
}
public void setDownContentType(String downContentType) {
this.downContentType = downContentType;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public JSONObject getJsonObject() {
return jsonObject;
}
public void setJsonObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
}
public Timestamp getStart() {
return start;
}
public void setStart(Timestamp start) {
this.start = start;
}
public Timestamp getEnd() {
return end;
}
public void setEnd(Timestamp end) {
this.end = end;
}
public List<Resume> getFindDowns() {
return findDowns;
}
public void setFindDowns(List<Resume> findDowns) {
this.findDowns = findDowns;
}
@Validations(requiredStrings = {
@RequiredStringValidator(fieldName = "downFileName", message = "请输入附件标题"),
@RequiredStringValidator(fieldName = "downFileName", message = "请输入附件") })
public String addDown() {
return SUCCESS;
}
@Validations(requiredStrings = {
@RequiredStringValidator(fieldName = "downFileName", message = "请1"),
@RequiredStringValidator(fieldName = "downFileName", message = "") })
public String deleteDown() {
return SUCCESS;
}
}
但无论是哪种注解,默认情况下都是对这个action的所有方法进行验证。
如上面的图,我虽然给deleteDown和addDown两个方法都写了对应的验证注解,但是结果是访问delete的时候,add的验证也起作用了。
在struts2中好解决
<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
struts2中这个验证器是支持按方法进行区分注解的。
只要在
<!--开启方法级别的验证-->
validation的配置上加入validateAnnotatedMethodOnly,值是true
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
<param name="validateAnnotatedMethodOnly">true</param>
</interceptor-ref>
但是webwork是不支持的。
webwork默认是使用xwork1的。而xwork1的注解验证需要xwork-tiger.jar的支持。
我对其进行的跟踪。发现它构建注解验证配置的是在类com.opensymphony.xwork.validator.AnnotationValidationConfigurationBuilder的静态方法
public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(Class aClass) 从参数中可以看出来,它是不支持方法级别的验证的。
因此必须对这个类进行改进。
public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(Class aClass) {
List<ValidatorConfig> result = new ArrayList<ValidatorConfig>();
List<ValidatorConfig> temp = processAnnotations(aClass);
if (temp != null) {
result.addAll(temp);
}
Method[] methods = aClass.getDeclaredMethods();
if ( methods != null ) {
for (Method method : methods) {
temp = processAnnotations(method);
if (temp != null) {
result.addAll(temp);
}
}
}
return result;
}
它的上级调用代码(堆栈)是:
protected List<ValidatorConfig> buildClassValidatorConfigs(Class aClass, boolean checkFile) {
String fileName = aClass.getName().replace('.', '/') + VALIDATION_CONFIG_SUFFIX;
List<ValidatorConfig> result = new ArrayList<ValidatorConfig>(loadFile(fileName, aClass, checkFile));
List<ValidatorConfig> annotationResult = new ArrayList<ValidatorConfig>(AnnotationValidationConfigurationBuilder.buildAnnotationClassValidatorConfigs(aClass));
result.addAll(annotationResult);
return result;
}
两部分都可以看出,它并没有把调用的实际方法传进来,因此可以知道它不支持方法级别的注解。
再往上一级调用堆栈是
private List<ValidatorConfig> buildValidatorConfigs(Class clazz, String context, boolean checkFile, Set checked) {
List<ValidatorConfig> validatorConfigs = new ArrayList<ValidatorConfig>();
if (checked == null) {
checked = new TreeSet<String>();
} else if (checked.contains(clazz.getName())) {
return validatorConfigs;
}
if (clazz.isInterface()) {
Class[] interfaces = clazz.getInterfaces();
for (Class anInterface : interfaces) {
validatorConfigs.addAll(buildValidatorConfigs(anInterface, context, checkFile, checked));
}
} else {
if (!clazz.equals(Object.class)) {
validatorConfigs.addAll(buildValidatorConfigs(clazz.getSuperclass(), context, checkFile, checked));
}
}
// look for validators for implemented interfaces
Class[] interfaces = clazz.getInterfaces();
for (Class anInterface1 : interfaces) {
if (checked.contains(anInterface1.getName())) {
continue;
}
validatorConfigs.addAll(buildClassValidatorConfigs(anInterface1, checkFile));
if (context != null) {
validatorConfigs.addAll(buildAliasValidatorConfigs(anInterface1, context, checkFile));
}
checked.add(anInterface1.getName());
}
validatorConfigs.addAll(buildClassValidatorConfigs(clazz, checkFile));
if (context != null) {
validatorConfigs.addAll(buildAliasValidatorConfigs(clazz, context, checkFile));
}
checked.add(clazz.getName());
return validatorConfigs;
}
这里的参数context就是方法名。
validatorConfigs.addAll(buildClassValidatorConfigs(clazz, checkFile));这部会把整个类的方法的注解也加入配置集合中。基本上没找到可以入手的地方。
注解,既然它是针对注解的,那我就再写一个注解。
解压出xwork-tiger.jar包的源码。
添加注解类
package com.opensymphony.xwork.validator.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 定义方法级别的注解配置
*
* @author ANSHU87
*
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodValidation {
}
这个注解类只能写在class上面。
添加类
package com.opensymphony.xwork.validator;
import java.lang.annotation.Annotation;
public class ConfigState {
private boolean methodValidation = false; //是否开启方法注解
private Annotation[] annotations; //类的注解信息
public boolean isMethodValidation() {
return methodValidation;
}
public void setMethodValidation(boolean methodValidation) {
this.methodValidation = methodValidation;
}
public Annotation[] getAnnotations() {
return annotations;
}
public void setAnnotations(Annotation[] annotations) {
this.annotations = annotations;
}
}
用于保存获取到的class注解配置信息。
修改源码包的类
AnnotationValidationConfigurationBuilder
private static List<ValidatorConfig> processAnnotations(Object o) 方法修改如下
@SuppressWarnings("unchecked")
private static List<ValidatorConfig> processAnnotations(Object o,
String context, ConfigState configState) {
List<ValidatorConfig> result = new ArrayList<ValidatorConfig>();
String fieldName = null;
Annotation[] annotations = null;
if (o instanceof Class) {
Class clazz = (Class) o;
annotations = clazz.getAnnotations();
configState.setAnnotations(annotations);
}
if (o instanceof Method) {
Method method = (Method) o;
// 开启了方法级别验证
if (configState.isMethodValidation()) {
if(context != null && !context.equals("")){
if (!context.equals(method.getName())) {
return result;
}
}
}
fieldName = resolvePropertyName(method);
annotations = method.getAnnotations();
}
if (annotations != null) {
for (Annotation a : annotations) {
// Process collection of custom validations
if (a instanceof Validations) {
processValidationAnnotation(a, fieldName, result);
}
// Process single custom validator
if (a instanceof Validation) {
Validation v = (Validation) a;
if (v.validations() != null) {
for (Validations val : v.validations()) {
processValidationAnnotation(val, fieldName, result);
}
}
}
// Process single custom validator
else if (a instanceof ExpressionValidator) {
ExpressionValidator v = (ExpressionValidator) a;
ValidatorConfig temp = processExpressionValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process single custom validator
else if (a instanceof CustomValidator) {
CustomValidator v = (CustomValidator) a;
ValidatorConfig temp = processCustomValidatorAnnotation(v,
fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process ConversionErrorFieldValidator
else if (a instanceof ConversionErrorFieldValidator) {
ConversionErrorFieldValidator v = (ConversionErrorFieldValidator) a;
ValidatorConfig temp = processConversionErrorFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process DateRangeFieldValidator
else if (a instanceof DateRangeFieldValidator) {
DateRangeFieldValidator v = (DateRangeFieldValidator) a;
ValidatorConfig temp = processDateRangeFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process EmailValidator
else if (a instanceof EmailValidator) {
EmailValidator v = (EmailValidator) a;
ValidatorConfig temp = processEmailValidatorAnnotation(v,
fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process FieldExpressionValidator
else if (a instanceof FieldExpressionValidator) {
FieldExpressionValidator v = (FieldExpressionValidator) a;
ValidatorConfig temp = processFieldExpressionValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process IntRangeFieldValidator
else if (a instanceof IntRangeFieldValidator) {
IntRangeFieldValidator v = (IntRangeFieldValidator) a;
ValidatorConfig temp = processIntRangeFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process DoubleRangeFieldValidator
else if (a instanceof DoubleRangeFieldValidator) {
DoubleRangeFieldValidator v = (DoubleRangeFieldValidator) a;
ValidatorConfig temp = processDoubleRangeFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process RequiredFieldValidator
else if (a instanceof RequiredFieldValidator) {
RequiredFieldValidator v = (RequiredFieldValidator) a;
ValidatorConfig temp = processRequiredFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process RequiredStringValidator
else if (a instanceof RequiredStringValidator) {
RequiredStringValidator v = (RequiredStringValidator) a;
ValidatorConfig temp = processRequiredStringValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process StringLengthFieldValidator
else if (a instanceof StringLengthFieldValidator) {
StringLengthFieldValidator v = (StringLengthFieldValidator) a;
ValidatorConfig temp = processStringLengthFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process UrlValidator
else if (a instanceof UrlValidator) {
UrlValidator v = (UrlValidator) a;
ValidatorConfig temp = processUrlValidatorAnnotation(v,
fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process VisitorFieldValidator
else if (a instanceof VisitorFieldValidator) {
VisitorFieldValidator v = (VisitorFieldValidator) a;
ValidatorConfig temp = processVisitorFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process RegexFieldValidator
else if (a instanceof RegexFieldValidator) {
RegexFieldValidator v = (RegexFieldValidator) a;
ValidatorConfig temp = processRegexFieldValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// Process StringRegexValidator
else if (a instanceof StringRegexValidator) {
StringRegexValidator v = (StringRegexValidator) a;
ValidatorConfig temp = processStringRegexValidatorAnnotation(
v, fieldName);
if (temp != null) {
result.add(temp);
}
}
// 判断是否开启了方法级别
else if (a instanceof MethodValidation) {
configState.setMethodValidation(true);
}
}
}
return result;
}
public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(
Class aClass)方法修改如下
@SuppressWarnings("unchecked")
//添加context方法,存放调用的上下文
public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(
Class aClass, String context) {
List<ValidatorConfig> result = new ArrayList<ValidatorConfig>();
ConfigState configState = new ConfigState(); //保存配置信息
List<ValidatorConfig> temp = processAnnotations(aClass, context,
configState);
if (temp != null) {
result.addAll(temp);
}
Method[] methods = aClass.getDeclaredMethods();
if (methods != null) {
for (Method method : methods) {
temp = processAnnotations(method, context, configState);
if (temp != null) {
result.addAll(temp);
}
}
}
return result;
}
修改源码包的
AnnotationActionValidatorManager类,把所有调用public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(
Class aClass)方法的代码,改成调用新的方法,public static List<ValidatorConfig> buildAnnotationClassValidatorConfigs(
Class aClass, String context)。
测试
刚才的action 在class前面添加注解
@MethodValidation
public class DownloadlibraryAction extends ActionSupport
4个文件代码
http://dl.iteye.com/topics/download/4142f2ff-2d36-3c9e-a004-ba35d3d0e275
发表评论
-
jsp 中 自定义Map 调用国际化
2013-01-30 11:10 1174在jsp中,自动以map,如 #{"1": ... -
s:set 定义一个map并从map取出vlaue
2012-12-19 11:30 11471.方法一,先使用set <td><s:s ... -
标签的样式(去除tr等布局元素)
2012-11-15 17:36 1300struts2中theme属性包括xhtml,html,sim ... -
时间类型,精度问题(Timestamp,Date)
2011-04-08 16:38 5225经过调试,发现最后转换器会进入com.opensymphony ... -
freemarker
2011-03-27 23:37 14281.freemarker.properties locale= ... -
ognl
2011-03-27 19:03 1248ognl是object graph(图表) navigaton ... -
文件上传,下载
2011-03-27 13:32 1361关于和文件上传的配置,查看webwork.properties ... -
异常处理
2011-03-26 17:48 1067在默认配置中 <interceptor-stack na ... -
action配置
2011-03-26 17:26 1197普通配置不说明 动态方法调用: 如我们使用同一个表单,当用户 ... -
访问servlet api
2011-03-26 16:44 1085首先获取ActionContext ActionContext ... -
处理结果(result)和默认action
2011-03-26 16:39 1491默认action,在package元素 ... -
动态方法调用
2011-03-22 23:53 1042struts.enable.DynamicMethodInvo ... -
转换器的错误处理
2011-03-22 22:28 1170conversionError拦截器。如果有错误,回到了inp ... -
类转换器
2011-03-22 21:40 968如我们通过在action定义user,然后user.name, ... -
国际化
2011-03-22 21:27 1293保存一个文件到classpath下面,如message.pro ... -
验证框架
2011-03-22 21:21 1142重写validate方法,页面中使用了ww:form标签,已经 ... -
Array、List、Map
2011-03-22 18:41 1433数组方式,如我们有两 ... -
webwork.properties配置文件的属性
2011-03-16 16:44 13481.webwork.configuration=com.ope ... -
主要配置文件
2011-03-16 15:32 1184文件可选位置(相对于webapp)用途web.xml否/WEB ... -
webwork的过滤器
2011-03-15 10:40 1902在webwork的架构中,标准过滤器推荐从ActionCont ...
相关推荐
Java 自定义注解验证是Java开发中的一个重要特性,它允许开发者创建自己的元数据,以便在编译时或运行时对代码进行验证和处理。自定义注解为代码提供了额外的信息,使得程序更具可读性、可维护性和灵活性。在本案例...
在Spring MVC框架中,注解验证是一个非常重要的功能,它允许开发者通过在控制器方法的参数上添加特定的注解来实现数据验证。这种方式简洁且高效,使得代码更加整洁,易于维护。下面我们将深入探讨Spring MVC中的注解...
"java token验证和注解方式放行"的主题涉及了两个关键概念:Token验证和基于注解的权限管理。Token通常用于验证用户身份,防止未授权的访问。下面我们将深入探讨这两个主题。 首先,Token验证是一种常见的身份验证...
本项目是基于Java的简单javabean注解验证框架设计源码,包含25个文件,其中包括20个Java源文件、2个XML文件、1个gitignore文件、1个Markdown文档和1个Properties文件。该项目是一个简单的javabean注解验证框架,通过...
springboot 控制器以实体形式接收参数,以注解的方式对入参进行校验,使用参考地址https://blog.csdn.net/confused_kitten/article/details/111308066
本篇将详细介绍Struts2的极速表单验证框架,特别是其注解方式的使用。 ### 1. 表单验证的重要性 在Web应用程序中,验证用户输入的数据是必不可少的,因为不正确的数据可能导致业务逻辑错误,甚至安全漏洞。Struts2...
Spring 注解验证@NotNull 等使用方法 Spring 框架提供了一种基于注解的验证机制,通过使用注解可以对 JavaBean 的属性进行验证。这种机制可以在 Controller 层、Service 层、DAO 层等任何需要验证数据的地方使用。...
详细说明可以看MoneyActivity.class的注释。 当在 控件初始化前 调用Validate.reg(this)方法,则不会添加任何规则/监听事件 20170914 :有网友说@Shield不清楚有什么用,所以增加一个界面ShieldDemoActivity.java,...
本文将详细探讨"Excel导入动态校验,自定义注解动态校验"这一主题,旨在帮助你理解和掌握如何在处理Excel数据时进行有效的验证和校验。 首先,Excel导入动态校验是指在将Excel数据导入到系统或数据库时,对数据进行...
"使用SpringMVC的@Validated注解验证的实现" @Validated注解是SpringMVC中的一种验证注解,用于验证 Bean 中的属性是否满足一定的条件。该注解可以与其他验证注解如 @NotNull、@Size 等结合使用,实现复杂的验证...
在Java编程中,自定义注解是一种强大的工具,可以用于数据验证、元数据标注和代码生成等多种场景。本文将深入探讨如何在Java中通过自定义注解进行数据验证,这有助于提升代码的可读性和可维护性,同时也能实现验证...
在Struts2中,除了XML配置外,还可以使用注解进行字段验证。通过在ActionForm的属性上添加注解,如`@NotBlank`,`@Size`等,可以简化配置并提高代码可读性。 8. **Struts2插件**: Struts2提供了更丰富的验证插件...
使用注解验证则更简洁,可以直接在Action类的字段上添加注解,如@NotEmpty、@Size等,定义验证规则。这样,当Action执行时,Struts2会自动进行验证。 接下来是登录注册的具体实现。登录时,Action会接收到HTTP请求...
### 注解+反射实现自定义验证工具 #### 一、引言 在软件开发过程中,数据验证是一项必不可少的任务。为了确保程序的健壮性和数据的准确性,开发者常常需要对输入的数据进行各种各样的检查,例如长度限制、格式验证...
Blazor的本地化数据注释验证器组件 概要 这是Blazor的数据注释验证器组件,用于基于Microsoft.Extensions.Localization本地化验证错误消息。 现场演示-https : [注意] 目前,此组件可以在“服务器端Blazor”上正常...
注释验证器是ZF2模块,用于在其生命周期回调(事件)期间验证Doctrine实体。 通过在项目中使用注释验证器,可以提高生产率。 同时,参与项目的初级开发人员不必担心验证,因为它是在实体中进行的。 您还可以确保...
Spring还提供了`ConstraintValidator`接口,用于自定义注解验证器,这样可以利用Java的类型系统来声明和实现验证规则。 **验证框架的核心概念** 1. **验证规则**:这是表单验证的基础,定义了用户输入必须满足的...
在Struts2中,注解(Annotation)验证是一个强大的特性,它允许开发者在Action类的方法参数上直接定义验证规则,简化了传统XML配置的复杂性。这篇博文将深入探讨如何使用Struts2的注解进行数据验证。 首先,我们...
可以使用`@Validated`和`@Field`注解在Action类或字段上直接定义验证规则,简化了配置文件的使用。 6. **Struts2的拦截器** Struts2中的拦截器(Interceptor)机制也支持数据验证。例如,使用`...