Announcement Announcement Module
Collapse
No announcement yet.
Problem with exception resolver and view errors Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with exception resolver and view errors

    Hello Spring Users.

    I am having trouble with viewing error logs when I have a exception resolver set in my *-servlet.xml

    Here is my exception resolver.


    <!-- Exception Resolver -->
    <bean id="exceptionResolver"
    class="org.springframework.web.servlet.handler.Sim pleMappingExceptionResolver">
    <property name="exceptionMappings">
    <props>
    <!-- The value of the property will get resolved by the view resolver to display the error page -->
    <prop key="java.lang.Exception">Error</prop>
    </props>
    </property>
    </bean>


    Example situation :
    Here is a method in my controller

    public ModelAndView handleRemoveReusableUrl(HttpServletRequest request,
    HttpServletResponse response) throws ServletException {


    ModelAndView mav = new ModelAndView("redirectollUrls.htm");


    service.save();


    return mav;
    }


    in this method, the save() throws a DB exception say a DataAccessException.
    Now I dont want to catch the exception here and I want to show the error page , as defined in the Exception handller. But I also want to error stack trace to be available in the logs.

    How can I achieve this so thatI can see error message and do the necessary debugging.


    Thanks
    Aayush.

  • #2
    on the jsp page tou can use:
    Code:
    <div class="error">
            <c:forEach items="${exception.stackTrace}" var="stack">
                <c:out value="${stack}" /><br/>
            </c:forEach>
    </div>
    exception is an object available on the modelAndView.

    Comment


    • #3
      Logging error in the log files

      Thanks for the reply,

      The method you suggested will display the error stack trace on the jsp error page.
      But I am looking to log the error in a log file.
      Does that mean I have to catch the error , log it and rethrow it.

      Something like:
      public ModelAndView handleRemoveReusableUrl(HttpServletRequest request,
      HttpServletResponse response) throws ServletException {


      ModelAndView mav = new ModelAndView("redirectollUrls.htm");

      try{
      service.save();
      }catch(DataAccessException dae){
      logger.error("Error occured during save");
      dae.printstacktrace();

      throw new Exception();
      }

      return mav;
      }

      Comment


      • #4
        No.

        On the jsp if you use the following code, it will log the error for you.
        Code:
        <%
        final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(getClass());
        log.error(request.getAttribute("exception"));
        %>

        Comment


        • #5
          Great that solves my problem.
          Thank you.

          Comment

          Working...
          X