通过SpringMVC集成freemark模板生成页面

1、maven依赖


        true
        false
        true
        false
        4.1.9.RELEASE
        3.3.0
        4.11
    

    
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.aspectj
            aspectjrt
            1.6.10
        
        
            org.aspectj
            aspectjweaver
            1.6.10
        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
            org.mybatis
            mybatis-spring
            1.2.3
        
        
            log4j
            log4j
            1.2.17
        
        
            org.slf4j
            slf4j-log4j12
            1.7.2
        
        
        
            commons-dbcp
            commons-dbcp
            1.4
        
        
            com.alibaba
            fastjson
            1.2.24
        
        
            javax.servlet
            servlet-api
            2.5
            provided
        
        
            junit
            junit
            ${junit.version}
            test
        

        
            org.springframework
            spring-test
            ${spring.version}
        
        
            mysql
            mysql-connector-java
            5.1.9
        

        
            javax.servlet
            jstl
            1.2
        
        
            taglibs
            standard
            1.1.2
        

        
            org.codehaus.jackson
            jackson-mapper-asl
            1.9.13
        
        
            org.codehaus.jackson
            jackson-core-asl
            1.9.13
        
        
            org.bouncycastle
            bcprov-jdk16
            1.46
        
        
            org.apache.directory.studio
            org.apache.commons.codec
            1.8
        

        
            org.freemarker
            freemarker
            2.3.19
        


2、模板定义





找回密码-邮件



LUCKY业务管理平台密码重置提醒
 

尊敬的 ${employeeName} 先生/女士

您好!

您已经申请了重置登录密码,请在24小时内更换您的密码,如果不做任何操作,系统将保留原密码。
点击此处 立即更换登录密码。

如果上述文字点击无效,请把下面网页地址复制到浏览器地址栏中打开:
${projectUrl}/employee/changeByToken.do_?token=${token}

如有任何疑问或建议,欢迎随时与我们联系。感谢您使用LUCKY业务管理平台。


LUCKY

 


3、spring-mvc.xml配置


    
        
            /jsp/template/freemarker/
        
        
            
                0
                utf-8
                zh_CN
                true
            
        
    

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工厂类 
* @version V1.0 2017/7/15 17:01 by 石冬冬-Seig Heil([email protected])创建 */ @SuppressWarnings("rawtypes") @Component public class FreemarkGeneralFactory implements ApplicationListener { private Map configurationMap = new HashMap(); /** * 监听容器启动,预加载模板参数对象 */ @Override public void onApplicationEvent(ApplicationEvent event) { if (configurationMap.size() == 0 && event instanceof ContextRefreshedEvent) { WebApplicationContext context = (WebApplicationContext) ((ContextRefreshedEvent) event).getApplicationContext(); Map configMap = context.getBeansOfType(FreeMarkerConfigurer.class); Set 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 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
* @version V1.0 2017/7/15 17:06 by 石冬冬([email protected]) - Heil Hitler */ @Controller @RequestMapping("freemark") public class FreemarkGeneralController { private final Logger logger = LoggerFactory.getLogger(PlatformInfoController.class); private final String PAGE_PREFIX = "/template/freemarker/"; @Autowired private FreemarkGeneralFactory freemarkGeneralFactory; /** * 生成 * @return */ @RequestMapping("/general") public String list(Model model){ try { Map params = new HashMap(){{ 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"%>



  
  登陆页
  
  
  



${html}



你可能感兴趣的:(SpringMVC,Freemarker)