SpringBoot フォーム 画面遷移サンプルコード

環境
SpringBoot 3
JavaSE 17

1.送信側のファイルの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>submit</title>
</head>
<body>
<form method="post" th:action="@{/userform}">
<input type="text" name="username"/>
<input type="submit" value="送信" />
</form>
</body>
</html>
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> <title>submit</title> </head> <body> <form method="post" th:action="@{/userform}"> <input type="text" name="username"/> <input type="submit" value="送信" /> </form> </body> </html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <meta charset="utf-8" />
    <title>submit</title>
  </head>
  <body>
      <form method="post" th:action="@{/userform}">
            <input type="text" name="username"/>
            <input type="submit" value="送信" />	
      </form>
</body>
</html>

説明methodでpost方式を指定し、actionで送信先を指定します。

2.コントローラのクラスの定義
概要
@RequestParamは、リクエストパラメータを受け取ります。

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class MainController {
@GetMapping("/demo")
public String input() {
return "demo/index";
}
@PostMapping("/userform")
public String print(@RequestParam String username,
Model model) {
model.addAttribute("username", username);
model.addAttribute("addr", "東京");
return "demo/userform";
}
}
package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; @Controller public class MainController { @GetMapping("/demo") public String input() { return "demo/index"; } @PostMapping("/userform") public String print(@RequestParam String username, Model model) { model.addAttribute("username", username); model.addAttribute("addr", "東京"); return "demo/userform"; } }
package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class MainController {

      @GetMapping("/demo")
      public String input() {
            return "demo/index";
      }

      @PostMapping("/userform")
      public String print(@RequestParam String username, 
                  Model model) {
            model.addAttribute("username", username);
            model.addAttribute("addr", "東京");
            return "demo/userform";
      }

}

説明
addAttributeメソッドでキーに"username"で値を
変数usernameにしています。

3.受け取る側のファイル(userform.html)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>submit</title>
</head>
<body>
<p th:text="${username}"></p>
<p>[[${addr}]]</p>
</body>
</html>
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> <title>submit</title> </head> <body> <p th:text="${username}"></p> <p>[[${addr}]]</p> </body> </html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <meta charset="utf-8" />
    <title>submit</title>
  </head>
  <body>	
      <p th:text="${username}"></p>
      <p>[[${addr}]]</p>
  </body>
</html>

説明
コントローラから渡された値を文字列で表示します。

IT

Posted by arkgame