天天看點

Struts2基于Annotation的服務端校驗

在使用Struts2開發時,經常會遇到在服務端Action方法中對資料有效性的校驗(當然任何架構都會遇到),當遇到一大堆屬性需要校驗時就顯得繁瑣,而struts2本身的校驗插件用起來也不是那麼簡單,最近自己就嘗試用Annotation的方式對資料的有效性進行了校驗。

首先簡單介紹下驗證思路:

1、制定校驗的Annotaion,主要針對Field、方法級别

2、Annotation相應的校驗規則

3、采用Struts2中的攔截器進行校驗,在攔截器初始化方法中加載校驗的Annotaion和校驗規則

4、攔截器對請求方法和Action中的Field截取,讀取Field上的Annotaion并采用3中儲存的校驗規則進行校驗

5、将校驗産生的不通過資訊存儲在ActionContext中,在具體的Action擷取并處理消息

接下來看看具體的實作過程,将分為以下幾部分:

1、Annotation和AnnotationChecker

這都是聲明的正常性Annotation,主要作用在對象的Field上,以下是一個最大長度校驗Annotation的聲明:

@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MaxLength {

    public int value() default 0;

}
           

MaxLength的校驗類MaxLengthChecker,這裡約定檢驗類是在Annotation+Checker:

public class MaxLengthChecker implements Checker {

    @Override
    public CheckerResult check(Object value, String fieldName, Annotation annotation) {
        if (null != value) {
            try {
                List<String> maxLeng = new ArrayList<String>();
                MaxLength maxLength = (MaxLength) annotation;
                String strVal = (String) value;
                maxLeng.add(String.valueOf(maxLength.value()));
                if (strVal.trim().length() > maxLength.value()) {
                    return new CheckerResult(fieldName, CheckerMsgConstant.CHECKER_MAXLENGTH_OVERFLOW, maxLeng);
                }
            } catch (Exception e) {
                //轉換為String失敗或其他異常給出提示
                return new CheckerResult(fieldName, CheckerMsgConstant.CHECKER_MAXLENGTH_NOTSTR);
            }
        }
        return null;
    }

}
           

 這裡所有Checker實作接口Checker,方法check作為Annotation的校驗方法,傳回CheckerResult,用來描述在哪個field和相應的失敗規則,如下:

public class CheckerResult implements Serializable {

