先参考文章:【Spring MVC】使用IDEA创建Spring MVC的Maven项目 ,创建 Spring MVC 项目。
1、在Maven中添加 FreeMarker jar 包依赖:
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++--> <!-- FreeMarker 模板引擎 --> <!--++++++++++++++++++++++++++++++++++++++++++++++++++++--> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency>
2、在 applicationContext.xml 中加入内容:
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++--> <!-- FreeMarker的配置 --> <!--++++++++++++++++++++++++++++++++++++++++++++++++++++--> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/view"/> <!--上下文地址的获取--> <property name="freemarkerVariables"> <map> <entry key="base" value="#{servletContext.contextPath}"/> </map> </property> <property name="defaultEncoding" value="UTF-8"/> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">10</prop> <prop key="locale">zh_CN</prop> <prop key="datetime_format">yyyy-MM-dd</prop><!-- 时间格式化 --> <prop key="date_format">yyyy-MM-dd</prop> <prop key="number_format">#.##</prop> </props> </property> </bean>
3、在 dispatcher-servlet.xml 中加入内容:
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++--> <!-- FreeMarker的配置 --> <!--++++++++++++++++++++++++++++++++++++++++++++++++++++--> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="cache" value="true"/> <property name="prefix" value=""/> <property name="suffix" value=".ftl"/> <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"/> </bean>
4、新建模板文件:/WEB-INF/view/freemarker.ftl
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>${title}</title> </head> <body> ${content} </body> </html>
5、Controler示例,通过ModelAndView引用一个模板,使用的时候名字不带后缀
package com.jianbao.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/home") public class IndexController { @GetMapping("/index") public ModelAndView index() { ModelAndView mv = new ModelAndView("freemarker"); mv.addObject("title", "Spring MVC And Freemarker"); mv.addObject("content", "Hello world,This is my first spring mvc !"); return mv; } }
6、浏览器访问: http://localhost:8080/home/index,输出
Hello world,This is my first spring mvc !
温馨提示:
可以把模板文件放到 \src\main\resources\templates
目录下(如:Spring Boot 项目的默认模板文件目录就是这个)
然后再调整一下FreeMarker的模板加载目录,即把
<property name="templateLoaderPath" value="/WEB-INF/view"/>
改为:
<property name="templateLoaderPath" value="classpath:/templates"/>
参考:
Spring Boot FreeMarker 模板路径的几种设置方式