0
0

SpringBootで静的なcssやJavaScriptのファイルを扱う

Posted at

この記事はこれの続きです

静的なファイルを置いておくフォルダー作成

パッケージではなくフォルダー というのがポイント。

『src/main/resources』直下にフォルダー作成します。
image.png

フォルダー名は『static』。
image.png

静的なファイル作成後はこちら

staticより下の階層については、フォルダー名やファイル名に特別な縛りはありません。
本記事ではこのようにcssとJavaScriptのファイル置きました。
image.png

cssの中身。
image.png

JavaScriptファイルの中身。
image.png

thymeleafファイル

本記事では『css』『js』フォルダーは認証除外フォルダーに指定していません。
そのため、ログイン成功後画面で読み込むようにします。

login-success.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">


<head>
	<link th:href="@{/css/global.css}" rel="stylesheet">
	<script th:src="@{/js/global.js}" charset="utf-8"></script>
</head>


<body>
	
	<h1>login-success</h1>
	
	<div th:text="${userName}"></div>
	
	<form th:action="@{/logout}" method="POST">
		<button type="submit" value="go">ログアウト</button>
	</form>
	
</body>
</html>

動作確認

cssが効いていて…
image.png

JavaScriptも動作しました。
image.png

バージョン

Microsoft Windows [Version 10.0.22631.3672]
JAVA 17.0.10
Spring Boot v3.3.0

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0