推荐:Spring学习基础---配置文件1,配置文件的配置头 ?xmlversion=1.0encoding=UTF-8? !-- -ApplicationcontextdefinitionforJPetStore’sbusinesslayer. -ContainsbeanreferencestothetransactionmanagerandtotheDAOsin -dataAccessContext-local/jta.xml(seeweb.xml’scontextConfigLo
配置Spring异常处理之需要增加一个bean的配置:
<!-- Exception Resolver-->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView">
<value>/exception/failure</value>
</property>
<property name="exceptionMappings">
<props>
<prop key="java.sql.SQLException">/exception/showDBError</prop>
<prop key="java.lang.RuntimeException">/exception/showError</prop>
</props>
</property>
</bean>
这样就可以统一分别处理不同Exception了。注意jsp页面在request中attribute等于“exception”,
而不是“Exception”注意大小写。页面如下:
错误显示页面
<c:set value="${exception}" var="ee"/>
<jsp:useBean id="ee" type="java.lang.Exception" />
<%=ee.getMessage()%><br>
<%ee.printStackTrace( new java.io.PrintWriter(out));%>
当然也可以做得更友好些,例如可以显示隐藏详细信息。
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ page contentType="text/html;charset=GBK" language="java" pageEncoding="GBK"%>
<%@ page import="java.util.Enumeration,java.util.Iterator"%>
<script>
function showErr(){
var isHidde = document.all.isHidde.value;
//alert(isHidde);
if( isHidde == "true" ){
document.all.errdiv.style.display=’block’;
document.all.isHidde.value= ’false’;
document.all.showbtn.value="隐藏错误信息";
}else{
document.all.errdiv.style.display=’none’;
document.all.isHidde.value= ’true’;
document.all.showbtn.value="显示错误信息";
}
}
</script>
<html>
<head>
<title>this is failure</title>
</head>
<body onload="showErr()">
<c:set value="${exception}" var="ee"/>
<jsp:useBean id="ee" type="java.lang.Exception" />
<%=ee.getMessage()%>ok,<br>
<table id="errdiv" align="center" bgcolor="darkseagreen">
<tr><td>
<font color=red>
<%ee.printStackTrace( new java.io.PrintWriter(out));%>
</font>
</td></tr></table>
<input type="hidden" id="isHidde" value="true"/>
<input type="button" id="showbtn" onclick="showErr();"/>
</body>
</html>
分享:Spring学习基础---多框架集成ApplicationContextctx 1,定义资源文件获得资源文件的消息,国际化信息 beanid=messageResourceclass=org.springFramework.context.support.ResourceBoundleMessageSource propertyname=basenames xxxx /property /bean 将会搜索xxxx.properties,xxxx_