在struts中,通常采用的全局錯(cuò)誤控制模式是構(gòu)建一個(gè)baseAction,在其execute方法中完成前臺(tái)傳回方法的dispatch操作,并由 try……catch……捕獲程序錯(cuò)誤,實(shí)現(xiàn)錯(cuò)誤的控制和展示。一個(gè)典型的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("發(fā)生錯(cuò)誤:", ex);
forwardPage = processBaseException(request, mapping, ex);
} catch (Throwable ex) {
log.error("發(fā)生錯(cuò)誤:", 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頁(yè)面直接通過(guò)調(diào)用managed bean中的方法完成數(shù)據(jù)交互,不能像struts一樣通過(guò)捕獲dispatch操作過(guò)程拋出的異常來(lái)完成錯(cuò)誤的處理(因?yàn)楦揪蜎](méi)有dispatch方法),似乎jsf根本就不支持全局的錯(cuò)誤處理。
如果在managed bean中throw 一個(gè)exception(這里是AppException),觀察一下控制臺(tái)的日志,可以看到其實(shí)錯(cuò)誤是從一個(gè)ActionListener的實(shí)現(xiàn)中拋出的(針對(duì)myfaces,這里是ActionListenerImpl),參考jsf的生命周期過(guò)程,方法出來(lái)了:
代碼
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) {
//這里需要根據(jù)框架的不同,判斷實(shí)例是否是程序中手動(dòng)拋出的錯(cuò)誤
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;
}
}
監(jiān)聽(tīng)器配置,faces-config-application.xml:
代碼
org.springframework.web.jsf.DelegatingVariableResolver
org.snailportal.webframework.listener.GlobalActionListener
這樣,開(kāi)發(fā)人員只需要在action和managed bean里面根據(jù)業(yè)務(wù)的需要拋出指定基礎(chǔ)類(lèi)型的Exception實(shí)例,由BaseAction和ActionListener完成錯(cuò)誤的封裝處理,再傳遞給前臺(tái)進(jìn)行顯示,從而減少開(kāi)發(fā)的代碼量,提高框架的可維護(hù)性。
新聞熱點(diǎn)
疑難解答
圖片精選