struts的消息处理

本文介绍了Struts框架中自定义异常处理机制的具体实现方式。通过创建ActionException类继承RuntimeException,并结合ActionExceptionHandler进行异常捕获与转发,实现了针对特定异常的定制化处理流程。
[code]

public class ActionException extends RuntimeException
{

private ActionMessage actionMessage;

public ActionException(String key)
{
actionMessage = new ActionMessage(key);
}

public String getMessage()
{
if(actionMessage != null)
return actionMessage.toString();
else
return super.getMessage();
}
}

public class ActionExceptionHandler extends ExceptionHandler
{

public ActionExceptionHandler()
{
}

public ActionForward execute(Exception ex, ExceptionConfig config, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws ServletException
{
if(ex instanceof ActionException)
{
ActionMessages errors = new ActionMessages();
errors.add("org.apache.struts.action.GLOBAL_MESSAGE", ((CannaException)ex).getActionMessage());
saveErrors(request, errors);
ActionForward forward = mapping.getInputForward();
if(forward == null)
forward = new ActionForward(config.getPath());
return forward;
} else
{
return super.execute(ex, config, mapping, form, request, response);
}
}

protected void saveErrors(HttpServletRequest request, ActionMessages errors)
{
if(errors == null || errors.isEmpty())
{
request.removeAttribute("org.apache.struts.action.ERROR");
return;
} else
{
request.setAttribute("org.apache.struts.action.ERROR", errors);
return;
}
}
}
[/code]

[code]
<exception key="exception.actionception" type="com.ActionException"
handler="com.ActionExceptionHandler" />
[/code]
[code]
<message-resources parameter="com.LocalStrings"
null="false" />
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值