【Spring Framework】Spring Framework ~ Formデータの入出力 / Thymeleaf 編 ~

■ はじめに

https://blogs.yahoo.co.jp/dk521123/37753597.html
の続き。

今回は、以下の習得を目的にサンプルを作成してみる

今回の目的

1) Spring Framework での Formデータの入出力
2) テンプレートエンジンである「Thymeleaf (タイムリーフ)」

■ サンプル

スターター

HelloWorldApplication.java
package com.sample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class HelloWorldApplication {
  public static void main(String[] args) {
    SpringApplication.run(HelloWorldApplication.class, args);
  }
}

コントローラ側

HelloWorldController.java
package com.sample.controllers;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sample.forms.HelloForm;

@Controller
@EnableAutoConfiguration
public class HelloWorldController {
  @RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.POST})
  public String home(Model model) {
    model.addAttribute("resultForm", new HelloForm());
    model.addAttribute("world", "World!!");
    return "home";
  }

  @RequestMapping(value = "/result", method = RequestMethod.POST)
  public String result(@ModelAttribute HelloForm helloForm, Model model) {
    model.addAttribute("resultForm", helloForm);
    return "hello-world";
  }
}

モデル側

HelloForm.java
package com.sample.forms;

import java.io.Serializable;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class HelloForm implements Serializable {
  private static final long serialVersionUID = 1L;
  @NotNull
  @Size(min = 5, max = 10)
  private String greeting;

  private String name;

  public void setGreeting(String greeting) {
    this.greeting = greeting;
  }

  public String getGreeting() {
    return this.greeting;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return this.name;
  }
}

ビュー側

home.html (src/main/resources/templates/home.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h1>Hello, <span th:text="${world}"></span></h1>
<form th:action="@{/result}" method="post">
  <div><label> Greeting : <input type="text" name="greeting"/> </label></div>
  <div><label> Name: <input type="text" name="name"/> </label></div>
  <input type="submit" value="Login"/>
</form>
</body>
</html>
hello-world.html (src/main/resources/templates/hello-world.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Result</title>
</head>
<body>
<h1>Result</h1>
<p th:text="${resultForm.greeting} + ', ' + ${resultForm.name} + '!'" />
<a href="/">Next</a>
</body>
</html>

■ 実行する

[1] プロジェクトを右クリックし、[Run As]-[3 Spring Boot App]を選択
[2] ブラウザで以下のURLにアクセスする
 => 例えば、Greetingに「Hello」、Name「Mike」を入力し、「Login」ボタン押下。
 => 「Hello, Mike!」が表示される
[[[http://localhost:8080/]]]