首页 > 代码库 > Mingyang.net:自定义FreeMarkerView

Mingyang.net:自定义FreeMarkerView

自定义FreeMarkerView的目的是为了放一些公共的变量到FreeMarker模版里面.
spring-context.xml:

<!-- **********************************************************************      ** FreeMaker解析器                                                      **      ********************************************************************** --><bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">    <!--<property name="viewClass" value="http://www.mamicode.com/org.springframework.web.servlet.view.freemarker.FreeMarkerView" />-->    <property name="viewClass" value="http://www.mamicode.com/net.mingyang.cms.util.FtlView" />       <property name="contentType" value="text/html;charset=UTF-8" />    <property name="exposeRequestAttributes" value="true" />      <property name="exposeSessionAttributes" value="true" />      <property name="exposeSpringMacroHelpers" value="true" />    <!-- <property name="suffix" value="http://www.mamicode.com/.ftl" /> -->      <property name="order" value="0" /></bean><bean 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="number_format">0.##########</prop>             <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>            <prop key="date_format">yyyy-MM-dd</prop>             <prop key="classic_compatible">true</prop>             <prop key="template_exception_handler">ignore</prop>         </props>     </property> </bean>

FtlView.java:

package net.mingyang.cms.util;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.springframework.web.servlet.view.freemarker.FreeMarkerView;public class FtlView extends FreeMarkerView{    @Override    protected void exposeHelpers(Map<String, Object> model,            HttpServletRequest request) throws Exception    {        model.put("contextPath", request.getContextPath());        super.exposeHelpers(model, request);    }    }