10.struts2_声明式异常的处理
来源:互联网 发布:js水平时间轴 编辑:程序博客网 时间:2024/05/24 07:09
1.异常处理:exception-mapping元素
struts.xml中配置 exception-mapping标签,可以捕捉在Action中出现的异常,
exception:需要捕捉的异常名称
result:出现异常后,跳转到哪个result
<struts><!-- 修改当前 Struts 应用的主题 --><constant name="struts.ui.theme" value="simple"></constant> <package name="default" namespace="/" extends="struts-default"><action name="product-save" class="com.atguigu.struts.valuestack.Product"method="save"><exception-mapping result="input" exception="ArithmeticException"></exception-mapping><result name="input">/input.jsp</result><result>/details.jsp</result></action><action name="emp-*"class="com.atguigu.struts.valuestack.app.Employee"method="{1}"><result name="{1}">/emp-{1}.jsp</result></action> </package></struts>
Struts2框架的ExceptionMappingInterceptor拦截器会将Exception放入值栈。
@Override public String intercept(ActionInvocation invocation) throws Exception { String result; try { result = invocation.invoke(); } catch (Exception e) { if (isLogEnabled()) { handleLogging(e); } List<ExceptionMappingConfig> exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings(); ExceptionMappingConfig mappingConfig = this.findMappingFromExceptions(exceptionMappings, e); if (mappingConfig != null && mappingConfig.getResult()!=null) { Map parameterMap = mappingConfig.getParams(); // create a mutable HashMap since some interceptors will remove parameters, and parameterMap is immutable invocation.getInvocationContext().setParameters(new HashMap<String, Object>(parameterMap)); result = mappingConfig.getResult(); publishException(invocation, new ExceptionHolder(e)); } else { throw e; } } return result; }源码中的publishException
protected void publishException(ActionInvocation invocation, ExceptionHolder exceptionHolder) { invocation.getStack().push(exceptionHolder); }
对应页面可以打印出错误信息 input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><s:debug></s:debug><s:property value="exceptionStack"/><br>异常对象:<s:property value="exception.message"/><br><br><br><form action="product-save.action" method="post">ProductName: <input type="text" name="productName"/><br><br>ProductDesc: <input type="text" name="productDesc"/><br><br>ProductPrice: <input type="text" name="productPrice" /><br><br><input type="submit" value="Submit"/><br><br></form></body></html>
阅读全文
0 0
- 10.struts2_声明式异常的处理
- Struts2_声明式异常处理
- Struts2_声明式异常处理_实现
- 9.struts2_声明式异常处理
- Struts2_学习笔记(九)----bbs2009、声明式的异常处理、default-action-ref(BUG)
- 声明式异常处理
- 声明式异常处理
- 声明式异常处理
- Struts2的声明式异常处理
- Struts2的声明式异常处理
- Struts2的声明式异常处理
- Struts2的声明式异常处理
- Struts2的声明式异常处理
- Struts2的声明式异常处理
- Struts2的声明式异常处理
- Struts2的声明式异常处理
- Struts2的声明式异常处理
- Struts2的声明式异常处理
- APP弱网模拟测试
- 约瑟夫环问题
- Erlang学习系列
- UVa 843
- Maven搭建Spring+Struts2+Hibernate项目详解
- 10.struts2_声明式异常的处理
- 【uoj35】后缀排序
- Python打开含中文字符文件的血案
- 2017第八届蓝桥杯C/C++ B组省赛题解(下)
- java服务端和android支付宝支付
- 用Maven创建第一个web项目
- 基于Vue 2和高德地图的地图组件
- 一行代码搞定漂亮的Android6.0权限申请界面
- spring都注入哪些东西