Springboot集成velocity

♀尐吖头ヾ 提交于 2019-11-27 07:11:44

1.加入maven包

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

2.velocity配置

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

3.测试页面 index.vm

<html>
<body>
亲爱的${toUserName},你好!

    ${message}

祝:开心!
    ${fromUserName}55
    ${time}

</body>
</html>

4.后台数据接口

@Controller
@SpringBootApplication
public class DemoApplication {
    @RequestMapping("/")
    public String velocityTest(Map map){
        map.put("message", "这是测试的内容。。。");
        map.put("toUserName", "张三1");
        map.put("fromUserName", "老许");
        return "index";
    }

5.springboot启动器

@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));
    }
}

  

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!