SSH答疑解惑系列(三)——Struts2的异常处理
Struts2的异常采用声明式异常捕捉,具体通过拦截器来实现。
在项目中,我们可以在Action中直接抛出异常,剩下的就交给Struts2的拦截器来处理了。当然,我们需要进行相关配置。
Struts2配置了默认的拦截器,来处理异常。在struts-default.xml中,可以找到配置。有了这个配置以后,我们在项目中就方便多了。
<interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>
在项目中,只需要使用exception-mapping标签来配置:
1)全局异常
适用对象:所有Action
<global-results >
<!-- 跳转到全局异常的页面 -->
<result name="global-error">/global-error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="global-error" exception="com.struts2.ApplicationException"></exception-mapping>
</global-exception-mappings>
2)局部异常
使用对象:异常所在的Action
<!-- 局部异常,LoginAction -->
<action name="login" class="com.struts2.LoginAction">
<exception-mapping result="error" exception="com.struts2.ApplicationException"></exception-mapping>
<result>/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
ApplicationException
Throwable是所有异常的根
Error是错误
Exception是异常,RuntimeException是运行中的异常。
除了Struts2自带的拦截器,我们还可以自定义拦截器,覆盖底层的拦截器,使得异常的捕捉更加灵活。
自定义拦截器的配置也十分方便。
<interceptors>
<!-- 声明拦截器 -->
<interceptor name="errorInterceptor" class="cn.itcast.util.ErrorInterceptor" />
<!-- 配置拦截器栈 -->
<interceptor-stack name="myErrorInterceptor">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="errorInterceptor" />
</interceptor-stack>
</interceptors>
<!-- 覆盖底层的拦截器栈 对包中的所有action都有效 -->
<default-interceptor-ref name="myErrorInterceptor"/>
<global-results>
<result name="errorMsg">/errorMsg.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="errorMsg" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
Struts2的“错误页”
经常使用ssh框架开发,会发现Struts2中各种报错页面的布局和显示信息很类似。这是因为Struts2提供了一个它自己的“错误页”,在org.apache.struts2.dispatcher包下。
<html>
<head>
<title>Struts Problem Report</title>
<style>
pre {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<h2>Struts Problem Report</h2>
<p>
Struts has detected an unhandled exception:
</p>
<#assign msgs = [] />
<#list chain as ex>
<#if ex.message??>
<#assign msgs = [ex.message] + msgs/>
</#if>
</#list>
<#assign rootex = exception/>
<#list chain as ex>
<#if (ex.location?? && (ex.location != unknown))>
<#assign rootloc = ex.location/>
<#assign rootex = ex/>
<#else>
<#assign tmploc = locator.getLocation(ex) />
<#if (tmploc != unknown)>
<#assign rootloc = tmploc/>
<#assign rootex = ex/>
</#if>
</#if>
</#list>
<div id="exception-info">
<table>
<tr>
<td><strong>Messages</strong>:</td>
<td>
<#if (msgs?size > 1)>
<ol>
<#list msgs as msg>
<#if (msg?is_method)>
<li>${msg[0]}</li>
<#else>
<li>${msg}</li>
</#if>
</#list>
</ol>
<#elseif (msgs?size == 1)>
<#if (msgs[0]?is_method)>
<li>${msgs[0][0]}</li>
<#else>
<li>${msgs[0]}</li>
</#if>
</#if>
</td>
</tr>
<#if rootloc??>
<tr>
<td><strong>File</strong>:</td>
<td>${rootloc.URI}</td>
</tr>
<tr>
<td><strong>Line number</strong>:</td>
<td>${rootloc.lineNumber}</td>
</tr>
<#if (rootloc.columnNumber >= 0)>
<tr>
<td><strong>Column number</strong>:</td>
<td>${rootloc.columnNumber}</td>
</tr>
</#if>
</#if>
</table>
</div>
<#if rootloc??>
<#assign snippet = rootloc.getSnippet(2) />
<#if (snippet?size > 0)>
<div id="snippet">
<hr />
<#list snippet as line>
<#if (line_index == 2)>
<#if (rootloc.columnNumber >= 3)>
<pre style="background:yellow">${(line[0..(rootloc.columnNumber-3)]?html)}<span style="background:red">${(line[(rootloc.columnNumber-2)]?html)}</span><#if ((rootloc.columnNumber)<line.length())>${(line[(rootloc.columnNumber-1)..]?html)}</#if></pre>
<#else>
<pre style="background:yellow">${line?html}</pre>
</#if>
<#else>
<pre>${line?html}</pre>
</#if>
</#list>
</div>
</#if>
</#if>
<div id="stacktraces">
<hr />
<h3>Stacktraces</h3>
<#list chain as ex>
<div class="stacktrace" style="padding-left: ${ex_index * 2}em">
<strong>${ex}</strong>
<div>
<pre>
<#list ex.stackTrace as frame>
${frame}
</#list>
</pre>
</div>
</div>
</#list>
</div>
<div class="footer">
<hr />
<p>
You are seeing this page because development mode is enabled. Development mode, or devMode, enables extra
debugging behaviors and reports to assist developers. To disable this mode, set:
<pre>
struts.devMode=false
</pre>
in your <code>WEB-INF/classes/struts.properties</code> file.
</p>
</div>
</body>
</html>
SSH答疑解惑系列(三)——Struts2的异常处理的更多相关文章
- SSH答疑解惑系列(一)——spring容器是如何启动的
SSH框架十分受欢迎,其中有一个原因就是spring可以和Struts2框架无缝整合.在使用spring时,无需手动创建web容器,而是通过配置文件声明式创建spring容器. 在web应用中,创建s ...
- SSH答疑解惑系列(二)——java.lang.reflect.InvocationTargetException异常
在项目中遇到了invocationTargetException的问题,在这里跟大家分享一下. 报错信息如下: 使用反射时,比如执行invoke方法,如果被反射执行的方法体抛出了Exception,这 ...
- (Struts2学习系列三)Struts2动态方法调用:通配符方式
更改src/struts2.xml的代码: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...
- Redis总结(五)缓存雪崩和缓存穿透等问题 Web API系列(三)统一异常处理 C#总结(一)AutoResetEvent的使用介绍(用AutoResetEvent实现同步) C#总结(二)事件Event 介绍总结 C#总结(三)DataGridView增加全选列 Web API系列(二)接口安全和参数校验 RabbitMQ学习系列(六): RabbitMQ 高可用集群
Redis总结(五)缓存雪崩和缓存穿透等问题 前面讲过一些redis 缓存的使用和数据持久化.感兴趣的朋友可以看看之前的文章,http://www.cnblogs.com/zhangweizhon ...
- 《浅谈磁盘控制器驱动》,磁盘控制器驱动答疑解惑![2012.1.29完结]by skyfree
<浅谈磁盘控制器驱动>,磁盘控制器驱动答疑解惑![2012.1.29完结] https://www.itiankong.net/thread-178655-1-1.html Skyfre ...
- Ansible Tower系列 三(使用tower执行一个任务)【转】
创建playbook Tower playbook 项目默认存在 /var/lib/awx/projects/ su - awx cd projects/ mkdir ansible-for-devo ...
- 【struts2】Struts2的异常处理
在Action中execute方法声明为:public String execute() throws Exception,这样,Action可以抛出任何Exception. 1)自己实现异常处理 我 ...
- S5PV210开发系列三_简易Bootloader的实现
S5PV210开发系列三 简易Bootloader的实现 象棋小子 1048272975 Bootloader是嵌入式系统上电后第一段运行的代码.对于功能简单的处理器,可能并没有Bo ...
- SSH初体验系列--Hibernate--2--crud操作
Ok,今天比较详细的学习一下hibernate的C(create).R(read).U(update).D(delete) 相关api... 前言 Session: 是Hibernate持久化操作的基 ...
随机推荐
- Spring框架中的IOC?
Spring中的org.springframework.beans包和org.SpringframeWork.context包构成了Spring框架IOC容器的基础.BeanFactory接口提供了一 ...
- js的事件流你真的弄明白了吗?
当浏览器发展到第四代时候,浏览器开发团队遇到了一个有意思的问题:页面的哪一部分会拥有某个特地的事件?要明白这个问题问的是什么,可以想象画在纸上的一组同心圆,如果你把手指放在圆心上,那么你的手指指向的不 ...
- Sass学习日志
一.什么是SASS SASS是一中CSS的开发工具,提供了许多便利的写法,大大节约了设计者们的时间,使得CSS的开发,变得简单和可维护.本文总结了SASS的主要方法.我们的目标是,有了这篇文章,日常的 ...
- Python实现trim函数
Python中其实也有类似Java的trim函数的,叫做strip,举例: #!/usr/bin/python # -*- coding: UTF-8 -*- str = "0000000h ...
- shell 输出带颜色字体
输出特效格式控制:\033[0m 关闭所有属性 \033[1m 设置高亮度 \03[4m 下划线 \033[5m 闪烁 \033[7m 反显 \033[8m 消隐 \ ...
- jQuery的封装
封装,最简单的效果就是一个效果你可以重复的去调用 本来前端之路时间不是很长,但是对封装还是有一点点自己个人的理解,曾经踩过的坑也不在少数,最后总结出我个人风格的封装,听一位大神指点过,每个人都有属 ...
- jquer搜索
<body> <br/> <center> <tr><td>创建时间:</td><td><input type ...
- 解决pycharm报错:AttributeError: module 'pip' has no attribute 'main'
找到pycharm安装目录下 helpers/packaging_tool.py文件,找到如下代码: def do_install(pkgs): try: import pip except Impo ...
- jenkins邮件发送jmeter接口测试报告
在Jenkins中配置实现邮件通知,Jenkins提供了两种方式的配置. 一种是Jenkins内置默认的邮件通知,但是它本身有很多局限性,比如它的邮件通知无法提供详细的邮件内容.无法定义发送邮件的格式 ...
- Leecode刷题之旅-C语言/python-35.搜索插入位置
/* * @lc app=leetcode.cn id=35 lang=c * * [35] 搜索插入位置 * * https://leetcode-cn.com/problems/search-in ...