Simplified exception page handling

- so we don't use JSP scriptlets anymore
- Used input from http://www.thymeleaf.org/petclinic.html
- also some improvements to SimpleMappingExceptionResolver make it
easier to get the error message from the JSP
This commit is contained in:
Mic 2013-01-16 12:46:23 +08:00
parent 3d22e37944
commit 16b1476c40
5 changed files with 30 additions and 121 deletions

View file

@ -1,12 +1,20 @@
package org.springframework.samples.petclinic.web; package org.springframework.samples.petclinic.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.samples.petclinic.Clinic; import org.springframework.samples.petclinic.Clinic;
import org.springframework.samples.petclinic.PetType;
import org.springframework.samples.petclinic.Vets; import org.springframework.samples.petclinic.Vets;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;

View file

@ -1,30 +0,0 @@
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html lang="en">
<jsp:include page="header.jsp"/>
<body>
<div id="main">
<%
Exception ex = (Exception) request.getAttribute("exception");
%>
<h2>Data access failure: <%= ex.getMessage() %></h2>
<p/>
<%
ex.printStackTrace(new java.io.PrintWriter(out));
%>
<p/>
<br/>
<a href="<spring:url value="/" htmlEscape="true" />">Home</a>
</div>
<jsp:include page="footer.jsp"/>
</body>
</html>

View file

@ -0,0 +1,21 @@
<html lang="en">
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<jsp:include page="header.jsp"/>
<body>
<div class="container">
<spring:url value="/resources/images/banner-graphic.png" var="banner"/>
<img src="${banner}" />
<spring:url value="/resources/images/pets.png" var="petsImage"/>
<img src="${petsImage}" />
<h2>Something happened...</h2>
<p>${exception.message}</p>
<jsp:include page="footer.jsp"/>
</div>
</body>
</html>

View file

@ -1,64 +0,0 @@
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="en">
<jsp:include page="header.jsp"/>
<body>
<div id="main">
<h2>Internal error</h2>
<p/>
<%
try {
// The Servlet spec guarantees this attribute will be available
Throwable exception = (Throwable) request.getAttribute("javax.servlet.error.exception");
if (exception != null) {
if (exception instanceof ServletException) {
// It's a ServletException: we should extract the root cause
ServletException sex = (ServletException) exception;
Throwable rootCause = sex.getRootCause();
if (rootCause == null)
rootCause = sex;
out.println("** Root cause is: "+ rootCause.getMessage());
rootCause.printStackTrace(new java.io.PrintWriter(out));
}
else {
// It's not a ServletException, so we'll just show it
exception.printStackTrace(new java.io.PrintWriter(out));
}
}
else {
out.println("No error information available");
}
// Display cookies
out.println("\nCookies:\n");
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
out.println(cookies[i].getName() + "=[" + cookies[i].getValue() + "]");
}
}
} catch (Exception ex) {
ex.printStackTrace(new java.io.PrintWriter(out));
}
%>
<p/>
<br/>
</div>
<jsp:include page="footer.jsp"/>
</body>
</html>

View file

@ -75,8 +75,6 @@
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="messages/messages"/> p:basename="messages/messages"/>
<!-- <!--
Processes annotated handler methods, applying PetClinic-specific request parameter binding. Processes annotated handler methods, applying PetClinic-specific request parameter binding.
--> -->
@ -93,32 +91,10 @@
- here with all other types of exceptions. - here with all other types of exceptions.
--> -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings"> <property name="defaultErrorView" value="exception"/>
<props>
<prop key="org.springframework.web.servlet.PageNotFound">pageNotFound</prop>
<prop key="org.springframework.dao.DataAccessException">dataAccessFailure</prop>
<prop key="org.springframework.transaction.TransactionException">dataAccessFailure</prop>
</props>
</property>
<property name="defaultErrorView" value="uncaughtException"/>
</bean> </bean>
<!--
- This view resolver delegates to the InternalResourceViewResolver and BeanNameViewResolver,
- and uses the requested media type to pick a matching view. When the media type is 'text/html',
- it will delegate to the InternalResourceViewResolver's JstlView, otherwise to the
- BeanNameViewResolver. Note the use of the expression language to refer to the contentType
- property of the vets view bean, setting it to 'application/vnd.springsource.samples.petclinic+xml'.
-->
<!-- - The AtomView rendering a Atom feed of the visits --> <!-- - The AtomView rendering a Atom feed of the visits -->
<bean id="visitsList" class="org.springframework.samples.petclinic.web.VisitsAtomView"/> <bean id="visitsList" class="org.springframework.samples.petclinic.web.VisitsAtomView"/>
@ -132,6 +108,4 @@
<oxm:class-to-be-bound name="org.springframework.samples.petclinic.Vets"/> <oxm:class-to-be-bound name="org.springframework.samples.petclinic.Vets"/>
</oxm:jaxb2-marshaller> </oxm:jaxb2-marshaller>
</beans> </beans>