struts 2デフォルトブロックのprepare


struts 2のstruts-default.xmlにはprepareブロッカーとして名前が定義され、実装クラスはcom.opensymphony.xwork2.interceptor.PrepareInterceptorであり、com.opensymphony.xwork2.Preparableインタフェースのaction呼び出し関連メソッドを実装する役割を果たす.このブロッキングには、alwaysInvokePrepare、firstCallPrepareDoの2つのパラメータがあります.両方のタイプはbooleanで、デフォルト値はtrue、falseです.
このブロッカーのコアコードは次のとおりです.
 
    public String doIntercept(ActionInvocation invocation) throws Exception {
        Object action = invocation.getAction();
        if (action instanceof Preparable) {
            try {
                String[] prefixes;
                if (firstCallPrepareDo) {
                    prefixes = new String[] {ALT_PREPARE_PREFIX, PREPARE_PREFIX};
                } else {
                    prefixes = new String[] {PREPARE_PREFIX, ALT_PREPARE_PREFIX};
                }
                PrefixMethodInvocationUtil.invokePrefixMethod(invocation, prefixes);
            }
            catch (InvocationTargetException e) {
                /*
                 * The invoked method threw an exception and reflection wrapped it
                 * in an InvocationTargetException.
                 * If possible re-throw the original exception so that normal
                 * exception handling will take place.
                 */
                Throwable cause = e.getCause();
                if (cause instanceof Exception) {
                    throw (Exception) cause;
                } else if(cause instanceof Error) {
                    throw (Error) cause;
                } else {
                    /*
                     * The cause is not an Exception or Error (must be Throwable) so
                     * just re-throw the wrapped exception.
                     */
                    throw e;
                }
            }
            if (alwaysInvokePrepare) {
                ((Preparable) action).prepare();
            }
        }
        return invocation.invoke();
    }

このコードの論理は非常に簡単であり、actionがcom.opensymphony.xwork2.Preparableインタフェースを実装すると、setXXXおよびexecute()メソッドを呼び出す前にシリーズメソッドが呼び出される.firstCallPrepareDoがtrueの場合、prepareDoXXXメソッドが呼び出されます.そうでない場合、prepareXXXメソッドが呼び出されます(XXXはaction対応のメソッドです).次にalwaysInvokePrepareステータスを表示し、その値がtrueの場合、com.opensymphony.xwork2.Preparableインタフェースのprepareメソッドを呼び出します.最後にactionを次のブロックに渡します.
 
 
著作権所有、転載は出典を明記してください:http://blogwarning.iteye.com/blog/1390172