iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 10
1
Modern Web

30天帶你潮玩Spring Boot Zone系列 第 10

[Day 10] - 實戰 - 使用thymeleaf

閱讀時間: 10分鐘

基本的知識都了解後,終於又來到實戰的時候。
大家要預先準備一些html文件(至少有一個主頁, index.html),但由於時間關係,都為大家準備好了一份html模板。當然,你也可以使用自己準備的。在今天的教學用什麼模板都不會影響的。

第1步: 準備已設計好的html文件

為大家準備好的html模板在這裡: 按一下 下載
https://colorlib.com/wp/template/login-form-v5/

下載完成後,請先解壓。
之後打開Login_v5的文件,會看到以下的檔案。
https://ithelp.ithome.com.tw/upload/images/20190915/20119569YDGJblJFct.jpg

第2步: 將文件檔案放到合適的位置

將所有文件檔案(除了index.html)放到C:\Users\martinyeung\eclipse-workspace\spring-project-2024\src\main\resources\static底下,如下圖所示。
https://ithelp.ithome.com.tw/upload/images/20190915/20119569DvbTKY4tkF.jpg

將index.html放到C:\Users\ martinyeung \eclipse-workspace\spring-project-2026\src\main\resources\templates底下,如下圖所示。
https://ithelp.ithome.com.tw/upload/images/20190915/20119569u1odCHaOoF.jpg

第3步: 確認文件檔案更新

再打開Eclipse 大家就會看到src/main/resources/static 及src/main/resources/templates的底下都多了相對應的文件檔案。
https://ithelp.ithome.com.tw/upload/images/20190915/20119569ioHAqLFBXt.jpg

第4步: 修改controller (SpringBootHelloWorld.java),SpringBootHelloWorld.java是之前的教學裡創建的。

修改成下面的代碼

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class SpringBootHelloWorld {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	@GetMapping("/x")
	public String hello(){
		return "Hey, Spring Boot 的 Hello World !";
	}
	
	@GetMapping("/index")
	public String helloIndex(){
		return "index";
	}

}

要點:
要用@GetMapping 取代 @RequestMapping
要用@Controller 取代 @RestController

第5步: 開啟sprint boot 應用程式(用內置tomcat server)

https://ithelp.ithome.com.tw/upload/images/20190915/20119569wHWgKBjD0r.jpg

點一下項目 – spring-project-2026,按一下右鍵,再選Run As,再選 Run on Server。

https://ithelp.ithome.com.tw/upload/images/20190915/20119569homTWnS599.jpg

點一下 Tomcatv9.0 Server at localhost,再按”Next”。
https://ithelp.ithome.com.tw/upload/images/20190915/20119569KgO1jH9xBq.jpg

如只運行一個項目,就將它加進右邊,再按”Finish”。

https://ithelp.ithome.com.tw/upload/images/20190915/201195694qNEmRO9L5.jpg

完成後,會在”console”中出現上圖的訊息。

最終成品

https://ithelp.ithome.com.tw/upload/images/20190915/20119569Dd4KDCRz5p.jpg
之後會自己出現以上畫面,自動連上主頁 -
http://localhost:8080/spring-project-2026/

http://localhost:8080/spring-project-2026/ = http://localhost:8080/spring-project-2026/indexx/

如文件有任何修改,在重新儲存後,server會自行reload
https://ithelp.ithome.com.tw/upload/images/20190915/20119569igfOjvbFsa.jpg

大家可能會問用Spring Boot App 開啟又有什麼差別?

答:差別在於URL的顯示資料
用Spring Boot App 開啟的應用程式只會顯示http://localhost:8080/
而不會顯示項目的名稱。

如下圖所示:
https://ithelp.ithome.com.tw/upload/images/20190915/20119569PHJbAQWKOC.jpg

如輸入http://localhost:8080/x
就會出現下圖
https://ithelp.ithome.com.tw/upload/images/20190915/201195696c7U4v0v81.jpg

因為x的html頁面是不存在的,所以就會出現這個情況。

如果在一般的瀏覽器看,會出現一個ErrorPage。
https://ithelp.ithome.com.tw/upload/images/20190915/2011956914oKKbiCvE.jpg

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Sep 15 16:11:14 CST 2019
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [Hey, Spring Boot 的 Hello World !], template might not exist or might not be accessible by any of the configured Template Resolvers

所以當使用mapping時要記得為對應的mapping準備一個html檔的頁面。


上一篇
[Day9] – Spring Boot的視圖(View)和模板引擎Thymeleaf介紹
下一篇
[Day 11] - @GetMapping 和 @RequestMapping 的比較及使用
系列文
30天帶你潮玩Spring Boot Zone15
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
jbuduoo
iT邦新手 4 級 ‧ 2020-08-13 15:05:47

謝啦,顯示html的地方我找好久,原來就是這兩行。

要用@GetMapping 取代 @RequestMapping
要用@Controller 取代 @RestController

不客氣,能夠幫到你解決問題,就太好啦

0
JJ
iT邦新手 5 級 ‧ 2021-05-31 11:09:22

請問一下~
我用Run on Server都會出現404錯誤
後來用Spring Boot App就可以了
Server問題是出在哪呢?

0
rain_yu
iT邦新手 1 級 ‧ 2023-11-24 16:51:51

我用windows 將檔案放到各個路徑下後,點選 run on server,但是沒辦法選擇 existing server這個要怎麼解決;
我跟上面的一樣,要使用Spring Boot App才可以正常顯示。

我要留言

立即登入留言