0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Java Spring Frameworkでドロワーメニューを作ってみよう

0
Last updated at Posted at 2026-02-23

1.サーバーに /test データをリクエストする (fetch)

2.サーバーから返されたHTMLフラグメントを
(return "samplefragment/wrapper_sample :: fragmentSample")
テキストとして受け取る (res.text())

3.そのHTMLを特定のボックスに流し込む (innerHTML)

4.最後に、隠れていたドロワーを開く (openDrawer)

Controller(java)

public class SampleController {

	@GetMapping("/test/main")
	public String sampleMain() {
		return "/sample/main_sample";
        //file path: template/sample/main_sample.html
	}
	
	@GetMapping("/test")
	public String sampleTest() {
		return "sample/wrapper_sample :: fragmentSample";
        //file path: template/sample/wrapper_sample.html
	}
}

Parent View(親html) main_sample.html

<script type="text/javascript">
function openTest() {

    fetch('/test')//ここで@GetMapping("/test")を呼び出す
        .then(res => res.text())
        .then(html => {
            //htmlに'drawerContent'を入れる
            document.getElementById('drawerContent').innerHTML = html;
            openDrawer('drawer'); //open drawer
        });
}
</script>

<input type="button" value="sample" onclick="openTest()"/>

<div id="drawerContent">
							
					<!-- drawerContent wrapper fragment -->
</div>

Drawer View(子html) wrapper_sample.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

</head>
<body>
<!-- fragment -->
<div th:fragment="fragmentSample">
    	<p>親htmlのボタンをクリックしたときdrawerが表示される</p>
</div>
</body>
</html>

これを活用すれば、サイドメニューを作ることも可能

KakaoTalk_20260220_175354237.png

学んだこと: drawer htmlでJavaScriptは使えないようだ。親htmlにscriptを作成するとdrawer htmlに反映される。
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?