Freemarker template error handling with Spring MVC

南楼画角 提交于 2019-12-08 05:03:29

Below change in servlet-context.xml resolved the issue. Now it throws HTTP 500 error which is intercepted by web server and I am able to mast the message by redirecting to pre-defined error page.

<beans:bean id="freemarkerConfig"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <beans:property name="templateLoaderPath" value="/WEB-INF/ftl/" />
        <beans:property name="freemarkerSettings">
            <beans:props>
                <beans:prop key="template_exception_handler">rethrow</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

This is the java-based equivalent configuration:

@Bean 
public FreeMarkerConfigurer freemarkerConfig() throws TemplateException { 
    FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); 
    freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/ftl/");

    Properties settings = new Properties();
    settings.setProperty(freemarker.template.Configuration.TEMPLATE_EXCEPTION_HANDLER_KEY, "rethrow");
    freeMarkerConfigurer.setFreemarkerSettings(settings);
    return freeMarkerConfigurer; 
}

A simple way to change the mode from "debug" to "rethrow" is to configure it in your application.properties:

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