struts2中的exception-mapping声明时异常处理

1.struts2提供了一种机制就是声明时异常处理,把action请求处理时没有捕获的异常在执行时进行处理。

2.如何对异常进行配置呢?

其实很简单就是在配置文件中添加exception-mapping节点即可。exception-mapping节点有两个属性,一个是exception另一个是result,他们分别的作用是什么呢?exception属性是用来指定需要捕获异常的类型,属性值一般是异常类型的类全路径。result则是指定捕获异常后需要执行的result结果,该result可以是来自当前的action中声明的result也可以是来自global-result中声明的result。当然一个action中可以声明多个exception-mapping节点,因为一个action请求可能会有多个异常发生。

3.那么我们可以配置全局性的异常映射,让它可以捕获所有action请求处理时的异常吗?

答案是当然可以。struts2为我们提供了global-exception-mapping和global-result节点来配置全局的异常捕获映射。需要注意的是global-exception-mapping下的exception-mapping元素只能引用global-results下配置的某个result元素。

4.struts2中声明时异常处理机制由ExceptionMappingInterceptor拦截器负责处理的,当exception-mapping中配置的异常被捕获到时ExceptionMappingInterceptor拦截器就会向值栈中的Object stack中添加两个对象一个是exception:表示被捕获异常的Exception对象,另一个是exceptionStack:包含被捕获异常的栈。

5.需要注意的两点总结:

①exception属性等于""(空)可以捕获所有类的exception异常。

②发生异常时从struts2中struts.xml配置的异常映射的顺序查找如果和exception类型匹配就执行result结果

③声明时异常的作用域,如果action中配置了异常映射优先匹配action中的异常映射,如果没有此异常映射那么就到全局的异常映射中寻找并执行result结果。

下面展示一个exception-mapping映射的例子:

Action类User类的声明:

 

 
  1. package com.yd.action;

  2.  
  3. import java.io.File;

  4. import java.io.FileInputStream;

  5. import java.io.InputStream;

  6. import java.io.Serializable;

  7. import java.util.ArrayList;

  8. import java.util.List;

  9. import java.util.Map;

  10.  
  11. import com.opensymphony.xwork2.ActionContext;

  12. import com.opensymphony.xwork2.interceptor.ExceptionHolder;

  13. import com.opensymphony.xwork2.util.CompoundRoot;

  14. import com.opensymphony.xwork2.util.ValueStack;

  15.  
  16. public class User implements Serializable{

  17. /**

  18. *

  19. */

  20. private static final long serialVersionUID = 1L;

  21. private String name;

  22. private String hobby;

  23.  
  24. public String getName() {

  25. return name;

  26. }

  27. public void setName(String name) {

  28. this.name = name;

  29. }

  30. public String getHobby() {

  31. return hobby;

  32. }

  33. public void setHobby(String hobby) {

  34. this.hobby = hobby;

  35. }

  36. @Override

  37. public String toString() {

  38. return "User [name=" + name + ", hobby=" + hobby + "]";

  39. }

  40. public String exception(){

  41. //int result=10/0; //java.lang.ArithmeticException

  42. String name=null;

  43. name.length(); //java.lang.NullPointerException

  44. return "exception";

  45. }

  46. }

struts.xml配置如下:

 

 
  1. <?xml version="1.0" encoding="UTF-8" ?>

  2. <!DOCTYPE struts PUBLIC

  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

  4. "http://struts.apache.org/dtds/struts-2.3.dtd">

  5.  
  6. <struts>

  7. <!-- 设置允许使用ognl调用静态方法 -->

  8. <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>

  9. <package name="user" extends="struts-default" namespace="/user">

  10. <!-- 全局结果 -->

  11. <global-results>

  12. <result name="show3">/show3.jsp</result>

  13. <result name="show4">/show4.jsp</result>

  14. </global-results>

  15. <!-- 全局异常映射 result必须引用全局结果集中的result -->

  16. <global-exception-mappings>

  17. <exception-mapping result="show3" exception="java.lang.ArithmeticException"></exception-mapping>

  18. </global-exception-mappings>

  19. <action name="user-login" class="com.yd.action.User" method="getObject">

  20. <result name="user">/show.jsp</result>

  21. </action>

  22. <action name="user-exception" class="com.yd.action.User"

  23. method="exception">

  24. <!-- 给action配置声明式异常 -->

  25. <exception-mapping result="show4" exception="java.lang.ArithmeticException"></exception-mapping>

  26. <exception-mapping result="show3" exception="java.lang.NullPointerException"></exception-mapping>

  27. <exception-mapping result="exception-show2" exception=""></exception-mapping>

  28. <result name="exception-show">/eshow.jsp</result>

  29. <result name="exception-show2">/show2.jsp</result>

  30. <result name="exception">/show.jsp</result>

  31. </action>

  32. </package>

  33. </struts>

index.jsp页面如下:

 

 
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"

  2. pageEncoding="UTF-8"%>

  3. <%@taglib prefix="s" uri="/struts-tags" %>

  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  5. <html>

  6. <head>

  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  8. <title>首页</title>

  9. </head>

  10. <body>

  11. <s:debug></s:debug>

  12. <a href="/Struts2_4_2/user/user-exception.action">user-exception</a>

  13. </body>

  14. </html>

show3.jsp页面如下:

 

 
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"

  2. pageEncoding="UTF-8"%>

  3. <%@taglib prefix="s" uri="/struts-tags"%>

  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  5. <html>

  6. <head>

  7.  
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  9. <title>Insert title here</title>

  10. </head>

  11. <body>

  12. show3.jsp

  13. <s:debug></s:debug>

  14. </body>

  15. </html>

运行结果如下:

如运行的结果显示当捕获异常后拦截器在Object Stack中添加了exception,和exceptionStack对象。

https://blog.csdn.net/nihaowoshiyudong/article/details/54561769

猜你喜欢

转载自blog.csdn.net/xiao190128/article/details/81667371