Skip to content
Snippets Groups Projects
Commit 16b1476c authored by Mic's avatar Mic
Browse files

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
parent 3d22e379
No related branches found
No related tags found
No related merge requests found
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;
......
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html lang="en"> <html lang="en">
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<jsp:include page="header.jsp"/> <jsp:include page="header.jsp"/>
<body> <body>
<div id="main"> <div class="container">
<% <spring:url value="/resources/images/banner-graphic.png" var="banner"/>
Exception ex = (Exception) request.getAttribute("exception"); <img src="${banner}" />
%> <spring:url value="/resources/images/pets.png" var="petsImage"/>
<img src="${petsImage}" />
<h2>Data access failure: <%= ex.getMessage() %></h2> <h2>Something happened...</h2>
<p/> <p>${exception.message}</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"/> <jsp:include page="footer.jsp"/>
</div>
</body> </body>
</html> </html>
<%@ 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>
...@@ -75,8 +75,6 @@ ...@@ -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 @@ ...@@ -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 @@ ...@@ -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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment