SSH笔记-Struts2的exception-mapping标签和全局标签

1、exception-mapping:用于当前action的声明式异常处理,就是出错的时候,设置当前action应该有什么反应和表现

2、有两个属性
- result:指定的相应结果,可指定当前actino的result,也可指定全局result
- exception:指定需要捕获的异常类型

3、全局标签:
- global-exception-mappings:全局异常捕获
- global-results:全局结果
- global-exception-mappings下捕获的异常只能引用global-results下的元素

4、输出异常到页面:
- 可以用< s:property value=”异常参数名”>标签,显示异常信息
- exception:表示异常的Exception对象
- exceptionStack:捕获的异常信息

5、sturts.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.action.extension" value="action,do,"></constant>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    <package name="default" namespace="/" extends="struts-default">

        <global-results>
            <result name="GBexcp">/globalResult.jsp</result>
        </global-results>
        <global-exception-mappings>
            <exception-mapping result="GBexcp" exception="java.lang.Exception"></exception-mapping>     
        </global-exception-mappings>

        <action name="Test_Exception" class="com.demo.sshtest.ExceptionTest" method="Test_Exception">
            <exception-mapping result="excp" exception="java.lang.Exception">
            <result name="excp">/Test_Exception.jsp</result>
        </action>
    </package>
</struts>

6、页面打印异常:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<% request.setCharacterEncoding("UTF-8"); %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Exception</title>
</head>
<body>
    <br><br>
    exception>>>>> <s:property value="exception"/>
    <br><br>
    exceptionStack>>>>> <s:property value="exceptionStack"/>
    <br><br>
    <s:debug></s:debug>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值