1. 程式人生 > 其它 >boot spring 解析csv_spring-boot-starter-thymeleaf 避坑指南

boot spring 解析csv_spring-boot-starter-thymeleaf 避坑指南

技術標籤:boot spring 解析csv

spring-boot-starter-thymeleaf 避坑指南

d85df984c8654d564333947b9c7b7fd5.png

spring-boot-starter-thymeleaf 避坑指南

第一步:pom配置環境 先不要管包是做什麼的 總之必須要有 否則進坑

 net.sourceforge.nekohtml nekohtml 1.9.22org.springframework.boot spring-boot-starter-thymeleaf

第二步:配置application.properties

注意 1.結尾一定要有------ #thymeleaf end --------- 否則掉坑

2.#模板編碼 spring.thymeleaf.mode=LEGACYHTML5

要想使用LEGACYHTML5這個編碼格式必須引入 上面pom中‘避坑包’ 否則用不了

肯定有人要問為什麼不用HTML5 ,你可以試試

因為你可能會發現在預設配置下,thymeleaf對.html的內容要求很嚴格,比如,

如果少最後的標籤封閉符號/,就會報錯而轉到錯誤頁。也比如你在使用Vue.js這樣的庫,然後有

也會被thymeleaf認為不符合要求而丟擲錯誤。因此,建議增加下面這段:

spring.thymeleaf.mode = LEGACYHTML5

spring.thymeleaf.mode的預設值是HTML5,其實是一個很嚴格的檢查,改為LEGACYHTML5可以得到一個可能更友好親切的格式要求。

需要注意的是,LEGACYHTML5需要搭配一個額外的庫NekoHTML才可用 也就時上文的避坑包

#spring.thymeleaf.cache=false## 檢查模板是否存在,然後再呈現spring.thymeleaf.check-template-location=true#Content-Type值spring.thymeleaf.content-type=text/html#啟用MVC Thymeleaf檢視解析度spring.thymeleaf.enabled=true## 應該從解決方案中排除的檢視名稱的逗號分隔列表##spring.thymeleaf.excluded-view-names=#模板編碼 spring.thymeleaf.mode=LEGACYHTML5# 在構建URL時預先檢視名稱的字首spring.thymeleaf.prefix=classpath:/templates/# 構建URL時附加檢視名稱的字尾.spring.thymeleaf.suffix=.html# 鏈中模板解析器的順序#spring.thymeleaf.template-resolver-order= o# 可以解析的檢視名稱的逗號分隔列表#spring.thymeleaf.view-names=#thymeleaf end

這是我的靜態網頁結構

748477286858ed8a3dba1686a3334bfa.png

第三步:controller層

注入的時候一定要是Controller 不要是RestController 因為它是rest介面(json格式) 是解析不到html

@Controller 注意不要是RestController

@RequestMapping(value = "/")public class MainController {  @Autowired MainService mainService;  @GetMapping(value = "/home") public String homePage(){  return "test"; }}

實在沒有那麼多需求 就用原生態 隨便玩想跳哪裡跳哪裡

 @GetMapping(value = "/home") public void homePage(HttpServletResponse response)throws IOException{ response.sendRedirect("index.html");// return "index"; }

需要傳值的話,java介面使用正常的方法就可

 model.addAttribute("yu