2009-07-27

SAStrutsでエラーメッセージを設定してエラー画面に遷移する方法 2

前回のSAStrutsでエラーメッセージを設定してエラー画面に遷移する方法だと記述量が多いので扱いにくい。
static importや可変長パラメータを使用して書きやすくしてみた。

ユーティリティクラス
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

public class AppUtil {
public static ActionMessages getErrors(String msgKey, String ...args) {
ActionMessages errors = new ActionMessages();
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage(msgKey, args));
return errors;
}
}


使用クラスメソッド(ここではアクションクラスのメソッド)
import static org.seasar.struts.util.ActionMessagesUtil.*;
import static org.seasar.struts.util.MessageResourcesUtil.*;
import org.seasar.struts.util.RequestUtil;
import static sample.AppUtil.*;
・・・
Integer id;
try {
id = Integer.parseInt(form.id);
} catch(NumberFormatException e) {
addErrors(RequestUtil.getRequest(),
getErrors("errors.integer", getMessage("labels.workstyleId")));
return "/error/";
}
・・・

0 件のコメント: