Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I have common error page templates with tiles in a Spring/MVC 3.0 app?

I have a Spring MVC/3.0 app using tiles as it's view, this is working fine however I can't figure out how to get the error pages to also use tiles.

I have in my web.xml

<error-page>
  <error-code>404</error-code>
  <location>/WEB-INF/error/404.jsp</location>
</error-page>

which works fine as an ordinary view NOT using tiles, however when I change the location to one of the view names, the view is not found and renders the ordinary error page.

My tiles.xml file for the view contains the following definition

<definition name="404" extends="standardLayout">
  <put-attribute name="body" value="/WEB-INF/error/404.jsp" />
</definition>

I'm configuring tiles through spring as follows:

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
  <property name="definitions">
    <list>
      <value>/WEB-INF/**/tiles.xml</value>
    </list>
  </property>
</bean>

I'm suspecting this is all due to the view not coming from spring itself?

like image 745
Brett Ryan Avatar asked Aug 23 '10 23:08

Brett Ryan


2 Answers

You need to add the "layouted" jsp in your web.xml. Below is the explaination code:

// Your web.xml should look like this:
<error-page>
  <error-code>404</error-code>
  <location>/WEB-INF/error/layout-404.jsp</location>
</error-page>


// Your layout-404.jsp should look like this:
<%@page isELIgnored="false" %>
<%@page contentType="text/html"%>
<%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertDefinition name="404" />    


// Your layout def should look like this:
<definition name="404" extends="standardLayout">
  <put-attribute name="body" value="/WEB-INF/error/404.jsp" />
</definition>
like image 200
naikus Avatar answered Sep 19 '22 09:09

naikus


It would be just simpler to define error template in tiles:

<definition name="error/*" template="/views/error/layout.jsp">
    <put-attribute name="body" value="/views/error/{1}.jsp" />
</definition>

And handle that with Spring MVC, e.g.:

@ExceptionHandler({ MissingResourceException.class })
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleMissingResource(Exception e) {
    return "error/404";
}

In this case, you don't have to add error pages to your web.xml, and one .jsp file per error page will suffice.

like image 39
Seiya Avatar answered Sep 19 '22 09:09

Seiya