URLの構成
http://hostname:port/contextpath/path?querystring
Spring Boot で Hello World
HeloController.java
package jp.abc;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HeloController {
@RequestMapping("/")
public String index() {
return "Hello Spring Boot World!";
}
}
@RequestMappingアノテーションでいろいろなURLパスに対応する機能を追加できる。
HeloController.java に以下のメソッドを追加する。
http://localhost:8080/hello
@RequestMapping("/hello")
public String hello() {
return "Hello World! - path = /hello";
}
パス変数
URLパスに変数を含めることができる。
@PathVariable アノテーションを使用する。
@RequestMapping("/sum/{num}")
public String sum(@PathVariable int num) {
int res = 0;
for (int i = 1; i <= num; i++) {
res += i;
}
return "total: " + res;
}
JSONを出力する
テキストではDataObjectクラスを同一ソース内に書いているが好ましくないので、別のクラスとして新規作成する。
package jp.abc;
public class DataObject {
private int id;
private String name;
private String value;
public DataObject(int id, String name, String value) {
this.id = id;
this.name = name;
this.value = value;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
HeloControllerに以下のフィールドとメソッドを追加する。
String[] names = { "tsuyano", "hanako", "taro",
"sachiko", "ichiro" };
String[] mails = {
"syoda@tsuyano.com",
"hanako@flower",
"taro@yamada",
"sachiko@happy",
"ichiro@baseball"
};
@RequestMapping("/users/{id}")
public DataObject users(@PathVariable int id) {
return new DataObject(id, names[id], mails[id]);
}
Thymeleafを使ってWebサイトを作る
Thymeleafを使うには、ライブラリを追加する必要がある。
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 http://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.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>jp.abc</groupId> <artifactId>MyBootApp</artifactId> <version>0.0.1-SNAPSHOT</version> <name>MyBootApp</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
[Maven]-[プロジェクトの更新]と[実行]-[maven install]を実施する。
テキストではHeloControllerを修正しているが、ここでは新規クラスを作成する。
IndexController.java
package jp.abc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/index")
public String index() {
return "index";
}
}
HTMLファイルを作成する。
src/main/resources/templates を右クリックして新規作成でHTMLファイルを作成する。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>top page</title>
<style type="text/css">
h1 {
font-size: 18pt;
font-weight: bold;
color: gray;
}
body {
font-size: 13pt;
color: gray;
margin: 5px 25px;
}
</style>
</head>
<body>
<h1>helo page</h1>
<p class="msg">this is Thymeleaf sample page</p>
</body>
</html>
テンプレートに値を表示する
<body>
<h1>helo page</h1>
<p class="msg">this is Thymeleaf sample page</p>
<p class="msg" th:text="${msg}"></p>
</body>
コントローラでは引数にModelを追加する。
Modelを通してテンプレートに値を渡す。
package jp.abc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/index/{num}")
public String index(@PathVariable int num, Model model) {
int res = 0;
for (int i = 1; i <= num; i++) {
res += i;
}
model.addAttribute("msg", "total: " + res);
return "index";
}
}