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

℡╲_俬逩灬. 提交于 2019-11-30 17:26:24

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>

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!