Springboot集成velocity


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

  

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM