`

Struts2 BusinessException

阅读更多
最近更新STRUTS的版本到2.1.8 ,发现异常发生后,没有收到异常邮件了,
配置

<global-exception-mappings>  
            <exception-mapping result="normalException"  
                exception="com.esc.common.exception.BusinessException">  
            </exception-mapping>  
        </global-exception-mappings> 


BusinessException继承了RuntimeExcption,2.0的时候,凡是非BusinessException子类,就会向外抛出,那么就能在我下面的catch块中捕获到异常
代码

//我的filter,在struts的filter之前  
public void doFilter(ServletRequest org0, ServletResponse org1,  
            FilterChain chain) throws IOException, ServletException {  
        HttpServletRequest request = (HttpServletRequest) org0;  
        HttpServletResponse response = (HttpServletResponse) org1;  
        try {  
            ..........  
            chain.doFilter(org0, org1);//到STRUTS的FILTER  
            ..........  
        }catch (Exception e) {//现在这里现在永远到不了  
            sendEmailIfNecessary(e, request);//发邮件  
        }  


查了很久,发现此版本不再抛异常了,org.apache.struts2.dispatcher.Dispatcher类serviceAction方法代码

public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,  
                             ActionMapping mapping) throws ServletException {  
  
       .......;  
       try {  
           ......;  
       } catch (ConfigurationException e) {//异常处理  
        // WW-2874 Only log error if in devMode  
        if(devMode) {  
            LOG.error("Could not find action or result", e);  
        }  
        else {  
            LOG.warn("Could not find action or result", e);  
        }  
           sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);  
       } catch (Exception e) {  
           //这里以前是throw new ServletException(e);  
           sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);  
       } finally {  
           UtilTimerStack.pop(timerKey);  
       }  
   }  

来看看sendError的代码

public void sendError(HttpServletRequest request, HttpServletResponse response,  
           ServletContext ctx, int code, Exception e) {  
       if (devMode) {  
//开发模式,即Struts的配置文件中有设如下设置<constant name="struts.devMode" value="true" />  
           response.setContentType("text/html");  
  
           try {  
               FreemarkerManager mgr = getContainer().getInstance(FreemarkerManager.class);  
  
               freemarker.template.Configuration config = mgr.getConfiguration(ctx);  
               Template template = config.getTemplate("/org/apache/struts2/dispatcher/error.ftl");  
  
               List<Throwable> chain = new ArrayList<Throwable>();  
               Throwable cur = e;  
               chain.add(cur);  
               while ((cur = cur.getCause()) != null) {  
                   chain.add(cur);  
               }  
  
               HashMap<String,Object> data = new HashMap<String,Object>();  
               data.put("exception", e);  
               data.put("unknown", Location.UNKNOWN);  
               data.put("chain", chain);  
               data.put("locator", new Locator());  
               template.process(data, response.getWriter());  
               response.getWriter().close();  
           } catch (Exception exp) {  
               try {  
                   response.sendError(code, "Unable to show problem report: " + exp);  
               } catch (IOException ex) {  
                   // we're already sending an error, not much else we can do if more stuff breaks  
               }  
           }  
       } else {//普通模式  
           try {  
               // WW-1977: Only put errors in the request when code is a 500 error  
               if (code == HttpServletResponse.SC_INTERNAL_SERVER_ERROR) {  
                   // send a http error response to use the servlet defined error handler  
                   // 原来异常被这样处理掉了  
                   request.setAttribute("javax.servlet.error.exception", e);  
  
                   // for compatibility  
                   request.setAttribute("javax.servlet.jsp.jspException", e);  
               }  
  
               // send the error response  
               response.sendError(code, e.getMessage());  
           } catch (IOException e1) {  
               // we're already sending an error, not much else we can do if more stuff breaks  
           }  
       }  
   }  


再把结果说明白一点,
1、开发模式:异常不好拿了,异常信息被直接写到页面,不过没关系,开发嘛,这不正是你想要的吗
2、正常模式:通过request.getAttribute("javax.servlet.error.exception")或者request.getAttribute("javax.servlet.jsp.jspException")可以取得异常对象。
分享到:
评论

相关推荐

    BusinessException.java

    BusinessException.java

    NC(UFIDA)错误帮助文档加强版

    业务异常nc.vo.pub.BusinessException类 第二节 开发问题集 UI工厂分为哪几大基本模块? 怎样配置eclipse参数? 单据开发涉及哪几大模板? pdm文件数据类型问题 制作单据模板的时候,主表或者子表的字段名显示不出来...

    NC开发常见问题记录.doc

    nc开发常见问题记录,

    ASP.NET Core 全局异常处理和日志记录

    public class BusinessException : Exception { public BusinessException(){} public BusinessException(string message) : base(message) {} public BusinessException(string message, Exception ...

    ARCH4系统开发指南

    2.28.2 Struts2校验框架应用 71 2.28.2.1 校验配置文件 71 2.28.2.2 角色Insert页面示例 71 2.28.2.3 用户Query页面示例 73 2.28.2.4 多行输入域校验 75 2.29 多语言管理 77 2.29.1 定义输入域的name属性和资源文件 ...

    NC后台任务插件开发代码

    Public interface nc.bs.pub.taskcenter. IBackgroundWorkPlugin { /** * 任务插件执行体 * @param context 执行环境 ... PreAlertObject executeTask(BgWorkingContext context) throws BusinessException; }

    UAP-STUDIO-6.5.0.2 适用于NC65产品二开工具

    用友NC65产品二开的集成开发环境

    online-exam.zip

    技术框架:SpringBoot + tkmybatis 在线考试系统 简单系统通用Web后端框架,提供: 1.xss防护。 2.实现请求白名单功能。(加入@Public于接口方法...(业务层直接throw new BusinessException处理业务异常或程序异常)

    银行账户管理系统 简称BAM(项目介绍及源码)绝对精典

    以上四个异常类有一个共同的父类 BusinessException 并妥善的处理这些异常 项目五 练习8:(集合) 改写Bank类,采用集合的方式来管理多个Account对象 为Bank类添加一个方法 打印所有用户的总资产排名 说明:一个用户...

    iretail

    Spring Boot新零售系统叠放主文件结构... │ └── GlobalDefaultExceptionHandler.java │ │ └── BusinessException.java │ │ │ ├── controller │ │ └── UserController.java │ │ │ ├── dto │

    common-spring-boot-stater:基于SpringBoot的Web开发公共模块

    BusinessException 可自定义 可自定义 空指针异常 500 空指针异常 例外 500 未知异常 1.1.2数据访问异常处理器 负责处理数据库访问产生的异常: 异常类型 响应码 响应文本 DataAccessException 500 数据访问错误 ...

    MorriosPayment

    目录 说明 Morrios工具包支付库 安装 依赖要求 ...1.x 7.0 + ext-json ext-mbstring ...use Morrios \ Base \ Exception \ BusinessException ; use Morrios \ Payment \ PaymentFactory ; use Morrios \ Payment \ Par

    Java Unique Exception Extractor:用于提取唯一 Java 异常的实用程序-开源

    可以设置为仅收集某些异常(例如,NullPointerException、SQLException 等) 可以设置为跳过某些异常(例如,MissingResourceException、BusinessException 等) Cab 被配置为仅捕获某些单词列表(具有公共前缀)。...

Global site tag (gtag.js) - Google Analytics