struts2配置之全局result及异常处理

来源:互联网 发布:季后赛数据统计 编辑:程序博客网 时间:2024/05/18 03:27

提起异常处理,不由想起铺天盖地的try{...}catch{...}语句,在方法中使用catch块处理异常已经成为我们的习惯。

在struts2中,是该改变一下的时候了;

struts2凭借自己的强大拦截器功能,隆重推出声明式异常处理机制。

struts2的声明式异常处理机制是在struts.xml文件中配置<exception-mapping>来实现的

不废话,看代码:

[html] view plain copy
  1. <package name="default" namespace="/" extends="struts-default">  
  2.         <default-action-ref name="default-error" />  
  3.         <global-results>  
  4.             <result name="login" type="redirect">/admin/login.jsp</result>  
  5.             <result name="index" type="redirect">/admin/index.jsp</result>  
  6.             <result name="error">/admin/500.html</result>  
  7.         </global-results>  
  8.         <global-exception-mappings>  
  9.             <exception-mapping result="error" exception="java.lang.Exception" />  
  10.         </global-exception-mappings>  
  11.         <action name="default-error">  
  12.             <result>/admin/500.html</result>  
  13.         </action>  
  14. </package>  

从这可以看出action中execute方法throw exception的良苦用心了,将异常信息直接抛给struts拦截器处理。

如果想显示异常信息,在前台页面中使用

[html] view plain copy
  1. <s:property value="exception.message"/>  

来获取异常信息,用

[html] view plain copy
  1. <s:property value="exceptionStack"/>  
来获取异常的堆栈信息。
阅读全文
0 0
原创粉丝点击