崛起於Springboot2.X + freemaker(24)

《SpringBoot2.X心法總綱》 html

   (本篇博客已於2019-08-28優化更新)spring

一、pom

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

二、application.yml

spring:
  freemarker:
    allow-request-override: false
    cache: true
    check-template-location: true
    charset: UTF-8
    content-type: text/html
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    suffix: .html
  profiles:
    active: dev

      若是你使用的是ftl後綴,那麼更改suffix爲ftl,或者不用寫,默認是ftl,若是你用html開發,那麼suffix標註htmlsession

三、編寫controller

@GetMapping(value = "/test2")
public String test2(ModelMap modelMap){

    modelMap.put("name","木九天");
    return "/helloworld";
}

四、建立html

      在resources/template下建立helloworld.htmlapp

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
${name}
</body>
</html>

五、測試

六、疑惑解惑

5.1 :使用freemaker的時候,咱們在controller返回的是一個路徑,同時咱們也使用了ModeMap,咱們在ModeMap添加了數據,而後返回路徑到具體html頁面,${name}  就是咱們ModeMap裏面的數據,但願你們不要迷惑輸出結果:木九天怎麼來的!
   5.2: 返回具體頁面/找對應頁面的時候,千萬不能使用@RestController 和@ResponseBody,由於使用它們以後返回的是一個字符串而不是一個具體頁面了,謹記。
相關文章
相關標籤/搜索