SpringMVC 集成 Freemarker 模板引擎
2024-09-06 22:17:13
本文通过 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>
最新文章
- Win10 UWP开发系列:使用VS2015 Update2+ionic开发第一个Cordova App
- javascript 模式(2)——单例模式
- Neteans 切换用户语言为英语
- 一个很奇怪的问题,程序没有改动加密参数应该也没有变化.但是两次的加密结果却不一致.md5加密问题
- 海量数据相似度计算之simhash短文本查找
- codeforces 499A.Inna and Pink Pony 解题报告
- ---Ubuntu 14.04 虚拟机器和主机时间同步
- APT攻击将向云计算平台聚焦
- Win7-其中的文件夹或文件已在另一个程序中打开
- 解决Windows+Eclipse+Python错误: SyntaxError: Non-ASCII character
- Robotframe work学习之初(二)
- 利用ASP.netCore自带DI(DependencyInjection)实现批量依赖注入
- YYHS-NOIP模拟赛-mine
- Java注解(二):实战 - 直接使用对象列表生成报表
- [Kubernetes]关于 Kubernetes ,你想要的,都在这儿了
- STM32F103 TIM3定时器初始化程序
- MEF在运行时替换插件
- ubuntu wireshark找不到网卡及开启IP转发
- [转载]CentOS 6.3安装Subversion服务器
- CCF CSP 201803-1 跳一跳