views:

33

answers:

1

I've an ErrorFilter which extends the spring GenericFilterBean. I want to show an error page decorated with tiles if some error happens. Is there any way to set a view name from the filter?

<filter>
    <filter-name>errorFilter</filter-name>
    <filter-class>com.abc.filter.ErrorFilter</filter-class>
    <init-param>
        <param-name>errorPage</param-name>
        <param-value>/jsp/errorpage.jsp</param-value>
    </init-param>
</filter>

This is the configuration in web.xml and the doFilter method in errorfilter is the following:

public void doFilter(ServletRequest req, ServletResponse resp,
        FilterChain chain) throws IOException, ServletException {

    HttpServletRequest httpReq = (HttpServletRequest) req;
    StringBuffer reqUrl = httpReq.getRequestURL();
    try {
        chain.doFilter(req, resp);
    } catch (Exception ex) {
        String requestRepresentation = createRequestRepresentation(req);
        errorService.handleException(reqUrl.toString(), ex, requestRepresentation);
        req.getRequestDispatcher(
                getFilterConfig().getInitParameter("errorPage")).forward(req, resp);
    } catch (Error er) {
        errorService.handleError(reqUrl.toString(), er);
        req.getRequestDispatcher(
                getFilterConfig().getInitParameter("errorPage")).forward(req, resp);
    }
}

the current errorpage is not docorated with tiles... so i want to decorate it with normal header and footer and call that view name from the filer.. is it possible ?

+1  A: 

There must be a way to do it with Spring MVC, but i suppose a redirect is enough.

try {
    chain.doFilter(req, resp);
} catch (Exception ex) {
    if (response.isCommitted()) {
        log(ex); // at least a part of the response is already sent to the client, you can't do much about it
    } else {
        session.setAttribute("last.exception", ex);
        response.sendRedirect(request.getContextPath() + getFilterConfig().getInitParameter("errorPage"));
    }
}

P.S. don't catch an Error!

iimuhin