springboot系列十四 web模板 freemarker thymeleaf

Freemarker

https://freemarker.apache.org/html

http://freemarker.foofun.cn/java

springboot中freemarker的默認配置

package org.springframework.boot.autoconfigure.freemarker;

import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.autoconfigure.template.AbstractTemplateViewResolverProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(
    prefix = "spring.freemarker"//配置文件默認前綴
)
public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {
    public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";//默認靜態資源位置
    public static final String DEFAULT_PREFIX = "";//默認前綴
    public static final String DEFAULT_SUFFIX = ".ftl";//默認後綴
    private Map<String, String> settings = new HashMap();
    private String[] templateLoaderPath = new String[]{"classpath:/templates/"};
    private boolean preferFileSystemAccess = true;

    public FreeMarkerProperties() {
        super("", ".ftl");
    }

    //省略...
	//其餘見父類
}

示例

依賴

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

簡單配置

spring:
  freemarker:
    cache: false #生成環境可設置爲true
    template-loader-path: classpath:/static
    suffix: .ftl

頁面模板

在resource目錄下新建目錄 static,而後新建個文件 index.ftlgit

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Freemarker測試</title>
</head>
<body>
    <h1>姓名:${name}</h1>
</body>
</html>

web處理器

添加一個springmvc控制器web

@Controller
public class IndexController {
    @RequestMapping({"","index"})
    public String index(Model model){
        model.addAttribute("name", "哈哈哈");
        return "index";
    }
}

查看效果

啓動項目後,訪問 localhost:8080spring

Thymeleaf

參考 https://gitee.com/yimingkeji/springboot/tree/master/thymeleafapache

相關文章
相關標籤/搜索