【Thymeleaf】groovyを使ってHTMLを出力する

1.以下のgroovyファイル、テンプレートを作成する。

2.「template」フォルダを作成し、テンプレートファイルを格納する。

3.コマンドプロンプト(ターミナル)で以下コマンドを実行する。

spring run test.groovy

4.localhost:8080を確認する※

ポート8080でエラーが出る場合は他のポートを指定して実行すると良いです。

参考:【groovy】実行するポートを指定(変更)する方法

groovyファイル

ファイル名:test.groovy

@Grab("thymeleaf-spring5")

@Controller
class App{

	@RequestMapping("/")
	@ResponseBody
	def home(ModelAndView mav){
		mav.setViewName("home")
		mav
	}
}

Thymeleaf(テンプレート)

ファイル名:home.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index Page</title>
<style>
body {font-size:16pt; color:#999;}
h1 {font-size:100px; text-align:right; color:"f6f6f6;"
letter-spacing:-0.1em;margin:-50px 0px -50px 0px;}
</style>
</head>
<body>
<h1>Hello!</h1>
<p>this is sample web page.</p>
</body>
</html>

実行イメージ

コメント