首页  韩国资源  酷站加油  我的展厅  设计名站  古典元素  推荐下载  设计欣赏  每周专访  招募精英  人才专区  网页教程  平面设计  编程开发  设计竞赛
当前位置:首页 > 编程开发 > JSP教程 > 正文
Google
JSF和Struts框架的错误控制与封装处理
来源:68design.net 2007年07月27日 08:42 网友评论:0条 点击:

在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完成错误的封装处理,再传递给前台进行显示,从而减少开发的代码量,提高框架的可维护性。

上一篇:Java程序员要掌握的十个JSP中的标签库   下一篇:如何实现javabean的属性拷贝
收藏此页】【打印】【关闭
 相关文章  我要点评
·Spring集成JSF的最简单方式
·选择JSF不选Struts的十大理由
·用 jsfl 扩展你的 flash
·JSF技术的WEB应用开发研究
·介绍JSF框架使用的设计模式
·JSF框架中使用的设计模式介绍
·对比JSF在Eclipse和NetBeans中的应用
·基于MVC模式Struts框架研究与应用详解

免责声明:本站刊载此文不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。对本文有任何异议,请联络:68design#163.com
转载要求:作者及来源信息必需保留。转载之图片、文件,链接请不要盗链到本站,且不准打上各自站点的水印。



关于我们 | 在线反馈 | 广告报价 | 友情链接 | 联系我们 | 免责声明 | 在线投稿 | 网站地图
Copyright © 2003-2007 68design.net, All Rights Reserve 【找网页设计师,当然上网页设计师联盟】