freemarker 异常处理
生活随笔
收集整理的這篇文章主要介紹了
freemarker 异常处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SSH2處理方案:
freemarker文件如果出錯,網站的前臺頁面會報出很明顯的錯誤-焦黃的背景,血紅的文字,很不利于用戶體驗的。如何修改這個問題呢?
首先需要在struts.xml配置文件里添加下面一行代碼:
| 1 | <constant name="struts.freemarker.manager.classname" value="net.swiftlet.freemarker.MyFreemarkerManager" /> |
接著新建MyFreemarkerManager類,如下所示:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class MyFreemarkerManager extends org.apache.struts2.views.freemarker.FreemarkerManager { ????private static final Logger LOG = LoggerFactory.getLogger(MyFreemarkerManager.class); ????public void init(ServletContext servletContext) throws TemplateException ????{ ????????config = createConfiguration(servletContext); ????????config.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER); ????????contentType = DEFAULT_CONTENT_TYPE; ????????wrapper = createObjectWrapper(servletContext); ????????if (LOG.isDebugEnabled()) ????????{ ????????????LOG.debug("Using object wrapper of class " + wrapper.getClass().getName()); ????????} ????????config.setObjectWrapper(wrapper); ????????templatePath = servletContext.getInitParameter(INITPARAM_TEMPLATE_PATH); ????????if (templatePath == null) ????????{ ????????????templatePath = servletContext.getInitParameter("templatePath"); ????????} ????????configureTemplateLoader(createTemplateLoader(servletContext, templatePath)); ????????loadSettings(servletContext); ????} } |
?
SpringMVC-Freemarker異常配置
?
1、spring mvc的異常配置
? ? spring mvc 提供了SimpleMappingExceptionResolver來處理異常,這里的只是由web 請求,經由controller引發的異常,無法處理freemarker的異常
?
2、freemarker自定義異常
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** ?*?freemarker頁面上的異常控制 ?*?在webmvc-config.xml里面的freemarkerSettings里頭配置 ?*?@author?scipio ?*?@created?2014-02-01 ?*/ public?class?FreemarkerExceptionHandler?implements?TemplateExceptionHandler?{ ????private?static?final?Logger?log?=?LoggerFactory ????????????.getLogger(FreemarkerExceptionHandler.class); ????public?void?handleTemplateException(TemplateException?te,?Environment?env, ????????????????????????????????????????Writer?out)?throws?TemplateException?{ ????????????log.warn("[Freemarker?Error:?"?+?te.getMessage()?+?"]"); ????????????throw?new?ViewException("freemarker?error",te); ????} } |
?
?
? 這里重新包裝異常拋出(這種方式要求比較嚴格,因為freemarker對個別的錯誤,比如某個變量沒有定義,可以忽略,只是后臺拋異常,然后整個頁面還是會渲染出來)
?
3、配置HtmlFreeMarkerConfigurer
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ?<bean?id="freemarkerConfig"?class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> ????????<property?name="templateLoaderPath"?value="/WEB-INF/views/"/> ????????<property?name="freemarkerSettings"> ????????????<props> ????????????????<prop?key="template_update_delay">0</prop> ????????????????<prop?key="default_encoding">UTF-8</prop> ????????????????<prop?key="locale">zh_CN</prop> ????????????????<prop?key="url_escaping_charset">UTF-8</prop> ????????????????<prop?key="whitespace_stripping">true</prop> ????????????????<prop?key="number_format">#</prop> ????????????????<!--?配置自定義的freemarker異常處理--> ????????????????<prop?key?=?"template_exception_handler">com.persia.exception.FreemarkerExceptionHandler</prop> ????????????</props> ????????</property> ????</bean> |
?
?
4、針對該異常,配置web.xml
?
| 1 2 3 4 5 | <!--配置freemarker異常--> ????<error-page> ????????<exception-type>com.persia.exception.ViewException</exception-type> ????????<location>/WEB-INF/views/error/viewException.ftl</location> ????</error-page> |
?
轉載于:https://www.cnblogs.com/cz-xjw/p/4987232.html
總結
以上是生活随笔為你收集整理的freemarker 异常处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言开方
- 下一篇: MyBatis 实践