SpringMVC 集成 Freemarker 模板引擎
本文通过 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">
<!– 自动装载,引入Freemarker,用于Freemarker Macro引入 –>
/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 模板引擎的更多相关文章
- SpringMVC 集成 Velocity 模板引擎
		
本文通过 maven 项目中集成 1.引入 SpringMVC 与 Velocity 需要的依赖 <!-- SpringMVC --> <dependency> <gro ...
 - freemarker模板引擎的使用
		
freemarker是一套前端模板引擎,在使用时,要先在web项目中添加freemarker.jar的依赖. 我在这里主要演示spring-mvc整合freemarker模板引擎.项目案例的文件包结构 ...
 - Spring Boot 揭秘与实战(七) 实用技术篇 - FreeMarker 模板引擎
		
文章目录 1. FreeMaker 代替 JSP 作为页面渲染 2. 生成静态文件 3. 扩展阅读 4. 源代码 Spring Boot 提供了很多模板引擎的支持,例如 FreeMarker.Thym ...
 - SpringBoot系列之集成jsp模板引擎
		
目录 1.模板引擎简介 2.环境准备 4.源码原理简介 SpringBoot系列之集成jsp模板引擎 @ 1.模板引擎简介 引用百度百科的模板引擎解释: 模板引擎(这里特指用于Web开发的模板引擎)是 ...
 - FreeMarker模板引擎
		
现在开发的项目,也是基于SOA架构,每个功能接口都是用WebService实现,Web服务的通信协议就是用XML来传输. 以前写WebService都是自动生成XML,没想到这项目竟然要自己定义XML ...
 - Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎
		
前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...
 - Spring Boot 2.0 整合 FreeMarker 模板引擎
		
本篇博文将和大家一起使用Spring Boot 2.0 和FreeMarker 模板引擎整合实战. 1. 创建新的项目 2. 填写项目配置信息 3. 勾选web 模块 4. 勾选freemarker模 ...
 - Spring Boot☞ 使用freemarker模板引擎渲染web视图
		
效果图 代码 package com.wls.integrateplugs.hello.controller; /** * Created by wls on 2017/8/24. */ import ...
 - Java之利用Freemarker模板引擎实现代码生成器,提高效率
		
https://blog.csdn.net/huangwenyi1010/article/details/71249258 java模板引擎freemarker代码生成器 更多 个人分类: 一步一步 ...
 
随机推荐
- 2019.03.02 ZJOI2019模拟赛 解题报告
			
得分: \(10+0+40=50\)(\(T1\),\(T3\)只能写大暴力,\(T2\)压根不会) \(T1\):道路建造 应该是一道比较经典的容斥题,可惜比赛时没有看出来. 由于要求最后删一条边或 ...
 - 基于ngx_lua模块的waf开发实践
			
0x00 常见WAF简单分析 WAF主要分为硬件WAF和软件防火墙,硬件WAF如绿盟的NSFOCUS Web Application Firewall,软件防火墙比较有名的是ModSecurity,再 ...
 - stixel-world代码解读
			
下边缘的求法应该是使用的第二篇论文的方法 上边缘的求法应该是使用的第一篇论文的方法 这是求上边缘的代码: std::vector<float> integralMembership(vma ...
 - 【洛谷P1880】[NOI1995]石子合并
			
石子合并 fmax[l][r]表示合并区间[l,r]的最大分值, fmin[l][r]表示合并区间[l,r]的最小分值 for(k l~r-1) fmax[l][r]=max(fmax[l][r],f ...
 - Java关键字transient和volatile小结
			
转自:http://heaven-arch.iteye.com/blog/1160693 transient和volatile两个关键字一个用于对象序列化,一个用于线程同步,都是Java中比较高阶的话 ...
 - 2.vue脚手架项目配置
			
1.更改网站名: index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
 - cmd文件内容添加到文件内容命令
			
今天需要因为有点SQL文件需要添加修改,但是感觉是做运维工作得当然不能一个一个来了.搞了半天bat才找到这个命令(真是一个不合格的运维) 例如:a.txt 内容添加到 b.txt (不是覆盖,而是在 ...
 - Element表单验证(1)
			
Element表单验证(1) 首先要掌握Element官方那几个表单验证的例子,然后才看下面的教程. Element主要使用了async-validator这个库作为表单验证 async-valida ...
 - BZOJ4128: Matrix(BSGS 矩阵乘法)
			
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 813 Solved: 442[Submit][Status][Discuss] Descriptio ...
 - activemq启动闪退/失败 (Illegal character in hostname at index 5: ws://****:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600)
			
java.net.URISyntaxException: Illegal character in hostname at index 5: ws://****:61614?maximumConnec ...