【问题标题】:How do I have common error page templates with tiles in a Spring/MVC 3.0 app?如何在 Spring/MVC 3.0 应用程序中使用带有磁贴的常见错误页面模板?
【发布时间】:2011-04-02 22:10:34
【问题描述】:

我有一个使用磁贴作为视图的 Spring MVC/3.0 应用程序,这工作正常,但是我不知道如何让错误页面也使用磁贴。

我有我的web.xml

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

作为不使用图块的普通视图可以正常工作,但是当我将位置更改为视图名称之一时,找不到该视图并呈现普通错误页面。

我的tiles.xml 视图文件包含以下定义

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

我正在通过 spring 配置图块,如下所示:

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

我怀疑这完全是由于视图不是来自 spring 本身?

【问题讨论】:

    标签: java spring jsp spring-mvc tiles


    【解决方案1】:

    您需要在 web.xml 中添加“布局的”jsp。下面是解释代码:

    // 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>
    

    【讨论】:

    • 奈库斯,我感激不尽。这就像一个魅力。我是来自 .NET 的 spring、spring-mvc、tiles 的新手。像这样的事情有很长的路要走,谢谢。
    【解决方案2】:

    在图块中定义错误模板会更简单:

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

    并使用 Spring MVC 处理,例如:

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

    在这种情况下,您不必将错误页面添加到 web.xml,每个错误页面一个 .jsp 文件就足够了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 2013-10-21
      • 1970-01-01
      相关资源
      最近更新 更多