提前声明这是一个最小环境,我最烦学个小模板还把 hibernate, spring security 加上去,唯恐天下不乱,
我是不喜欢velocity的,但公司要用,spring boot高版本有放弃对velocity的支持,这里是低版本的
我用的是idea项目结构为
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.aliyun</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-velocity</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties
spring.velocity.cache= false
spring.velocity.charset=UTF-8
spring.velocity.check-template-location=true
spring.velocity.content-type=text/html
spring.velocity.enabled=true
spring.velocity.resource-loader-path=/templates
spring.velocity.prefix=/templates/
spring.velocity.suffix=.vm
index.vm为
<html> <body> 亲爱的${toUserName},你好! ${message} 祝:开心! ${fromUserName}55 ${time} </body> </html>
DemoApplication.java
package com.aliyun.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.util.Map; @Controller @SpringBootApplication public class DemoApplication { @RequestMapping("/") public String velocityTest(Map map){ map.put("message", "这是测试的内容。。。"); map.put("toUserName", "张三1"); map.put("fromUserName", "老许"); return "index"; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
DemoApplicationTests.java
package com.aliyun.demo; import org.apache.velocity.app.VelocityEngine; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.ui.velocity.VelocityEngineUtils; import java.util.HashMap; import java.util.Map; @RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests { @Test public void contextLoads() { } @Autowired VelocityEngine velocityEngine; @Test public void velocityTest(){ Map<String, Object> model = new HashMap<String, Object>(); model.put("message", "这是测试的内容。。。"); model.put("toUserName", "张三"); model.put("fromUserName", "老许"); System.out.println(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "/templates/index.vm", "UTF-8", model)); } }
没有废话