本文通过 maven 项目中集成

1、引入 SpringMVC 与 Freemarker 需要的依赖

<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency> <!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>

2、在resources 目录下创建 SpringMVC 框架配置文件 dispatcherServlet.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> </beans>

3、创建 freemarker 环境配置扩展类 top.jimc.ssm.common.freemarker.FreeMarkerConfigExtend

package top.jimc.ssm.common.freemarker;

import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import java.io.IOException; /**
* freemarker环境配置扩展
* @author Jimc.
* @since 2018/7/3.
*/
public class FreeMarkerConfigExtend extends FreeMarkerConfigurer {
@Override
public void afterPropertiesSet() throws IOException, TemplateException {
super.afterPropertiesSet();
Configuration cfg = this.getConfiguration();
// 添加shiro标签
// cfg.setSharedVariable("shiro", new ShiroTags());//shiro标签
}
}

4、添加 freemarker 视图配置扩展类 top.jimc.ssm.common.freemarker.FreeMarkerViewExtend

package top.jimc.ssm.common.freemarker;

import org.springframework.web.servlet.view.freemarker.FreeMarkerView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map; /**
* freemarker视图配置扩展
* @author Jimc.
* @since 2018/7/4.
*/
public class FreeMarkerViewExtend extends FreeMarkerView { /**
* 项目根路径
*/
private static final String CONTEXT_PATH = "ctxPath"; /**
* 静态资源版本(清除缓存)
*/
private static final String STATIC_VERSION = "_v"; @Override
protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) {
try {
super.exposeHelpers(model, request);
} catch (Exception e) {
e.printStackTrace();
// LoggerUtils.error(FreeMarkerViewExtend.class,e, "FreeMarkerViewExtend 加载父类出现异常。请检查。");
} model.put(CONTEXT_PATH, request.getContextPath());// 项目根路径
model.put(STATIC_VERSION, System.currentTimeMillis());// 清除静态资源缓存用
}
}

5、在 dispatcherServlet.xml 中添加集成 freemarker 的相关配置

<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- freemarker环境配置 -->
<bean id="freemarkerConfig" class="top.jimc.ssm.common.freemarker.FreeMarkerConfigExtend">
<!-- 模版位置,这里配置了下面就不用配了 -->
<property name="templateLoaderPath" value="/WEB-INF/ftl" />
<property name="freemarkerSettings"><!-- 一些设置 -->
<props>
<prop key="template_update_delay">0</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="boolean_format">true,false</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.##########</prop>
<prop key="classic_compatible">true</prop>
<prop key="template_exception_handler">ignore</prop>
<!--<prop key="auto_import">
&lt;!&ndash; 自动装载,引入Freemarker,用于Freemarker Macro引入 &ndash;&gt;
/common/meta.ftl as _meta,
/common/header.ftl as _header,
/common/menu.ftl as _menu
</prop>-->
</props>
</property>
</bean> <!-- freemarker视图解析器配置 -->
<bean id="freemarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass" value="top.jimc.ssm.common.freemarker.FreeMarkerViewExtend"/>
<property name="contentType" value="text/html; charset=utf-8" />
<property name="cache" value="true" />
<property name="suffix" value=".ftl" />
<!-- 配置视图的优先级 -->
<property name="order" value="0" />
</bean> </beans>

6、配置 web.xml (启动 SpringMVC 框架)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <!-- 配置SpringMVC容器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置SpringMVC启动的初始化参数 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcherServlet.xml</param-value>
</init-param>
<!-- 容器在启动时加载并初始化这个servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

项目源码下载

