方法一 直接在spring-servlet.xml 中进行配置
<bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:freemarker.properties" /> </bean> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPaths" value="/templates" /> <property name="defaultEncoding" value="utf-8" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">0</prop> <prop key="url_escaping_charset">UTF-8</prop> <prop key="locale">UTF-8</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="date_format">yyyy-MM-dd</prop> <prop key="number_format">#.##</prop> <prop key="classic_compatible">true</prop> </props> </property> <property name="freemarkerVariables"> <map> <entry key="BasePath" value="${base.path}" /> <entry key="IncPath" value="${web.root}" /> <entry key="xml_escape" value-ref="fmXmlEscape" /> </map> </property> </bean> <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
freemarker.properties
base.path=localhost:8080
web.root=www.springmvc.com
方法二(推荐)自定义一个视图,继承FreeMarkerView
public class MyFreeMarkerView extends FreeMarkerView { private static final String CONTEXT_PATH = "base"; @Override protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception { model.put(CONTEXT_PATH, request.getContextPath()); model.put("cxb", "caoxiaobo"); super.exposeHelpers(model, request); } }
<!--视图解释器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="exposeRequestAttributes" value="true" /> <property name="exposeSessionAttributes" value="true" />
<!-- 自定义视图 -->
<property name="viewClass"> <value>com.view.freemarker.MyFreeMarkerView</value> </property> <property name="cache" value="true" /> <!-- <property name="prefix" value="/" /> --> <property name="suffix" value=".ftl"/> <property name="contentType" value="text/html;charset=UTF-8"></property> </bean>
方法三 自定义一个 FreeMarkerConfigurer 继承 FreeMarkerConfigurationFactory 并实现FreeMarkerConfig, InitializingBean, ResourceLoaderAware, ServletContextAware
// 这里其实就是完整的替换了FreeMarkerConfigurer
public class MyFreeMarkerConfigurer extends FreeMarkerConfigurationFactory implements FreeMarkerConfig, InitializingBean, ResourceLoaderAware, ServletContextAware { private Configuration configuration; private TaglibFactory taglibFactory; @Override public void afterPropertiesSet() throws IOException, TemplateException { if (this.configuration == null) { this.configuration = createConfiguration(); } SimpleHash model = new SimpleHash(); model.put("baseUrl", "www.springmvc.com"); this.configuration.setAllSharedVariables(model); } // ... 省略很多方法 }
<bean id="freemarkerConfig" class="com.view.freemarker.MyFreeMarkerConfigurer"> <property name="templateLoaderPath" value="/" /> <property name="freemarkerSettings"> <props> <prop key="locale">zh_CN</prop> <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="classic_compatible">true</prop> <prop key="template_exception_handler">ignore</prop> </props> </property> </bean>