    private static final long serialVersionUID = 103986447231347145L;
    /** 受檢查字段 */
    private String            field;
    /** 檢查結果錯誤類型 */
    private String            checkType;
    /** 檢查中傳遞的參數(如最大最小值等) */
    private List<String>      values;
           

2、Struts2攔截器初始化加載所有的校驗Annotation

這是校驗的主要方法,繼承struts2中的攔截器,首先在初始化方法中根據指定路徑加載Annotation和AnnotationChecker

/**
     * load check rules
     */
    public void init() {
        try {
            URL packageUrl = CheckerIntercepter.class.getClassLoader().getResource(
                    CKECHER_ANNOTATION_PACKAGE.replaceAll("\\.", "/"));
            for (String file : new File(packageUrl.getFile()).list()) {
                if (!file.endsWith(".class"))
                    continue;
                String checkerName = file.substring(0, file.length() - 6);
                Checker checker = (Checker) Class.forName(CKECHER_PACKAGE + "." + checkerName + "Checker")
                        .newInstance();
                checkerMap.put(checkerName, checker);
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
    }
           

這個會在容器啟動時加載一次,接下來是攔截器的主要方法intercept,主要擷取目前Action和執行的方法。

這裡處理了幾點:

1、如果方法有@Ignore就直接忽略

2、隻處理目前請求參數的annotation

@Override
    @SuppressWarnings("all")
    public String intercept(ActionInvocation invocation) throws Exception {
        Object obj = invocation.getAction();

        //隻處理目前方法的請求參數的校驗
        Set<String> paramSet = reqParams(invocation.getInvocationContext().getParameters());
        if (null == paramSet || paramSet.size() == 0) {
            return invocation.invoke();
        }
        reqParas = new WeakReference<Set<String>>(paramSet);

        if (null != obj) {
            //方式是否ignore
            Method method = obj.getClass().getDeclaredMethod(invocation.getProxy().getMethod(), null);
            boolean ignore = true;
            //方法不能為空,并且忽略預設執行方法(execute)
            if (null != method && !IGNORE_METHOD.equals(method)) {
                //方法是否有Ignore标注
                Annotation[] annotations = method.getAnnotations();
                for (Annotation anno : annotations) {
                    if (IGNORE_ANNOTATION.equals(anno.annotationType().getSimpleName())) {
                        ignore = false;
                        break;
                    }
                }

                if (ignore) {
                    List<CheckerResult> list = populateObject(obj, null);
                    if (null != list && !list.isEmpty()) {
                        //如果有校驗不通過的消息,存儲在ActionContext中供各Action調用
                        invocation.getInvocationContext().put(CheckerMsgConstant.CHECKER, list);
                    }
                }
            }

        }
        return invocation.invoke();
    }
           

3、對Action中的Field中的Annotation進行校驗,并支援作為成員變量的自定義類中Field的校驗。這裡主要是上面的populateObject方法

/**
     * 對目前對象(Action)或Action中的成員變量擷取其Field以及相應的Annotation進行校驗
     * 
     * @param obj
     * @param clazz
     * @return
     * @author robin
     * @date 2012-7-18
     */
    @SuppressWarnings("all")
    private <T> List<CheckerResult> populateObject(Object obj, Class<T> clazz) {
        List<CheckerResult> result = new ArrayList<CheckerResult>();
        Class clz = obj == null ? clazz : obj.getClass();

        //目前對象如果是Action擷取成員變量,如果是作為成員變量的對象,擷取相應的父類
        List<Field> fields = new ArrayList<Field>();
        while (!clz.equals(Object.class)) {
            fields.addAll(Arrays.asList(clz.getDeclaredFields()));
            if (clz.getSuperclass().equals(ActionSupport.class) || clz.equals(Class.class)) {
                break;
            }
            clz = clz.getSuperclass();
        }

        for (Field field : fields) {

            //是否有@Ignore、靜态、接口、數組成員變量忽略
            if (isIgnored(field)) {
                continue;
            }

            //目前請求參數中是否有該Field
            String fieldName = field.getName();
            if (!reqParas.get().contains(fieldName)) {
                continue;
            }

            Object object = null;
            if (null != obj) {
                try {
                    field.setAccessible(true);
                    object = field.get(obj);
                } catch (Exception e) {
                    LOG.error("FAILED get field value", e);
                    //do nothing
                }
            }

            if (isPrimitive(field.getType())) {
                result.addAll(checker(field, object));
            } else {
                result.addAll(populateObject(object, field.getType()));
            }
        }
        return result;
    }
           

這裡primitives是制定的預設直接進行校驗的集合,不過這樣做并不太合理。需要進一步考慮下,這是配置:

/**
     * 判斷是否是原生類型(如果是實體對象需要遞歸調用對其判斷)
     * 
     * @param clazz
     * @return
     * @author robin
     * @date 2012-7-20
     */
    private boolean isPrimitive(@SuppressWarnings("rawtypes") Class clazz) {
        return clazz.isPrimitive() || clazz.equals(String.class) || clazz.equals(Date.class)
                || clazz.equals(Boolean.class) || clazz.equals(Byte.class) || clazz.equals(Character.class)
                || clazz.equals(Double.class) || clazz.equals(Float.class) || clazz.equals(Integer.class)
                || clazz.equals(Long.class) || clazz.equals(Short.class) || clazz.equals(Locale.class)
                || clazz.isEnum();
    }
           

4、剩下的就是checker方法,對Field和該Field的值進行校驗

/**
     * 對目前Field的Annotation進行校驗
     * 
     * @param field
     * @param value
     * @return
     * @author robin
     * @date 2012-7-18
     */
    private List<CheckerResult> checker(Field field, Object value) {
        List<CheckerResult> result = new ArrayList<CheckerResult>();
        Annotation[] annotations = field.getAnnotations();
        for (Annotation anno : annotations) {
            Checker checker = checkerMap.get(anno.annotationType().getSimpleName());
            if (checker != null) {
                CheckerResult check = checker.check(value, field.getName(), anno);
                if (null != check) {
                    result.add(check);
                }
            }
        }
        return result;
    }
           

最後來看看如何使用:

1、需要将目前的攔截器加到項目的default-interceptor-ref如:

<interceptors>
			<interceptor name="checkerIntercepter" class="org.apache.struts2.valid.common.CheckerIntercepter" />
			<interceptor-stack name="s2webDefaultStack">
				<interceptor-ref name="defaultStack"/>
				<interceptor-ref name="checkerIntercepter" />
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="s2webDefaultStack"/>
           

 2、為了友善使用,提供了一個ActionBase方法,可繼承該Action,當然也可以自己處理,因為消息已經在struts2的資料上下文環境中:

List<CheckerResult> result = (List<CheckerResult>) ActionContext.getContext().get(CheckerMsgConstant.CHECKER);
           

 這是ActionBase中獲得消息的一個方法:

@SuppressWarnings("unchecked")
    protected List<String> getValidInfos() {
        List<String> resultList = new ArrayList<String>();
        List<CheckerResult> result = (List<CheckerResult>) ActionContext.getContext().get(CheckerMsgConstant.CHECKER);
        for (CheckerResult checkerResult : result) {
            //注意:第一個參數預設為類型,為message中的key
            //new String[]{/**預設為字段名稱*/, ...(一個或多個參數)}
            resultList.add(getText(checkerResult.getCheckType(), getValidateInfo(checkerResult)));
        }
        return resultList;
    }

    private String[] getValidateInfo(CheckerResult checker) {
        List<String> values = checker.getValues();
        String field = getText(checker.getField());
        if (null != values && values.size() > 0) {
            String[] result = new String[1 + values.size()];
            result[0] = field;
            int index = 1;
            for (String rst : values) {
                result[index] = rst;
                index++;
            }
            return result;
        }
        return new String[] { field };
    }
           

而驗證的錯誤消息聲明如下:

/** {0}長度不能超過{1} */
    public static final String CHECKER_MAXLENGTH_OVERFLOW = "checker.maxlength.overflow";
    /** {0}不是字元 */
    public static final String CHECKER_MAXLENGTH_NOTSTR   = "checker.maxlength.notstr";
           

這些消息的key已經在CheckerResult中的checkType展現,并且可以預設提供一系列的消息,如message.properties中:

checker.maxlength.overflow={0}\u957F\u5EA6\u4E0D\u80FD\u8D85\u8FC7{1}
           

3、在業務Action中,可以這樣:

public class ValiAction extends ValidBaseAction {
    private static final long  serialVersionUID = -5951668645132875324L;

    @Must
    @MaxLength(5)
    private String             username;
    @Must
    private String             password;

    private PageModule<String> pageModule;

    @Ignore
    public String index() {
        return SUCCESS;
    }

    public String save() {
//獲得校驗錯誤消息
        getValidInfos();
           

以上可見:https://github.com/yooodooo/s2valid.git

繼續閱讀