熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

JSF和struts基於框架的錯誤控制與封裝處理

2022-06-13   來源: Java開源技術 

  在struts中通常采用的全局錯誤控制模式是構建一個baseAction在其execute方法中完成前台傳回方法的dispatch操作並由 try……catch……捕獲程序錯誤實現錯誤的控制和展示一個典型的BaseAction例子如下

  代碼


  

  public ActionForward execute(ActionMapping mapping ActionForm form HttpServletRequest request HttpServletResponse response) {

  ……

  ActionForward forwardPage = null

  try {

  String parameter = mappinggetParameter()

  if (parameter == null) {

  String message = messagesgetMessage(dispatchhandler mappinggetPath())

  responsesendError( message)

  return null

  }

  String name = processReqCode(requestgetParameter(parameter))

  forwardPage = dispatchMethod(mapping form request response name)

  } catch (BaseException ex) {

  if (logisDebugEnabled())

  logdebug(發生錯誤 ex)

  forwardPage = processBaseException(request mapping ex)

  } catch (Throwable ex) {

  logerror(發生錯誤 ex)

  ActionMessages errors = new ActionMessages()

  ByteArrayOutputStream ostr = new ByteArrayOutputStream()

  exprintStackTrace(new PrintStream(ostr))

  errorsadd(orgapachestrutsactionGLOBAL_MESSAGE new ActionMessage(ostrtoString()))

  saveErrors(request errors)

  forwardPage = mappingfindForward(syserror

  outputsetStatus(fail

  outputsetError(exgetMessage())

  }

  ……

  } 

  由於JSF采用了managed beanJSP頁面直接通過調用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 = FacesContextgetCurrentInstance()

  Application application = facesContextgetApplication()

  ActionSource actionSource = (ActionSource) eventgetComponent()

  MethodBinding methodBinding = actionSourcegetAction()

  String fromAction = nullString outcome = null

  if (methodBinding != null) {

  fromAction = methodBindinggetExpressionString()

  try {

  outcome = (String) methodBindinginvoke(facesContext null)

  } catch (EvaluationException e) {

  Throwable cause = egetCause()

  if (cause != null && cause instanceof AppException) {

  //這裡需要根據框架的不同判斷實例是否是程序中手動拋出的錯誤       

  FacesUtilsaddErrorMessage(eventgetComponent()getClientId(facesContext)causegetMessage())}

  else {

  throw (AbortProcessingException) cause

  }

  } catch (RuntimeException e) {

  throw new FacesException(Error calling action method of component with id + eventgetComponent()getClientId(facesContext) e)

  }

  NavigationHandler navigationHandler = applicationgetNavigationHandler()

  navigationHandlerhandleNavigation(facesContext fromAction outcome)

  // Render Response if needed

  facesContextrenderResponse()

  }

  } 

  監聽器配置facesconfigapplicationxml

  代碼

   <application>
        <variableresolver>orgspringframeworkwebjsfDelegatingVariableResolver</variableresolver>
<messagebundle>resourcesapplication</messagebundle>
<localeconfig>
<defaultlocale>en</defaultlocale>
</localeconfig>
<actionlistener>orgsnailportalwebframeworklistenerGlobalActionListener</actionlistener>
</application> 

  這樣開發人員只需要在action和managed bean裡面根據業務的需要拋出指定基礎類型的Exception實例由BaseAction和ActionListener完成錯誤的封裝處理再傳遞給前台進行顯示從而減少開發的代碼量提高框架的可維護性


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28909.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.