struts2异常处理,global-results定义全局结果处理
<global-results>定义全局结果处理 一般发生异常之后 结果返回errHandler
因为errHandler是由<global-exception-mappings>关联到Exception这个类了
然后处理结果
<result name="errHandler" type="chain">
然后它就根据
<param name="actionName">errorProcessor</param>
找action
<action name="errorProcessor" class="cn.itcast.sh.error.ErrorProcess">
<result>/error.jsp</result>
</action>
处理了 然后 返回到 error.jsp
在struts.xml中
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
<!-- <constant name="struts.custom.i18n.resources" value="itcast"></constant> -->
<package name="struts-global" namespace="/" extends="struts-default">
<global-results>
<result name="errHandler" type="chain">
<param name="actionName">errorProcessor</param>
</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="errHandler" exception="java.lang.Exception">
</exception-mapping>
</global-exception-mappings> <action name="errorProcessor" class="cn.itcast.sh.error.ErrorProcess">
<result>/error.jsp</result>
</action>
</package>
然后其他包都继承它 就默认使用了其中定义的 错误处理
然后实现 类
class="cn.itcast.sh.error.ErrorProcess"
package cn.itcast.sh.error; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class ErrorProcess extends ActionSupport {
private Exception exception; public Exception getException() {
return exception;
} public void setException(Exception exception) {
this.exception = exception;
}
@Override
public String execute()
{
ActionContext.getContext().getValueStack().push(this.exception.getMessage()); //放到值栈顶
return this.SUCCESS;
}
}
例子
struts.xml配置
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
<!-- <constant name="struts.custom.i18n.resources" value="itcast"></constant> -->
<package name="struts-global" namespace="/" extends="struts-default">
<global-results>
<result name="errHandler" type="chain">
<param name="actionName">errorProcessor</param>
</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="errHandler" exception="java.lang.Exception">
</exception-mapping>
</global-exception-mappings> <action name="errorProcessor" class="cn.itcast.sh.error.ErrorProcess">
<result>/error.jsp</result>
</action>
</package>
<include file="struts-user.xml"></include>
<include file="struts-login.xml"></include>
</struts>
struts-user.xml中
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="user" namespace="/" extends="struts-global">
<action name="UserAction_*" method="{1}" class="cn.itcast.sh.action.UserAction">
<result name="userList">/user/list.jsp</result>
</action>
</package>
</struts>
然后 如果页面异常 都会转向 error.jsp中 显示
error.jsp可以进行错误显示
因为信息被放到栈顶了 所以可以取到
<s:property />
struts2异常处理,global-results定义全局结果处理的更多相关文章
- Struts2中防止表单重复提交,global-results定义全局结果处理
1.在表单中加入<s:token/>标签 2.在动作类中加入token的拦截器 <!--如果单单写 name="token" 会丧失 defaultStack 拦 ...
- 前端测试框架Jest系列教程 -- Global Functions(全局函数)
写在前面: Jest中定义了很多全局性的Function供我们使用,我们不必再去引用别的包来去实现类似的功能,下面将列举Jest中实现的全局函数. Jest Global Functions afte ...
- 【统一异常处理】@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
1.利用springmvc注解对Controller层异常全局处理 对于与数据库相关的 Spring MVC 项目,我们通常会把 事务 配置在 Service层,当数据库操作失败时让 Service ...
- Vue 中如何定义全局的变量和常量
Vue 中如何定义全局的变量和常量 我想要定义一个变量, 在项目的任何地方都可以访问到, 不需要每一次使用的时候, 都引入. 尝试1:创建 global.js 并且在其中定义 let a = 10 ...
- 16.怎样自学Struts2之Struts2异常处理[视频]
16.怎样自学Struts2之Struts2异常处理[视频] 之前写了一篇"打算做一个视频教程探讨怎样自学计算机相关的技术",优酷上传不了,仅仅好传到百度云上: http://pa ...
- vue 定义全局函数,监听android返回键事件
vue 定义全局函数,监听android返回键事件 方法一:main.js 注入(1)在main.js中写入函数Vue.prototype.changeData = function (){ aler ...
- python之GIL官方文档 global interpreter lock 全局解释器锁
0.目录 2. 术语 global interpreter lock 全局解释器锁3. C-API 还有更多没有仔细看4. 定期切换线程5. wiki.python6. python.doc FAQ ...
- vue-自定义全局键盘码
1.Vue.config.keyCodes.enter=13; //main.js中定义全局 <template> <div> <input v-model=" ...
- vue定义全局方法 调用其他组件的方法
官网的写法 vue实例.$on就可以在根实例上定义全局方法 this.$root就是获取根实例 如果没有根实例 就表示当前实例 this.$root.$on 不需要.eventHub 不需要下面这 ...
随机推荐
- php 实现树形结构
<?phpclass Tree{ private $OriginalList; public $pk;//主键字段名 public $parentKey;//上级id字段名 public $ch ...
- jd-gui 反编译后去除注释
默认情况下,使用jd-gui反编译时会有注释. 行号注释 /* 521:502 */ 元信息 /* Location: D:\core-1.0.0.jar * Qualified ...
- 296. Best Meeting Point
题目: A group of two or more people wants to meet and minimize the total travel distance. You are give ...
- OpenGL基础渲染
客户端-服务器 客户端是存储在CPU存储器中的,并且在应用程序中执行(或者驱动程序),驱动程序将渲染命令和数据组合起来,发动到服务器执行.服务器和客户机在功能上是异步的,他们是各自独立的软件模块或者硬 ...
- 快速获取Windows系统上的国家和地区信息
Windows系统上包含了200多个国家和地区的数据,有时候编程需要这些资料.以下代码可以帮助你快速获取这些信息.将Console语句注释掉,可以更快的完成分析. static void Main(s ...
- springmvc在web.xml中的配置
<!-- SpringMVC核心分发器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name ...
- git pull使用【转】
转自:http://www.yiibai.com/git/git_pull.html git pull命令的作用是,取回远程主机某个分支的更新,再与本地的指定分支合并.它的完整格式稍稍有点复杂. $ ...
- git服务器gitlab之搭建和使用--灰常好的git服务器【转】
转自:http://blog.csdn.net/zy416548283/article/details/38057925 git服务器比较有名的是gitosis和gitolite,这两个管理和使用起来 ...
- c#操作txt
C#追加文件 StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt"); sw ...
- 命令 crontab
crontab命令选项基本只有对用户操作的选项: -u 指定一个用户 -l 列出某个用户的任务计划 -r 删除某个用户的任务 -e 编辑某个用户的任务 所以,要查看所有用户的,只能根据/etc/pas ...