■
strtus2のデフォルトValidationであるConnvertionパッケージ以下を使用した場合、SkipValidationアノテーションが使える。
が、ovalでは使用できない。(使用できるかも知れませんが、ちょっと調べただけでは分からずじまい)
これに不便を感じ、SkipOvalValidationなるものを作成し使用した。
@SkipOvalValidation
package org.framework.ext.oval.validation; import static java.lang.annotation.ElementType.METHOD; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(METHOD) public @interface SkipOvalValidation { }
OvalAnnotationValidationInterceptor
package org.framework.ext.oval.validation; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Collection; import org.apache.struts2.oval.interceptor.OValValidationInterceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.util.AnnotationUtils; public class OvalAnnValidationInterceptor extends OValValidationInterceptor{ protected String doIntercept(ActionInvocation invocation) throws Exception { if (checkSkipValidation(invocation)) return invocation.invoke(); return super.doIntercept(invocation); } protected boolean checkSkipValidation(ActionInvocation invocation) throws Exception { Object action = invocation.getAction(); if (action != null) { Method method = getActionMethod(action.getClass(), invocation.getProxy().getMethod()); Collection<Method> annotatedMethods = AnnotationUtils.getAnnotatedMethods(action.getClass(), SkipOvalValidation.class); if (annotatedMethods.contains(method)) return true; //check if method overwites an annotated method Class clazz = action.getClass().getSuperclass(); while (clazz != null) { annotatedMethods = AnnotationUtils.getAnnotatedMethods(clazz, SkipOvalValidation.class); if (annotatedMethods != null) { for (Method annotatedMethod : annotatedMethods) { if (annotatedMethod.getName().equals(method.getName()) && Arrays.equals(annotatedMethod.getParameterTypes(), method.getParameterTypes()) && Arrays.equals(annotatedMethod.getExceptionTypes(), method.getExceptionTypes())) return true; } } clazz = clazz.getSuperclass(); } } return false; } protected Method getActionMethod(Class actionClass, String methodName) throws NoSuchMethodException { Method method; try { method = actionClass.getMethod(methodName, new Class[0]); } catch (NoSuchMethodException e) { try { String altMethodName = "do" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1); method = actionClass.getMethod(altMethodName, new Class[0]); } catch (NoSuchMethodException e1) { // throw the original one throw e; } } return method; } }
strtus.xml
<package name="ann-oval-default" extends="struts-default"> <interceptors> <interceptor name="annOvalValidation" class="org.framework.ext.oval.validation.OvalAnnValidationInterceptor" /> <interceptor-stack name="ovalAnnotaionStack"> <interceptor-ref name="autowiring" /> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="i18n"/> <interceptor-ref name="prepare"/> <interceptor-ref name="chain"/> <interceptor-ref name="debugging"/> <interceptor-ref name="profiling"/> <interceptor-ref name="scopedModelDriven"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="actionMappingParams"/> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*,^struts\..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <interceptor-ref name="annOvalValidation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack> </interceptors> </package>
TestAction
@Namespace("/") @ParentPackage("ann-oval-default") @InterceptorRefs({ @InterceptorRef(value="ovalAnnotaionStack") }) @Results({ @Result(name="success" , type="dispatcher" , location="success.jsp") }) public class TestAction extends ActionSupport { @NotEmpty private String name; @SkipOvalValidation public String sayName() { return "success"; } public String checkName(){ return "success"; } public String execute() { return "success"; } }
success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="windows-31j"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Insert title here</title> </head> <body> <s:form> <s:fielderror></s:fielderror> name:<s:input name="name" /> <s:submit action="input" method="sayname" value="sayname(skip)"></s:submit> <s:submit action="input" method="check" value="check"></s:submit> </form> </body> </html>
見てわかるように、org.apache.struts2.interceptor.validationの中身を少しアレンジしただけ。
struts-ovalプラグイン担当に提案した方がいいのかなぁ。。。