Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Actionインターセプター(Actionメソッドの共通前処理・後処理)
package jp.co.sample.interceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;
import org.seasar.framework.aop.interceptors.AbstractInterceptor;
import org.seasar.struts.annotation.Execute;
public class ActionInterceptor extends AbstractInterceptor {
// 変数
Logger logger = Logger.getLogger(getClass());
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// Executeアノテーションの場合
if (isExecuteMethod(invocation)) {
// 前処理
before(invocation);
}
// 主処理
Object object = invocation.proceed();
// Executeアノテーションの場合
if (isExecuteMethod(invocation)) {
// 後処理
after(invocation);
}
return object;
}
private String getActionName(MethodInvocation invocation) {
// 変数
String className;
String methodName;
String actionName;
// アクション名取得
className = getTargetClass(invocation).getSimpleName();
methodName = invocation.getMethod().getName();
actionName = className + "#" + methodName;
// アクション名返却
return actionName;
}
private Boolean isExecuteMethod(MethodInvocation invocation) {
// Executeアノテーション真偽値返却
return invocation.getMethod().isAnnotationPresent(Execute.class);
}
private void before(MethodInvocation invocation) {
// ログ出力
logger.info(getActionName(invocation) + "を開始します。");
}
private void after(MethodInvocation invocation) {
// ログ出力
logger.info(getActionName(invocation) + "を終了します。");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.