Struts中像这种全局错误能否进行封装?
例如像以下代码:
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response){
String action = request.getParameter( "action ");
if( "111 ".equals(action))
{
ActionMessages messages = new ActionMessages();
ActionMessage message = new ActionMessage( "error1 ");
messages.add(ActionMessages.GLOBAL_MESSAGE,message);
saveMessages(request,messages);
return mapping.findForward( "error1 ");
}
else if( "222 ".equals(action))
{
ActionMessages messages = new ActionMessages();
ActionMessage message = new ActionMessage( "error2 ");
messages.add(ActionMessages.GLOBAL_MESSAGE,message);
saveMessages(request,messages);
return mapping.findForward( "error2 ");
}
return mapping.findForward( "ok ");
}
如果这种判断很多,那代码就会显得很长,也很乱,能不能把
ActionMessages messages = new ActionMessages();
ActionMessage message = new ActionMessage( "error1 ");
messages.add(ActionMessages.GLOBAL_MESSAGE,message);
saveMessages(request,messages);
return mapping.findForward( "error1 ");
的代码块封装起来,然后再调用?
------解决方案--------------------可以呀,把这段代码写到一个函数中,参数ActionMapping mapping, HttpServletRequest request,
为参数