在struts中,通常采用的全局錯誤控制模式是構建一個baseAction,在其execute方法中完成前台傳回方法的dispatch操作,並由 try……catch……捕獲程序錯誤,實現錯誤的控制和展示。一個典型的BaseAction例子如下:
代碼
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
……
ActionForward forwardPage = null;
try {
String parameter = mapping.getParameter();
if (parameter == null) {
String message = messages.getMessage("dispatch.handler",mapping.getPath());
response.sendError(500, message);
return null;
}
String name = processReqCode(request.getParameter(parameter));
forwardPage = dispatchMethod(mapping, form, request, response, name);
} catch (BaseException ex) {
if (log.isDebugEnabled())
log.debug("發生錯誤:", ex);
forwardPage = processBaseException(request, mapping, ex);
} catch (Throwable ex) {
log.error("發生錯誤:", ex);
ActionMessages errors = new ActionMessages();
ByteArrayOutputStream ostr = new ByteArrayOutputStream();
ex.printStackTrace(new PrintStream(ostr));
errors.add("org.apache.struts.action.GLOBAL_MESSAGE", new ActionMessage
(ostr.toString()));
saveErrors(request, errors);
forwardPage = mapping.findForward("syserror");
output.setStatus("fail");
output.setError(ex.getMessage());
}
……
}
由於JSF采用了managed bean,JSP頁面直接通過調用managed bean中的方法完成數據交互,不能像struts一樣通過捕獲dispatch操作過程拋出的異常來完成錯誤的處理(因為根本就沒有dispatch方法),似乎jsf根本就不支持全局的錯誤處理。
如果在managed bean中throw 一個exception(這裡是AppException),觀察一下控制台的日志,可以看到其實錯誤是從一個ActionListener的實現中拋出的(針對myfaces,這裡是ActionListenerImpl),參考jsf的生命周期過程,方法出來了:
代碼
public class GlobalActionListener extends ActionListenerImpl {
public void processAction(ActionEvent event) throws AbortProcessingException {
FacesContext facesContext = FacesContext.getCurrentInstance();
Application application = facesContext.getApplication();
ActionSource actionSource = (ActionSource) event.getComponent();
MethodBinding methodBinding = actionSource.getAction();
String fromAction = null;String outcome = null;
if (methodBinding != null) {
fromAction = methodBinding.getExpressionString();
try {
outcome = (String) methodBinding.invoke(facesContext, null);
} catch (EvaluationException e) {
Throwable cause = e.getCause();
if (cause != null && cause instanceof AppException) {
//這裡需要根據框架的不同,判斷實例是否是程序中手動拋出的錯誤
FacesUtils.addErrorMessage(event.getComponent().getClientId(facesContext),
cause.getMessage());}
else {
throw (AbortProcessingException) cause;
}
} catch (RuntimeException e) {
throw new FacesException("Error calling action method of component with id " +
event.getComponent().getClientId(facesContext), e);
}
NavigationHandler navigationHandler = application.getNavigationHandler();
navigationHandler.handleNavigation(facesContext, fromAction, outcome);
// Render Response if needed
facesContext.renderResponse();
}
}
監聽器配置,faces-config-application.xml:
代碼
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
<message-bundle>resources.application</message-bundle>
<locale-config>
<default-locale>en</default-locale>
</locale-config>
<action-listener>
org.snailportal.webframework.listener.GlobalActionListener
</action-listener>
</application>
這樣,開發人員只需要在action和managed bean裡面根據業務的需要拋出指定基礎類型的Exception實例,由BaseAction和ActionListener完成錯誤的封裝處理,再傳遞給前台進行顯示,從而減少開發的代碼量,提高框架的可維護性。