JSF和Struts框架的错误控制与封装处理

豆豆网   技术应用频道   2007年07月27日    社区交流

内容摘要:在struts中,通常采用的全局错误控制模式是构建一个baseAction,在其execute方法中完成前台传回方法的dispatch操作,并由 try……catch……捕获程序错误,实现错误的控制和展示。

  在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());
  }
  ……
  }

责编:豆豆技术应用

正在加载评论...