1、把freemark相关jar包引入到工程中,Maven项目直接在pom.xml中添加如下代码:
org.freemarker freemarker 2.3.18 2、在src/main/resources下新建freemarker.properties配置文件,内容如下:tag_syntax=auto_detect
template_update_delay=30
default_encoding=UTF-8
output_encoding=UTF-8
date_format=yyyy-MM-dd
time_format=HH:mm:ss
datetime_format=yyyy-MM-dd HH:mm:ss
classic_compatible=true
template_exception_handler=ignore
配置这个是为了解决freemarker返回HTML页面中文乱码问题
3、在spring-mvc.xml中添加视图解析器相关代码:
<!-- spring-mvc jsp视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/html/"></property>
<property name="suffix" value=".jsp"></property>
<property name="order" value="1"></property>
</bean>
<!-- 设置freeMarker的配置文件路径 -->
<bean id="freemarkerConfiguration"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:freemarker.properties" />
</bean>
<!-- html视图解析器 必须先配置freemarkerConfig,注意html是没有prefix前缀属性的 -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="freemarkerSettings" ref="freemarkerConfiguration" />
<property name="templateLoaderPath">
<value>/WEB-INF/page/</value>
</property>
</bean>
<bean id="htmlviewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix" value=".html" />
<property name="order" value="0"></property>
<property name="contentType" value="text/html;charset=UTF-8"></property>
</bean>