Spring Bootにアノテーション@SessionScopeでセッションを使用する
環境
Windows10 64bit
Spring Boot 2.6.3
Spring Tool Suite 4
JDK 11
thymeleaf 3
説明
1.@SessionScope
@SessionScope は、ライフサイクルが現在の Web セッションにバインドされているコンポーネント用の @Scope の特殊化です。
2.@Autowired
@Autowired は、コントローラークラスやサービスクラスのフィールドに対して付与するアノテーションです。
使用例
1.エンティティのクラス(Depart.java)
package com.example.demo;
import java.io.Serializable;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.SessionScope;
//SessionScopeアノテーション
@Component
@SessionScope
//エンティティのクラスDepartの定義
public class Depart implements Serializable {
/**
* エンティティDepartクラスの定義
*/
private static final long serialVersionUID = 1L;
private String dname;
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
}
package com.example.demo;
import java.io.Serializable;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.SessionScope;
//SessionScopeアノテーション
@Component
@SessionScope
//エンティティのクラスDepartの定義
public class Depart implements Serializable {
/**
* エンティティDepartクラスの定義
*/
private static final long serialVersionUID = 1L;
private String dname;
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
}
package com.example.demo; import java.io.Serializable; import org.springframework.stereotype.Component; import org.springframework.web.context.annotation.SessionScope; //SessionScopeアノテーション @Component @SessionScope //エンティティのクラスDepartの定義 public class Depart implements Serializable { /** * エンティティDepartクラスの定義 */ private static final long serialVersionUID = 1L; private String dname; public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } }
2.コントローラのクラス(DepController.java)
書式
model.addAttribute(オブジェクト名);
サンプルコード
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class DepController {
@Autowired
Depart depart;
@GetMapping("/depart")
public String funA(Model model) {
// セッションにあるオブジェクトに値をセットする
depart.setDname("開発部");
model.addAttribute(depart);
return "depart/index";
}
}
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class DepController {
@Autowired
Depart depart;
@GetMapping("/depart")
public String funA(Model model) {
// セッションにあるオブジェクトに値をセットする
depart.setDname("開発部");
model.addAttribute(depart);
return "depart/index";
}
}
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class DepController { @Autowired Depart depart; @GetMapping("/depart") public String funA(Model model) { // セッションにあるオブジェクトに値をセットする depart.setDname("開発部"); model.addAttribute(depart); return "depart/index"; } }
3.コントローラのクラス(SubDepartController)
書式
model.addAttribute(オブジェクト名);
サンプルコード
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class SubDepartController {
@Autowired
Depart depart;
@PostMapping("/depform")
public String funB(@RequestParam String dname, Model model) {
model.addAttribute("depname", dname);
// セッションのオブジェクトをセットする
model.addAttribute(depart);
return "depart/depform";
}
}
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class SubDepartController {
@Autowired
Depart depart;
@PostMapping("/depform")
public String funB(@RequestParam String dname, Model model) {
model.addAttribute("depname", dname);
// セッションのオブジェクトをセットする
model.addAttribute(depart);
return "depart/depform";
}
}
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class SubDepartController { @Autowired Depart depart; @PostMapping("/depform") public String funB(@RequestParam String dname, Model model) { model.addAttribute("depname", dname); // セッションのオブジェクトをセットする model.addAttribute(depart); return "depart/depform"; } }
4.ユーザー登録画面(値を送信する側)
ファイル名 src\main\resources\templates\depart\index.html
サンプルコード
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>ユーザー登録</title>
</head>
<body>
<form method="post" th:action="@{/depform}">
<!-- オブジェクトの値を表示 -->
セッションの値: <p th:text="${depart.dname}"></p>
<input type="text" name="dname" />
<input type="submit" value="送信" />
</form>
</body>
</html>
ファイル名 src\main\resources\templates\depart\index.html
サンプルコード
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>ユーザー登録</title>
</head>
<body>
<form method="post" th:action="@{/depform}">
<!-- オブジェクトの値を表示 -->
セッションの値: <p th:text="${depart.dname}"></p>
<input type="text" name="dname" />
<input type="submit" value="送信" />
</form>
</body>
</html>
ファイル名 src\main\resources\templates\depart\index.html サンプルコード <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> <title>ユーザー登録</title> </head> <body> <form method="post" th:action="@{/depform}"> <!-- オブジェクトの値を表示 --> セッションの値: <p th:text="${depart.dname}"></p> <input type="text" name="dname" /> <input type="submit" value="送信" /> </form> </body> </html>
5.ユーザー登録確認画面(値を受け取る側)
ファイル名 src\main\resources\templates\depart\depform.html
サンプルコード
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>オブジェクトの値を表示する</title>
</head>
<body>
<!-- オブジェクトの値を表示 -->
セッションの値: <p th:text="${depart.dname}"></p>
<p th:text="${depname}"></p>
</body>
</html>
ファイル名 src\main\resources\templates\depart\depform.html
サンプルコード
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>オブジェクトの値を表示する</title>
</head>
<body>
<!-- オブジェクトの値を表示 -->
セッションの値: <p th:text="${depart.dname}"></p>
<p th:text="${depname}"></p>
</body>
</html>
ファイル名 src\main\resources\templates\depart\depform.html サンプルコード <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> <title>オブジェクトの値を表示する</title> </head> <body> <!-- オブジェクトの値を表示 --> セッションの値: <p th:text="${depart.dname}"></p> <p th:text="${depname}"></p> </body> </html>
6.動作確認
Webブラウザに以下のURLを入力します。
http://127.0.0.1:8080/depart/
画面に「セッションの値:開発部」が表示されます。
内容を入力して、送信ボタンを押下します。
http://127.0.0.1:8080/depform
画面に「セッションの値:開発部」が表示されます。