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 不需要下面这 ...
随机推荐
- android中使用html作布局文件
在android开发中,通常使用xml格式来描述布局文件.就目前而言,熟悉android布局及美化的人员少之又少,出现了严重的断层.大部分企业,其实还是程序员自己动手布局.这样既浪费时间和精力,也未必 ...
- JVM垃圾回收机制总结(7) :调优方法
JVM调优工具 Jconsole,jProfile,VisualVM Jconsole : jdk自带,功能简单,但是可以在系统有一定负荷的情况下使用.对垃圾回收算法有很详细的跟踪.详细说明参考这里 ...
- CentOS7修改服务器主机名方法
CentOS7下修改主机名 第一种:hostname 主机名 01.hostname 主机名称 这种方式,只能修改临时的主机名,当重启机器后,主机名称又变回来了. 第二种:hostnamectl se ...
- ubuntu12.10升级至14.04
之前执行apt-get 不管是什么软件或apt-get update都会遇到fail to fetch http://us.archive.ubuntu.com quantal-updates/mai ...
- C# 获取打印机状态
public static string GetPrinterStatus(string PrinterName) { int intValue = GetPrinterStatusInt ...
- js动态判断密码强度&&实用的 jQuery 代码片段
// 网上拷贝的代码,效果不太好需要自己调整<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...
- JavaScript —— 对象的取值与赋值
可能是因为用惯了 Java ,对一个对象取值/赋值喜欢用 setXXX() 和 getXXX() . 在 JavaScript 中使用 setValue() 时,遇到了个奇怪的问题,所以查了下 Jav ...
- centos 升级GCC/G++
#get rep yum install centos-release-scl-rh #yum install centos-release-scl # install g++ 5.2.1 yum - ...
- guidelines for contributing Hearthstone-Deck-Tracker
https://github.com/Epix37/Hearthstone-Deck-Tracker/blob/master/CONTRIBUTING.md Coding style Always u ...
- objective-c 与 js之间传递中文乱码
最近在做关于js改写oc framework的小project,遇到了不少问题 其中刚遇到的是关于如何在两者之间传递中文字符,带特殊字符的URL字符串 不会很详细的介绍太多,以后会回头做个总结 oc传 ...