1. 의존성 추가

- Maven

1
2
3
4
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
cs

- Gradle

1
compile('org.springframework.boot:spring-boot-starter-freemarker')
cs

 

 

2. Properties 설정

1
2
3
4
5
6
7
8
9
10
#freemarker configs
spring.freemarker.template-loader-path=classpath:/templates
spring.freemarker.prefix=/freemarker/
spring.freemarker.suffix=.ftl
spring.freemarker.contentType=text/html
spring.freemarker.charset=UTF-8
spring.freemarker.cache=false
 
#devtools configs
spring.devtools.livereload.enabled=true
cs

※ livereload를 enable시 freemarker cache 옵션을 false로 변경하여 ftl이 변경시 자동으로 새로고침되도록 설정하며 운영시에는 반드시 cache옵션을 true로 변경

 


3. Controller 작성

1
2
3
4
5
@GetMapping("/hello")
public String hello(Map model){
    model.put("msg""hello freemarker");
    return "hello";
}
cs

 


4. Test Template 작성

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="utf-8">
    <title>hello</title>
</head>
<body>
    <h1>Freemarker Test</h1>
    <h2>${msg}</h2>
</body>
</html>
cs

 

 

Posted by 셋부터넷
,