zoukankan      html  css  js  c++  java
  • [原创]java WEB学习笔记60:Struts2学习之路--Actioin-声明式异常处理

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

    内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

    本人互联网技术爱好者,互联网技术发烧友

    微博:伊直都在0221

    QQ:951226918

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    1.exception-mapping 元素: 配置当前 action 的声明式异常处理

    2.exception-mapping 元素中有 2 个属性

       exception: 指定需要捕获的的异常类型。异常的全类名

       result: 指定一个响应结果, 该结果将在捕获到指定异常时被执行, 既可以来自当前 action 的声明, 也可以来自 global-results 声明.

    3.可以通过 global-exception-mappings 元素为应用程序提供一个全局性的异常捕获映射. 但在 global-exception-mappings 元素下声明的任何 exception-mapping 元素只能引用在 global-results 元素下声明的某个 result 元素

    声明式异常处理机制由 ExceptionMappingInterceptor 拦截器负责处理, 当某个 exception-mapping 元素声明的异常被捕获到时, ExceptionMappingInterceptor 拦截器就会向 ValueStack 中添加两个对象:

       exception: 表示被捕获异常的 Exception 对象

       exceptionStack: 包含着被捕获异常的栈

    可以在视图上通过 <s:property> 标签显示异常消息

     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  
     8     <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> 
     9      <package name="default" namespace="/" extends="struts-default">
    10      
    11          <!-- 配置全局 -->
    12          <global-results>
    13              <result name="input">/input.jsp</result>
    14          </global-results>
    15          
    16          <global-exception-mappings>
    17                  <exception-mapping result="input" exception="java.lang.ArithmeticException"></exception-mapping>
    18          
    19          </global-exception-mappings>
    20          
    21          <!-- 在一个action 中配置 -->
    22          <action name="product-save" class="com.jason.struts2.Product" method="save">
    23              <exception-mapping result="input" exception="java.lang.ArithmeticException"></exception-mapping>
    24              <result name="input">/input.jsp</result>
    25              
    26              <result name="success">/details.jsp</result>
    27          </action>
    28          
    29      </package>
    30 
    31     
    32     
    33     
    34 </struts>
  • 相关阅读:
    17. Letter Combinations of a Phone Number
    16. 3Sum Closest
    15. 3Sum
    14. Longest Common Prefix
    13. Roman to Integer
    12. Integer to Roman
    11. Container With Most Water
    10. Regular Expression Matching
    9. Palindrome Number
    8. String to Integer (atoi)
  • 原文地址:https://www.cnblogs.com/jasonHome/p/5838738.html
Copyright © 2011-2022 走看看