Apache Tiles和Spring MVC中的全局异常页面 [英] Global exception page in Apache Tiles and Spring MVC
问题描述
当抛出未处理的异常时(例如 RuntimeException
),我想显示一个常见的错误页面。
When an unhandled exception is throws (e.g. a RuntimeException
), then I want to show a common error page.
我想实现一些目标:
- 重用HTML模板(使用带标题的常见框架)和将异常信息放入正文中
- 在正文文档中提供有关异常的一些基本信息
我正在使用Apache Tiles和Spring MVC。什么是解决我问题的好方法?
I am using Apache Tiles and Spring MVC. What is a good approach to my problem?
我的部分瓷砖定义:
<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>
理想情况下,我想通过设置 body
属性...
Ideally I'd like to specify a definition for an exception page by setting the body
attribute...
推荐答案
假设你有Spring的 TilesViewResolver
和 TilesConfigurer
已配置,您可以尝试以下bean定义:
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>
这会将任何 Throwable
转发到右边视图,您可以访问异常本身( $ {exception}
)。这不会替换所有标准HTTP错误页面(对于404等)
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.)
这篇关于Apache Tiles和Spring MVC中的全局异常页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!