apache模板引擎结合spring框架的简单使用(org.apache.velocity)

1.org.apache.velocity模板引擎可以用在发送短信,极光推送,邮件等业务场景中。因为这些业务都有特殊的模板内容。比如app在用户生日时,会推送一条信息给用户,格式如下:尊敬的XXX客户,祝您生日快乐。可变的是用户的姓名,不变的是模板内容。像上诉的业务场景,可以使用该模板引擎。

代码:

<properties>  
        <spring.version>4.1.5.RELEASE</spring.version>
    </properties> 
  
  <dependencies>
        
    <!-- spring start -->  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-aop</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-aspects</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-beans</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <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-core</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-expression</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-orm</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-test</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-web</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-webmvc</artifactId>  
        <version>${spring.version}</version>  
    </dependency>  
    
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.7</version>
    </dependency>  
    <!-- spring end --> 
  </dependencies>

spring配置文件:
<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
           http://www.springframework.org/schema/aop   
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
           http://www.springframework.org/schema/tx  
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"
	default-autowire="byName" default-lazy-init="false">

	<!-- 采用注释的方式配置bean -->
	<context:annotation-config />
	
	<!-- 配置要扫描的包 -->
	<context:component-scan base-package="com.adai"/>
	
	<!-- 读入配置属性文件 -->
		
	<!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 -->  
	<aop:aspectj-autoproxy proxy-target-class="true" />
	
	<!-- velocity模板引擎实例 -->
	<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
		<property name="velocityProperties">
			<props>
				<prop key="resource.loader">class</prop>
				<prop key="class.resource.loader.class">
					org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
				</prop>
				<prop key="velocimacro.library"></prop>
			</props>
		</property>
	</bean>
	
	<!-- 自定义模板实例 -->
	<bean id="templet2" class="com.adai.templemt.Templet">
		<property name="velocityEngine2" ref="velocityEngine" />
	</bean>
</beans>
自定义模板:testTemplemtAuto.vm
内容:
auto hello
            ${name}
       world
自定义模板:testTemplemtSet.vm
模板内容:
set hello
            ${name}
       world
模板工具类:
package com.adai.templemt;
import java.util.Map;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.velocity.VelocityEngineUtils;
/**
 * 模板引擎工具类,该类用来读取指定模板的内容
 * @author v-chenk25
 *
 */
@Component("templet")
public class Templet {
	/**自动注入方式**/
	@Autowired
	private VelocityEngine velocityEngine ;
	/**set方法注入**/
	private static VelocityEngine velocityEngine2 ;
	/**
	 * 获取指定模板的内容
	 * @param pathTemplate 指定模板的名字
	 * @param param 模板内可变的参数
	 * @return 模板的详细内容
	 */
	public  String useTemple(String pathTemplate , Map<String,Object> param) {
		return VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, pathTemplate, "utf-8",param);
	}
	/**
	 * 获取指定模板的内容
	 * @param velocityEngine2 引擎实例
	 * @param pathTemplate 指定模板的名字
	 * @param param 模板的详细内容
	 * @return
	 */
	public String useTemple(VelocityEngine velocityEngine2, String pathTemplate, Map<String, Object> param) {
		return VelocityEngineUtils.mergeTemplateIntoString(velocityEngine2, pathTemplate, "UTF-8",param);
	}
	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}
	public static VelocityEngine getVelocityEngine2() {
		return velocityEngine2;
	}
	public static void setVelocityEngine2(VelocityEngine velocityEngine2) {
		Templet.velocityEngine2 = velocityEngine2;
	}
}
测试类:
package com.adai.templemt;

import java.util.HashMap;
import java.util.Map;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/***
 * 测试类
 * @author v-chenk25
 *
 */
public class TestTemplemt {
	static String pathTemplate = "testTemplemt" ;
	public static void main(String[] args) {
		ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(new String[] {"classpath:spring-context.xml"});

		Map<String,Object> param = new HashMap<String,Object>();
		param.put("name", "adai") ;
		//采用自动注入方式
		Templet templet = (Templet) app.getBean("templet") ;
		System.out.println( templet.useTemple(String.format("%sAuto.vm", pathTemplate), param) );
		System.out.println();
		//采用set方法注入方式
		Templet templet2 = (Templet) app.getBean("templet2") ;
		System.out.println( templet2.useTemple( Templet.getVelocityEngine2() , String.format("%sSet.vm", pathTemplate), param) );
	}
}
输出结果:

总结:使用模板引擎,可以自定义一些固定的内容。疑问扩展:如果推送富文本编辑器的内容,模板引擎的模板该如何写?




猜你喜欢

转载自blog.csdn.net/qq_17089617/article/details/79112758