Thymeleaf 可以取代JSP
https://www.thymeleaf.org/documentation.html
Can Thymeleaf be used as a complete substitute for JSP and JSTL?
究竟Thymeleaf能否完全取代JSP and JSTL?
Absolutely. Not only it can, but we strongly encourage you to do so.
絕對可以,而且我們強烈鼓勵你使用。
maven的pom.xml
添加依賴
|
|
- static:用於存放靜態資源,例如html、JavaScript、css以及圖片等。
- templates:用來存放模板引擎Thymeleaf(本質依然是.html檔案)
static網站檔案放入src\main\resources\static
底下
index.html
放入src\main\resources\templates
底下
修改code
|
|
- Model是一個特殊的類,相當於維護一個Map一樣,而Model中的資料通過controller層的關聯繫結在view層(即Thymeleaf中)可以直接使用。
- return “hello”:這個index就是在templates目錄下對應模板(本次為Thymeleaf模板)的名稱,即應該對應hello.html這個Thymeleaf檔案(與頁面關聯預設規則為:templates目錄下返回字串.html)。
index.html
加入code
|
|
- 畫面顯示
kkkkkkkkkk
greg54564hsfiuwe
- 分別1:
@GetMapping是在Spring4.3時才有的,在意思上@GetMapping 就是@RequestMapping (method = RequestMethod.GET)
- 分別2:
@GetMapping 可在method層面上用作處理 http 的請求。
@RequestMapping 可在class層面上配合@GetMapping 使用。
- 分別3:
@GetMapping 與 @RequestMapping 不能互相交換。
@GetMapping的位置只能在class之內,而@RequestMapping在內外也可以
Springboot官方提供的配置(application.properties)內容有以下:
# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.cache=true # Whether to enable template caching.
spring.thymeleaf.check-template=true # Whether to check that the template exists before rendering it.
spring.thymeleaf.check-template-location=true # Whether to check that the templates location exists.
spring.thymeleaf.enabled=true # Whether to enable Thymeleaf view resolution for Web frameworks.
spring.thymeleaf.enable-spring-el-compiler=false # Enable the SpringEL compiler in SpringEL expressions.
spring.thymeleaf.encoding=UTF-8 # Template files encoding.
spring.thymeleaf.excluded-view-names= # Comma-separated list of view names (patterns allowed) that should be excluded from resolution.
spring.thymeleaf.mode=HTML # Template mode to be applied to templates. See also Thymeleaf's TemplateMode enum.
spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.reactive.chunked-mode-view-names= # Comma-separated list of view names (patterns allowed) that should be the only ones executed in CHUNKED mode when a max chunk size is set.
spring.thymeleaf.reactive.full-mode-view-names= # Comma-separated list of view names (patterns allowed) that should be executed in FULL mode even if a max chunk size is set.
spring.thymeleaf.reactive.max-chunk-size=0 # Maximum size of data buffers used for writing to the response, in bytes.
spring.thymeleaf.reactive.media-types= # Media types supported by the view technology.
spring.thymeleaf.servlet.content-type=text/html # Content-Type value written to HTTP responses.
spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain.
spring.thymeleaf.view-names= # Comma-separated list of view names (patterns allowed) that can be resolved.