Global exception page in Apache Tiles and Spring MVC

情到浓时终转凉″ 提交于 2019-12-07 13:37:00

问题


When an unhandled exception is throws (e.g. a RuntimeException), then I want to show a common error page.

I want to achieve a few things:

  • reuse a HTML template (use a common "frame" with header etc.) and place exception info in the body
  • provide some basic info about the exception in the body document

I am using Apache Tiles and Spring MVC. What is a good approach to my problem?

Part of my tiles-definitions:

<tiles-definitions>
    <definition name="common" template="/WEB-INF/jsp/common.jsp">
        <put-attribute name="header" value="header"/>
        <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp"/>
    </definition>
   ...
   <definition name="main" extends="common">
       <put-attribute name="body" value="/WEB-INF/jsp/main.jsp"/>
   </definition>
</tiles-definitions>

Ideally I'd like to specify a definition for an exception page by setting the body attribute...


回答1:


Assuming you have Spring's TilesViewResolver and TilesConfigurer configured, you can try the following bean definition:

<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
   <property name="exceptionMappings">
      <props>
         <prop key="java.lang.Throwable">error</prop>
      </props>
   </property>
</bean>

And then simply define the logical view error:

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

This will forward any Throwable to the right view, where you have access to the exception itself (${exception}). This doesn't replace all standard HTTP error pages (for 404 etc.)




回答2:


<tiles-definitions>
    <definition name="common" template="/WEB-INF/jsp/common.jsp">
        <put-attribute name="header" value="header"/>
        <put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp"/>
    </definition>
   <definition name="common11" template="/WEB-INF/jsp/common11.jsp">//common11 is same page as common.jsp
        <put-attribute name="header" value=""/>
        <put-attribute name="footer" value=""/>
    </definition>
   ...
   <definition name="main" extends="common">
       <put-attribute name="body" value="/WEB-INF/jsp/main.jsp"/>
       <put-attribute name="header" value=""/>
        <put-attribute name="footer" value=""/>
   </definition>
</tiles-definitions>



回答3:


If you are using struts 2.0 then in struts.xml, You can use as following, Or if you are using jsp servlet then you can use following in web.xml

<global-results>
<result name="error">/WEB-INF/jsp/common.jsp</result>
</global-results>


来源:https://stackoverflow.com/questions/16792830/global-exception-page-in-apache-tiles-and-spring-mvc

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