Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
新增加了使用 nutz 表达式引擎进行验证的方法。 git-svn-id: http://nutzmore.googlecode.com/svn/trunk@35 967cf8f2-54b7-3246-d019-9e60b16c5941
- Loading branch information
Showing
17 changed files
with
1,271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="src" path="test"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v6.0"/> | ||
<classpathentry kind="lib" path="E:/workspace/nutz-1.b.37/nutz-1.b.37-jdk6.jar"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>NutzValidation</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.nutz.validation; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
/** | ||
* 验证错误信息汇总类 | ||
* | ||
* @author QinerG(QinerG@gmail.com) | ||
*/ | ||
public class Errors { | ||
/* | ||
* 存放错误信息的 map | ||
*/ | ||
private Map<String, String> errorMap = new TreeMap<String, String>(); | ||
|
||
/** | ||
* @return 是否存在验证错误 | ||
*/ | ||
public boolean hasError() { | ||
return errorMap.size() > 0; | ||
} | ||
|
||
/** | ||
* @return 返回存在错误的总数 | ||
*/ | ||
public int errorCount() { | ||
return errorMap.size(); | ||
} | ||
|
||
/** | ||
* 增加一个错误信息 | ||
* | ||
* @param fieldName | ||
* 存在错误的字段名 | ||
* @param errorMessage | ||
* 错误的详细信息 | ||
*/ | ||
public void add(String fieldName, String errorMessage) { | ||
errorMap.put(fieldName, errorMessage); | ||
} | ||
|
||
/** | ||
* 返回错误信息列表 | ||
* @return | ||
*/ | ||
public Collection<String> getErrorsList() { | ||
return errorMap.values(); | ||
} | ||
|
||
/** | ||
* 返回详细的错误信息列表,含验证错误的字段名和提示语 | ||
* @return | ||
*/ | ||
public Map<String, String> getErrorsMap() { | ||
return errorMap; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.nutz.validation; | ||
|
||
import org.nutz.validation.annotation.AnnotationValidation; | ||
|
||
/** | ||
* 验证工具类的接口。 | ||
* | ||
* @see AnnotationValidation | ||
* @author QinerG(QinerG@gmail.com) | ||
*/ | ||
public interface Validation { | ||
|
||
public Errors validate(Object target); | ||
|
||
public void validate(Object target, Errors errors); | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
nutz-validation/src/org/nutz/validation/ValidationInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.nutz.validation; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import org.nutz.aop.interceptor.AbstractMethodInterceptor; | ||
import org.nutz.ioc.loader.annotation.IocBean; | ||
import org.nutz.validation.annotation.AnnotationValidation; | ||
|
||
/** | ||
* 基于注解的验证用拦截器 | ||
* <p> | ||
* 该拦截器主要用于方法参数的验证,要求该方法中必须有一个 Errors 类型的参数(允许为空),当验证完成后会向这个参数赋值 | ||
* | ||
* @author QinerG(QinerG@gmail.com) | ||
*/ | ||
@IocBean(name = "validationInterceptor") | ||
public class ValidationInterceptor extends AbstractMethodInterceptor { | ||
|
||
private static AnnotationValidation av = new AnnotationValidation(); | ||
|
||
/** | ||
* 方法调用前进行拦截,遍历参数进行验证 | ||
*/ | ||
public boolean beforeInvoke(Object obj, Method method, Object... args) { | ||
Errors es = ValidationUtils.checkArgs(method.getParameterTypes(), args); | ||
if (null != es) { | ||
for (Object argsObj : args) { | ||
if (argsObj instanceof Errors) | ||
continue; | ||
av.validate(argsObj, es); | ||
} | ||
} | ||
return true; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
nutz-validation/src/org/nutz/validation/ValidationProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.nutz.validation; | ||
|
||
import org.nutz.mvc.ActionContext; | ||
import org.nutz.mvc.impl.processor.AbstractProcessor; | ||
import org.nutz.validation.annotation.AnnotationValidation; | ||
|
||
/** | ||
* 可用于 MVC 效验的动作链 | ||
* <p> | ||
* 要求 action 参数中必须有一个 Errors 类型的参数(不需要使用 Param 声明)。当验证完成后会向这个参数赋值 | ||
* | ||
* @author QinerG(QinerG@gmail.com) | ||
*/ | ||
public class ValidationProcessor extends AbstractProcessor { | ||
private static AnnotationValidation av = new AnnotationValidation(); | ||
|
||
public void process(ActionContext ac) throws Throwable { | ||
Errors es = ValidationUtils.checkArgs(ac.getMethod() | ||
.getParameterTypes(), ac.getMethodArgs()); | ||
// 参数中没找到 Errors 类型的参数,忽略处理,继续其它动作链处理 | ||
if (null != es) { | ||
for (Object obj : ac.getMethodArgs()) { | ||
av.validate(obj, es); | ||
} | ||
} | ||
doNext(ac); | ||
} | ||
} |
Oops, something went wrong.