本文主要是介绍通过SpringMVC集成freemark模板生成页面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、maven依赖
<properties><checkstyle.skip>true</checkstyle.skip><checkstyle.failOnViolation>false</checkstyle.failOnViolation><findbugs.failOnError>true</findbugs.failOnError><pmd.failOnViolation>false</pmd.failOnViolation><spring.version>4.1.9.RELEASE</spring.version><mybatis.version>3.3.0</mybatis.version><junit.version>4.11</junit.version></properties><dependencies><!--SSM框架依赖包--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.6.10</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.6.10</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.2.3</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.2</version></dependency><!--数据源包--><dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>1.4</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.24</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.9</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><dependency><groupId>taglibs</groupId><artifactId>standard</artifactId><version>1.1.2</version></dependency><dependency><groupId>org.codehaus.jackson</groupId><artifactId>jackson-mapper-asl</artifactId><version>1.9.13</version></dependency><dependency><groupId>org.codehaus.jackson</groupId><artifactId>jackson-core-asl</artifactId><version>1.9.13</version></dependency><dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk16</artifactId><version>1.46</version></dependency><dependency><groupId>org.apache.directory.studio</groupId><artifactId>org.apache.commons.codec</artifactId><version>1.8</version></dependency><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.19</version></dependency>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>找回密码-邮件</title>
</head><body style="font-family:Arial, Helvetica, sans-serif,'宋体';font-size:12px;" leftmargin="0" topmargin="0">
<table width="653" border="0" cellspacing="0" cellpadding="0" style="border:1px #e2e2e2 solid; color:#444341; margin:0 auto;"><tr><td colspan="3" height="48" align="center" style="border-bottom:2px solid #fcbb12; font-size:16px; padding-top:30px;"><strong>LUCKY业务管理平台密码重置提醒</strong></td></tr><tr><td width="46"> </td><td width="560" style="padding-top:15px; line-height:24px; padding-bottom:20px;"><p>尊敬的 <b>${employeeName}</b> 先生/女士</p><p>您好!</p><p>您已经申请了重置登录密码,请在24小时内更换您的密码,如果不做任何操作,系统将保留原密码。 <br /><a href="${projectUrl}/employee/changeByToken.do_?token=${token}" style="color:#0953cd;" target="_blank">点击此处</a> 立即更换登录密码。</p><p>如果上述文字点击无效,请把下面网页地址复制到浏览器地址栏中打开: <br /><a href="${projectUrl}/employee/changeByToken.do_?token=${token}" style="color:#0953cd;" target="_blank">${projectUrl}/employee/changeByToken.do_?token=${token}</a></p><p>如有任何疑问或建议,欢迎随时与我们联系。感谢您使用LUCKY业务管理平台。</p><br /><p align="right">LUCKY</p></td><td width="47"> </td></tr>
</table></body>
</html>
<!-- 模板参数配置 --><bean id="freemarker.mailAndMessageConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"><property name="templateLoaderPath"><value>/jsp/template/freemarker/</value></property><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="classic_compatible">true</prop></props></property></bean>
4、定义一个类实现ApplicationListener接口,加载freemark配置
package com.ucar.common.freemark;import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;import java.util.HashMap;
import java.util.Map;
import java.util.Set;/*** Description:freemark模板生成html工厂类 <br>* @version V1.0 2017/7/15 17:01 by 石冬冬-Seig Heil(dd.shi02@zuche.com)创建*/
@SuppressWarnings("rawtypes")
@Component
public class FreemarkGeneralFactory implements ApplicationListener {private Map<String, Configuration> configurationMap = new HashMap<String, Configuration>();/*** 监听容器启动,预加载模板参数对象*/@Overridepublic void onApplicationEvent(ApplicationEvent event) {if (configurationMap.size() == 0 && event instanceof ContextRefreshedEvent) {WebApplicationContext context = (WebApplicationContext) ((ContextRefreshedEvent) event).getApplicationContext();Map<String, FreeMarkerConfigurer> configMap = context.getBeansOfType(FreeMarkerConfigurer.class);Set<String> mapSet = configMap.keySet();for (String key : mapSet) {Configuration configuration = configMap.get(key).getConfiguration();configurationMap.put(key, configuration);}}}/*** 通过freemark把模板转换HTML字符串* @param beanId 参数Bean的XML配置ID* @param templateName 模板文件名(含后缀)* @param params 页面显示使用的组合Map* @return String* @throws Exception 异常*/public String generalHtml(String beanId, String templateName, Map<String, ?> params) throws Exception {Configuration configuration = configurationMap.get(beanId);Template template = configuration.getTemplate(templateName);return FreeMarkerTemplateUtils.processTemplateIntoString(template, params);}
}
5、Controller定义
package com.ucar.common.controller;import com.ucar.common.freemark.FreemarkGeneralFactory;
import com.ucar.passportmanager.controller.PlatformInfoController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;import java.util.HashMap;
import java.util.Map;/*** Description: freemark生成页面Controller<br/>* @version V1.0 2017/7/15 17:06 by 石冬冬(dongdong.shi@ucarinc.com) - Heil Hitler*/
@Controller
@RequestMapping("freemark")
public class FreemarkGeneralController {private final Logger logger = LoggerFactory.getLogger(PlatformInfoController.class);private final String PAGE_PREFIX = "/template/freemarker/";@Autowiredprivate FreemarkGeneralFactory freemarkGeneralFactory;/*** 生成* @return*/@RequestMapping("/general")public String list(Model model){try {Map<String,String> params = new HashMap<String, String>(){{this.put("employeeName","秋夜无霜");this.put("projectUrl","ssm");this.put("token","秋夜无霜");}};String html = this.freemarkGeneralFactory.generalHtml("freemarker.mailAndMessageConfig","getPasswordEmail.ftl",params);//logger.info("html={}",html);model.addAttribute("html",html);} catch (Exception e) {logger.error("生成页面异常",e);}return PAGE_PREFIX.concat("general");}
}
6、输出general.jsp定义
<%@ page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8" /><title>登陆页</title><meta name="keywords" content="服务治理,prism" /><meta name="description" content="服务治理" /><meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head><body class="login-layout">
${html}
</body>
</html>
这篇关于通过SpringMVC集成freemark模板生成页面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!