本文通过 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. 使用ABAP代码创建S/4HANA里的Sales Order

    下图是使用ABAP代码创建的S/4HANA的Sales Order的截图: 其中红色区域的值是我代码里硬编码的,而蓝色是函数SD_SALESDOCUMENT_CREATE自己创建的. 来看下代码: D ...

  2. MYSQL忘记超级用户密码修改

    #service mysql stop #mysqld_safe --skip-grant-tables 另外开个SSH连接或终端 [root@localhost ~]# mysql mysql> ...

  3. mysql默认字符集问题

    最近在使用mysql的时候出现了奇怪的乱码问题,最开始发现mysql的字符集的确存在一些问题. 经过修改配置文件/etc/my.cnf [mysqld] character-set-server=ut ...

  4. vscode-tfs插件报错:TF30063

    解决方案:删除tfs凭证,然后用vs重新登陆tfs服务器,此时会在电脑上创建要一个新的tfs凭证,然后再用vscode-tfs操作tfs就没有问题了.

  5. DESCryptoServiceProvider 类加密解密

    DESCryptoServiceProvider  点击查看介绍 加密解密辅助类:点击查看 私钥加密 定义:定义一个包装对象来访问加密服务提供程序 (CSP) 版本的数据加密标准 (DES) 算法.  ...

  6. caffe怎么把全连接层转成convolutional层

    caffe中有把fc层转化为conv层的,其实怎么看参数都是不变的,对alex模型来说,第一个fc层的参数是4096X9216,而conv的维度是4096x256x6x6,因此参数个数是不变的,只是需 ...

  7. Spring详解篇之IoC控制反转

    ###一.Spring概况 spring是一个开源框架 是一个轻量的控制反转和面向切面的容器框架 大小和开销都是轻量的. 通过控制反转技术可以达到松耦合的目的 切面编程,允许通过分离应用的业务逻辑. ...

  8. Status bar - iOS之状态栏

    (一)设置状态栏显示和隐藏 1.通过 Info.plist 文件增加字段,控制状态栏全局显示和隐藏 在 Info.plist 文件中增加字段 Status bar is initially hidde ...

  9. WebStrome react-native代码智能提示

    1.clone到本地 git clone https://github.com/virtoolswebplayer/ReactNative-LiveTemplate  2,添加ReactNative. ...

  10. udp回显客户端发送的数据

    这里让客户端给服务端发送的数据被服务端自动发回来 客户端: import socket client_socket = socket.socket(socket.AF_INET, socket.SOC ...