SpringMVC 集成 Freemarker 模板引擎的更多相关文章

  1. SpringMVC 集成 Velocity 模板引擎

    本文通过 maven 项目中集成 1.引入 SpringMVC 与 Velocity 需要的依赖 <!-- SpringMVC --> <dependency> <gro ...

  2. freemarker模板引擎的使用

    freemarker是一套前端模板引擎,在使用时,要先在web项目中添加freemarker.jar的依赖. 我在这里主要演示spring-mvc整合freemarker模板引擎.项目案例的文件包结构 ...

  3. Spring Boot 揭秘与实战(七) 实用技术篇 - FreeMarker 模板引擎

    文章目录 1. FreeMaker 代替 JSP 作为页面渲染 2. 生成静态文件 3. 扩展阅读 4. 源代码 Spring Boot 提供了很多模板引擎的支持,例如 FreeMarker.Thym ...

  4. SpringBoot系列之集成jsp模板引擎

    目录 1.模板引擎简介 2.环境准备 4.源码原理简介 SpringBoot系列之集成jsp模板引擎 @ 1.模板引擎简介 引用百度百科的模板引擎解释: 模板引擎(这里特指用于Web开发的模板引擎)是 ...

  5. FreeMarker模板引擎

    现在开发的项目,也是基于SOA架构,每个功能接口都是用WebService实现,Web服务的通信协议就是用XML来传输. 以前写WebService都是自动生成XML,没想到这项目竟然要自己定义XML ...

  6. Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎

    前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...

  7. Spring Boot 2.0 整合 FreeMarker 模板引擎

    本篇博文将和大家一起使用Spring Boot 2.0 和FreeMarker 模板引擎整合实战. 1. 创建新的项目 2. 填写项目配置信息 3. 勾选web 模块 4. 勾选freemarker模 ...

  8. Spring Boot☞ 使用freemarker模板引擎渲染web视图

    效果图 代码 package com.wls.integrateplugs.hello.controller; /** * Created by wls on 2017/8/24. */ import ...

  9. Java之利用Freemarker模板引擎实现代码生成器,提高效率

    https://blog.csdn.net/huangwenyi1010/article/details/71249258  java模板引擎freemarker代码生成器 更多 个人分类: 一步一步 ...

随机推荐

  1. 如何用代码的方式取出SAP C4C销售订单创建后所有业务伙伴的数据

    比如我创建了一个Sales Order(销售订单)后,如何用代码的方式取出这些通过SAP Partner determination自动填充的Involved Parties信息呢? 一种方法可以使用 ...

  2. FYI-django数据库操作-外键

    我先定义两个模型,一个是作者,一个是作者出版的书籍,算是一对多的类型.   class Person(models.Model); name = models.CharField('作者姓名', ma ...

  3. gearmand安装过程

    51 cd boost_1_53_0 52 tail -f build_log 53 dir 54 cd gearmand-1.1.8 55 ./configure 56 could not find ...

  4. failed to bind pixmap to texture

    问题描述:我用的是Ubuntue的操作系统,终端突然挂了.我重启了一下电脑,就进不去系统了. 日志信息:  failed to bind pixmap to texture 原因: 界面管理工具坏了, ...

  5. OpenACC例子

    timeinfo1.c代码 #include<stdio.h> #define N 100 int main() { int A[N]; #pragma acc kernels { ; i ...

  6. jQuery插件的使用和写法

    插件(plugin)也称为扩展(Extension),是一种遵循一定规范的应用程序接口编写出来的程序. jQuery的易扩展性,吸引了来自全球的开发者来共同编写jQuery的插件. jQuery表单验 ...

  7. 2017年10月26日 git上传文件失败的文件

    最近几天因为项目要用git,于是学习了一下git.今天上传项目到码云的时候,却发现总有一些文件夹上传不上去,git 也显示everything is update.找了一圈办法,都没有用,最后突然发现 ...

  8. 【赛时总结】◇赛时·V◇ Codeforces Round #486 Div3

    ◇赛时·V◇ Codeforces Round #486 Div3 又是一场历史悠久的比赛,老师拉着我回来考古了……为了不抢了后面一些同学的排名,我没有做A题 ◆ 题目&解析 [B题]Subs ...

  9. 远程桌面连接失败,提示CredSSP加密Oracel修正问题解决

    今天远程桌面的时候失败了,出现以下提示 于是上网找解决办法,经过测试,该方法是可行的. 首先,在控制台中输入regedit,打开注册表

  10. PHP生成特定长度的纯字母字符串

    PHP中,md5().uniqid()函数可以返回32位和13位不重复的字符串,但是这些字符串都可能包含有数字.如果需要纯字母的字符串,而且长度不定,比如8位,那么直接用这两个函数无法达到效果. 这时 ...