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>