Results 1 to 5 of 5

Thread: Problem with exception resolver and view errors

Hybrid View

  1. #1

    Default 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. #2
    Join Date
    Oct 2006
    Posts
    100

    Default

    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.

  3. #3

    Default 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;
    }

  4. #4
    Join Date
    Oct 2006
    Posts
    100

    Default

    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"));
    %>

  5. #5

    Default

    Great that solves my problem.
    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •