1、pom.xml中配置freemarker包
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
2、spring-freemarker.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置Freemarker属性文件路径 -->
<bean id="freemarkerConfiguration"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:freemarker.properties" />
</bean>
<!-- 配置freeMarker模板加载地址 -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!-- 视图解析器会在/WEB-INF/ftl/路径下扫描视图文件 -->
<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
<property name="defaultEncoding" value="utf-8" />
<!--全局变量部分 -->
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape" />
<entry key="html_escape" value-ref="fmHtmlEscape" />
</map>
</property>
</bean>
<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
<bean id="fmHtmlEscape" class="freemarker.template.utility.HtmlEscape" />
<!-- 配置freeMarker视图解析器 -->
<bean id="freemarkerResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
<!-- 给视图解析器配置优先级 -->
<property name="order" value="1" />
<!-- 扫描路径內所有以ftl结尾的文件 -->
<property name="viewNames">
<array>
<value>*.ftl</value>
</array>
</property>
<!--设置编码 -->
<property name="contentType" value="text/html;charset=utf-8" />
<property name="requestContextAttribute" value="request" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="allowSessionOverride" value="true" />
</bean>
</beans>
3、freemarker.properties
datetime_format=yyyy-MM-dd HH:mm:ss
date_format=yyyy-MM-dd
time_format=HH:mm:ss
boolean_format=true,false
whitespace_stripping=true
default_encoding=UTF-8
tag_syntax=auto_detect
url_escaping_charset=UTF-8
classic_compatible=true
template_update_delay=0
locale=zh_CN
number_format=\#0.\#\#\#\#\#
4、web.xml配置
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
5、ftl模板文件(路径:WEB-INF/ftl/home/index.ftl)
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>homepage</title>
<link rel="shortcut icon" href="./webstatic/index/img/logo.ico" type="image/x-icon">
</head>
<body>
<@common type="page/head.htm">${myHtml}</@common>
<div class="content">
<div>${message}</div>
<img src="${base}/webstatic/index/img/home.png"/>
</div>
<@common type="page/foot.htm">${myHtml}</@common>
</body>
</html>
6、controller方法
@RequestMapping("/index")
public String indexPage(Model model) {
model.addAttribute("myHtml", "true");
model.addAttribute("message", "hello");
return "home/index.ftl";
}
网友评论