springboot整合freemarker

  • 添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
  • 编写配置文件(application.yml)
spring:
  freemarker:
    cache: false  

为了实现热部署,这里仅配置一下freemarker的缓存,关于freemarker的其它配置使用默认即可。

  • 创建freemarker模板
    在src/java/resources目录下创建templates文件夹并创建demo.ftl。
    模板默认是从【classpath:/templates/】这个位置查找的。
    demo.ftl文件内容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
        请看说明:${descrip} <br />
        haahaaaa
</body>
</html>
  • 创建web层辅助类
@Controller
public class FreemarkerController {

    @RequestMapping("/demo")
    public String demo(Map<String, Object> map) {
        map.put("descrip", "It's a springboot integrate freemarker's demo!!!!");
        return "demo";
    }

}
  • 创建测试启动类
@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}

源代码链接:https://github.com/myNameIssls/springboot-study
参考链接:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples
关于springboot配置文件中的属性及freemarker的配置可参考链接:
http://docs.spring.io/spring-boot/docs/1.5.3.RELEASE/reference/htmlsingle/#appendix

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值