SpringBoot使用freemarker模板


導入依賴

 <!-- 添加freemarker模版的依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

application.properties文件中新增freemarker配置

## Freemarker 配置
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.request-context-attribute=request
spring.freemarker.prefix=/
spring.freemarker.suffix=.ftl

在src/main/resource/templates文件夾中創建helloFtl.ftl文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index01</title>
</head>
<body>
    歡迎:
    <#list listThree as three >
        ${three}
    </#list>
</body>
</html>

創建controller層

@Controller
@RequestMapping("/free")
public class FreeMarkerController {
@RequestMapping("/getThree") public String getThree(ModelMap map){ List<String> list=new ArrayList<>(); list.add("aa"); list.add("bb"); map.put("listThree",list); return "index"; } }

瀏覽器測試

    


免責聲明!

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



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