国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java自定義注解實現前后臺參數校驗的實例

2019-11-26 13:31:17
字體:
來源:轉載
供稿:網友

其實是可以通過@Constraint來限定自定義注解的方法。

@Constraint(validatedBy = xxxx.class)

下面是我做的 java自定義注解實現前后臺參數校驗 的代碼示例

對這個感興趣的,請好好看,好好學:

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: 自定義注解實現前后臺參數校驗,判斷是否包含非法字符* @author 無名* @date 2016-7-25 下午8:22:58 * @version 1.0 */@Target({ElementType.FIELD, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Constraint(validatedBy = IsValidString.ValidStringChecker.class)@Documentedpublic @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的內部類。

而該內部類實現了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.

實現的isValid方法便是,該接口的校驗方法。

試驗一下效果,在要校驗的實體類字段加上注解:

寫文章頁面,文章標題內加入'<'然后提交:

提交失敗,報500錯誤,說明注解生效:

但這樣還有問題,我的blog網站不能直接打印出報錯信息。還是要搞一個error頁面出來。

這個簡單,web.xml下加入error頁面路徑,然后做一個頁面即可:

<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>

首先介紹些基本概念:

1.java用@interface xx{}定義一個注解。

注解這個東西,其實并不神秘,不過是一種標記,程序運行到標記處,就執行相應邏輯罷了。注解本身即是一個類。

2.注解在定義時,標注一些注解可以表示特定意義:

@Retention(RetentionPolicy.SOURCE) // 注解僅存在于源碼中,在class字節碼文件中不包含

@Retention(RetentionPolicy.CLASS) // 默認的保留策略,注解會在class字節碼文件中存在,但運行時無法獲得

@Retention(RetentionPolicy.RUNTIME) // 注解會在class字節碼文件中存在,在運行時可以通過反射獲取到

(RUNTIME的值得注意下,因為意味著可以反射來獲取)

@Target(ElementType.TYPE) // 接口、類、枚舉、注解

@Target(ElementType.FIELD) // 字段、枚舉的常量

@Target(ElementType.METHOD) // 方法

@Target(ElementType.PARAMETER) // 方法參數

@Target(ElementType.CONSTRUCTOR) // 構造函數

@Target(ElementType.LOCAL_VARIABLE) // 局部變量

@Target(ElementType.ANNOTATION_TYPE) // 注解

@Target(ElementType.PACKAGE) // 包

有一種做法就是在定義注解時加上@Taget(xx)和@Retention(RetentionPolicy.RUNTIME) ,但沒有在注解中寫方法,只是在運行時通過反射機制來獲取注解,然后自己寫相應邏輯(所謂注解解析器)

大概是類似的寫法:

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;}

之后運行時,用反射獲取注解,具體不談。

之前在網上查找這方面技術文章找到的都是這種,給當時的我帶來很大困惑。覺得不是我想要的。

以上這篇java自定義注解實現前后臺參數校驗的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富平县| 余姚市| 苏尼特右旗| 肇庆市| 原阳县| 绵阳市| 彩票| 台湾省| 蓝山县| 平昌县| 新干县| 灵寿县| 墨江| 二手房| 靖边县| 兰考县| 中西区| 大埔县| 河北区| 来凤县| 屏东县| 仙桃市| 彭山县| 建昌县| 漯河市| 崇阳县| 长武县| 长海县| 平武县| 乐清市| 桦南县| 鹤岗市| 泰州市| 廊坊市| 宣化县| 荣昌县| 紫云| 富锦市| 敖汉旗| 敖汉旗| 通渭县|