其實是可以通過@Constraint來限定自定義注解的方法。
@Constraint(validatedBy = xxxx.class)
下面是我做的 java自定義注解實現(xiàn)前后臺參數(shù)校驗 的代碼示例
對這個感興趣的,請好好看,好好學(xué):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package sonn.sonnannotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import javax.validation.Constraint; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import javax.validation.Payload; import sonn.util.StringUtill; /** * @ClassName: IsValidString * @Description: 自定義注解實現(xiàn)前后臺參數(shù)校驗,判斷是否包含非法字符 * @author 無名 * @date 2016-7-25 下午8:22:58 * @version 1.0 */ @Target ({ElementType.FIELD, ElementType.METHOD}) @Retention (RetentionPolicy.RUNTIME) @Constraint (validatedBy = IsValidString.ValidStringChecker. class ) @Documented public @interface IsValidString { String message() default "The string is invalid." ; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; class ValidStringChecker implements ConstraintValidator<IsValidString,String> { @Override public void initialize(IsValidString arg0) { } @Override public boolean isValid(String strValue, ConstraintValidatorContext context) { if (StringUtill.isStringEmpty(strValue)) { return true ; } if (strValue.contains( "<" )) { return false ; } return true ; } } } |
上述代碼,通過@Constraint(validatedBy = IsValidString.ValidStringChecker.class)限定了注解的方法邏輯---該注解類的名為ValidStringChecker的內(nèi)部類。
而該內(nèi)部類實現(xiàn)了ConstraintValidator<IsValidString,String>接口
官方文檔是這樣描述的:
javax.validation
Interface ConstraintValidator<A extends Annotation,T>
•
------------------------------------------------
public interface ConstraintValidator<A extends Annotation,T>Defines the logic to validate a given constraint A for a given object type T.
Implementations must comply to the following restriction:
•T must resolve to a non parameterized type
•or generic parameters of T must be unbounded wildcard types
The annotation SupportedValidationTarget can be put on a ConstraintValidator implementation to mark it as supporting cross-parameter constraints. Check out SupportedValidationTarget and Constraint for more information.
實現(xiàn)的isValid方法便是,該接口的校驗方法。
試驗一下效果,在要校驗的實體類字段加上注解:
寫文章頁面,文章標(biāo)題內(nèi)加入'<'然后提交:
提交失敗,報500錯誤,說明注解生效:
但這樣還有問題,我的blog網(wǎng)站不能直接打印出報錯信息。還是要搞一個error頁面出來。
這個簡單,web.xml下加入error頁面路徑,然后做一個頁面即可:
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
首先介紹些基本概念:
1.java用@interface xx{}定義一個注解。
注解這個東西,其實并不神秘,不過是一種標(biāo)記,程序運行到標(biāo)記處,就執(zhí)行相應(yīng)邏輯罷了。注解本身即是一個類。
2.注解在定義時,標(biāo)注一些注解可以表示特定意義:
@Retention(RetentionPolicy.SOURCE) // 注解僅存在于源碼中,在class字節(jié)碼文件中不包含
@Retention(RetentionPolicy.CLASS) // 默認(rèn)的保留策略,注解會在class字節(jié)碼文件中存在,但運行時無法獲得
@Retention(RetentionPolicy.RUNTIME) // 注解會在class字節(jié)碼文件中存在,在運行時可以通過反射獲取到
(RUNTIME的值得注意下,因為意味著可以反射來獲取)
@Target(ElementType.TYPE) // 接口、類、枚舉、注解
@Target(ElementType.FIELD) // 字段、枚舉的常量
@Target(ElementType.METHOD) // 方法
@Target(ElementType.PARAMETER) // 方法參數(shù)
@Target(ElementType.CONSTRUCTOR) // 構(gòu)造函數(shù)
@Target(ElementType.LOCAL_VARIABLE) // 局部變量
@Target(ElementType.ANNOTATION_TYPE) // 注解
@Target(ElementType.PACKAGE) // 包
有一種做法就是在定義注解時加上@Taget(xx)和@Retention(RetentionPolicy.RUNTIME) ,但沒有在注解中寫方法,只是在運行時通過反射機制來獲取注解,然后自己寫相應(yīng)邏輯(所謂注解解析器)
大概是類似的寫法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Inherited @Target ({ ElementType.FIELD, ElementType.METHOD }) @Retention (RetentionPolicy.RUNTIME) public @interface Validate { public int min() default 1 ; public int max() default 10 ; public boolean isNotNull() default true ; } |
之后運行時,用反射獲取注解,具體不談。
之前在網(wǎng)上查找這方面技術(shù)文章找到的都是這種,給當(dāng)時的我?guī)砗艽罄Щ蟆SX得不是我想要的。
以上這篇java自定義注解實現(xiàn)前后臺參數(shù)校驗的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。