From 16b1476c40959b279f26fc0a41dcbe63259b7486 Mon Sep 17 00:00:00 2001 From: Mic <misvy@vmware.com> Date: Wed, 16 Jan 2013 12:46:23 +0800 Subject: [PATCH] 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 --- .../petclinic/web/ClinicController.java | 8 +++ .../webapp/WEB-INF/jsp/dataAccessFailure.jsp | 30 --------- src/main/webapp/WEB-INF/jsp/exception.jsp | 21 ++++++ .../webapp/WEB-INF/jsp/uncaughtException.jsp | 64 ------------------- src/main/webapp/WEB-INF/petclinic-servlet.xml | 28 +------- 5 files changed, 30 insertions(+), 121 deletions(-) delete mode 100644 src/main/webapp/WEB-INF/jsp/dataAccessFailure.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/exception.jsp delete mode 100644 src/main/webapp/WEB-INF/jsp/uncaughtException.jsp diff --git a/src/main/java/org/springframework/samples/petclinic/web/ClinicController.java b/src/main/java/org/springframework/samples/petclinic/web/ClinicController.java index 4ffcd71..8d5ea2d 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/ClinicController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/ClinicController.java @@ -1,12 +1,20 @@ 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.propertyeditors.CustomDateEditor; +import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.samples.petclinic.Clinic; +import org.springframework.samples.petclinic.PetType; import org.springframework.samples.petclinic.Vets; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; diff --git a/src/main/webapp/WEB-INF/jsp/dataAccessFailure.jsp b/src/main/webapp/WEB-INF/jsp/dataAccessFailure.jsp deleted file mode 100644 index 5674cab..0000000 --- a/src/main/webapp/WEB-INF/jsp/dataAccessFailure.jsp +++ /dev/null @@ -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> diff --git a/src/main/webapp/WEB-INF/jsp/exception.jsp b/src/main/webapp/WEB-INF/jsp/exception.jsp new file mode 100644 index 0000000..35bfc43 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/exception.jsp @@ -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> diff --git a/src/main/webapp/WEB-INF/jsp/uncaughtException.jsp b/src/main/webapp/WEB-INF/jsp/uncaughtException.jsp deleted file mode 100644 index 236a7b9..0000000 --- a/src/main/webapp/WEB-INF/jsp/uncaughtException.jsp +++ /dev/null @@ -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> diff --git a/src/main/webapp/WEB-INF/petclinic-servlet.xml b/src/main/webapp/WEB-INF/petclinic-servlet.xml index 153fd45..d203e4a 100644 --- a/src/main/webapp/WEB-INF/petclinic-servlet.xml +++ b/src/main/webapp/WEB-INF/petclinic-servlet.xml @@ -75,8 +75,6 @@ <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages/messages"/> - - <!-- Processes annotated handler methods, applying PetClinic-specific request parameter binding. --> @@ -93,32 +91,10 @@ - here with all other types of exceptions. --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> - <property name="exceptionMappings"> - <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"/> + <property name="defaultErrorView" value="exception"/> </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 --> <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:jaxb2-marshaller> - - </beans> -- GitLab