end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

springboot for java で 複数Controller間共有sessionには、@SessionScope

以下の通り。

以前は、@Scope と指定していましたが、最近では @SessionScope のようです。

参考url

dir構成

PATH NOTE
pom.xml 素朴な内容で、@SessionScope 用のdependencyはありません
MySessionBean.java @Component と @SessionScope がポイント
Index?Action.java MySessionBeanによるセッション変数test用です
templates/index?.html

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.5</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>jp.end0tknr</groupId>
  <artifactId>MySpring3</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>MySpring3</name>
  <description>seasar2 to spring</description>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
            </exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

MySessionBean.java

package jp.end0tknr;

import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.SessionScope;
import lombok.Data;

@Data
@Component
@SessionScope
public class MySessionBean {
    private String userId     = "USER's NAME";
    private String firstName  = "USER's First Name";
    private String lastName   = "USER's Last  Name";
    private String tmpMessage = "TEMP MESSAGE";
}

Index?Action.java

Index1Action.java

package jp.end0tknr;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Index1Action {

    @Autowired
    MySessionBean mySessionBean;

    @RequestMapping(value = "/index1")
    public String index(Model model) {

        mySessionBean.setTmpMessage("VISITED Index1");

        model.addAttribute("mySessionBean", mySessionBean);
        return "index1";
    }
}

Index2Action.java

package jp.end0tknr;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Index2Action {
    @Autowired
    MySessionBean mySessionBean;

    @RequestMapping(value = "/index2")
    public String index(Model model) {
        model.addAttribute("mySessionBean", mySessionBean);

        return "index2";
    }
}

templates/index?.html

templates/index1.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>SPRING SESSION TEST-1</title>
    <meta charset="utf-8" />
  </head>
  <body>
    <h1>SPRING SESSION TEST-1</h1>
  <p>
    <a href="/index1">INDEX1</a><br/>
    <a href="/index2">INDEX2</a><br/>
  </p>

  <p><div th:text="${mySessionBean.userId}"></div></p>
  <p><div th:text="${mySessionBean.tmpMessage}"></div></p>
  </body>
</html>

templates/index2.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>SPRING SESSION TEST-2</title>
    <meta charset="utf-8" />
  </head>
  <body>
    <h1>SPRING SESSION TEST-2</h1>
  <p>
    <a href="/index1">INDEX1</a><br/>
    <a href="/index2">INDEX2</a><br/>
  </p>

  <p><div th:text="${mySessionBean.userId}"></div></p>
  <p><div th:text="${mySessionBean.tmpMessage}"></div></p>
  </body>
</html>