例外を処理するインターセプター
SAStrutsのページには例外処理はstrutsのもともとの機構を使ってね。http://sastruts.seasar.org/featurereference.htmlとかかれているが、まぁどうせならstruts-config.xml触りたくないのでインターセプターを作ってみた。
public class ForwardThrowsInterceptor extends ActionMessagesThrowsInterceptor { private static final long serialVersionUID = 1L; public String globalForward; public String noRoleForward; public String handleThrowable(Exception e, MethodInvocation invocation) throws Throwable { return (globalForward == null ? getInput() : globalForward); } public String handleThrowable(NoRoleRuntimeException e, MethodInvocation invocation) throws Throwable { return (noRoleForward == null ? getInput() : noRoleForward); } protected String getInput() { S2ExecuteConfig executeConfig = S2ExecuteConfigUtil.getExecuteConfig(); if (executeConfig.getInput() != null) { return executeConfig.getInput(); } return S2ActionMappingUtil.getActionMapping().getInput(); } }
ActionMessagesExceptionをキャッチしたら前ページへ、@Execute(roles="xxx")で権限なし例外があったら指定されたページへ、それ以外の例外は指定したページへ・・・みたいな感じにしたかったんだけど、ちょっとうまくいかない。
というのも権限チェックはS2RequestProcessor#processRoles()でやっててそこからNoRoleRuntimeException が発生するため、costomizer.diconで指定するインターセプタには捕らえられない。だからstruts-config.xmlで指定してね。ってことなんだろうけど、この例外処理だけ別にコンテキストルート直下にjspファイルをおかなくちゃならない。
なんか気持ち悪いんだよなー。ActionServletはstrutsそのまま使ってるしRequestProcessorをDIするようにするのもなんか面倒くさい。
いい解決方法ないものかな・・・
追記
解決しました。→http://d.hatena.ne.jp/mostlyfine/20080725/1216953169