Spring Boot 1.3.5 + Thymeleaf + PostgreSQL 9.6 导入备忘录 (1)
首先
写作的过程中,我没有想到会变得如此冗长。
-
- Spring Boot導入~フォームの利用 (本記事)
- 入力のバリデーション、JPAを利用したデータの永続化 http://qiita.com/sashim1343/items/d37d2336f3625b86c141
被分成了两个部分。
环境
-
- JDK 8u91
-
- Spring Boot 1.3.5
-
- PostgreSQL 9.6
-
- NetBeans 8.1
- Maven 3.0.5 (NetBeansバンドル)
使用Spring Boot来实现~你好世界
在NetBeans上创建新项目→选择”Maven”类别,并创建一个适当的项目。
本次设置如下所示。
-
- プロジェクト名: SpringBootSample
-
- グループID: com.example
-
- パッケージ: com.example.springbootsample
- その他の項目は既定値のまま
当创建了项目后,需要修改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>
<groupId>com.example</groupId>
<artifactId>SpringBootSample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version> <!-- Javaのバージョンを明記 -->
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
接下来,创建com.example.springbootsample.controller包,并在其中创建SampleController.java。
package com.example.springbootsample.controller;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/")
public class SampleController {
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
String get() {
return "Hello, Spring Boot!";
}
}
被标注@Controller的类作为控制器运行,并根据设定的@RequestMapping条件处理事件。此外,如果标注@ResponseBody,可以直接返回内容作为返回值。换句话说,上述控制器对于GET请求/会返回字符串”Hello, Spring Boot!”。
另外,创建名为com.example.springbootsample.Application的类作为入口点。
package com.example.springbootsample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
完成后,请尝试运行一次。
Spring Boot的监听端口与Tomcat的默认设置相同,为8080(可根据配置进行更改)。
使用Web浏览器等访问http://localhost:8080/,检查是否显示“Hello, Spring Boot!”。
引入Thymeleaf(模板引擎)。
如果继续这样,就必须在控制器中创建所有要显示的内容,这是低效且麻烦的。
因此,在视图部分引入了一个名为Thymeleaf的模板引擎。
在pom.xml的依赖项中添加以下内容。
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
此外,创建src/main/resources/application.yml文件,并填写以下内容。
(由于src/main/resouces目录在默认情况下不存在,因此需要手动创建)
spring:
thymeleaf:
cache: false
通过这样做,Thymeleaf的缓存将被禁用。
然后,准备作为模板的src/main/resources/templates/sample.html。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello, Spring Boot!</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<p th:text="${message}"></p>
<p>using Thymeleaf</p>
</body>
</html>
关键是第8行的th:text=”${message}”。
通过这样做,可以将值从控制器传递到视图。
需要注意的是,在NetBeans中使用Thymeleaf时,如果不指定xmlns=”http://www.w3.org/1999/xhtml”,将会遇到很多错误。
最后,我们需要在SampleController中对之前创建的视图进行映射设置。
package com.example.springbootsample.controller;
import org.springframework.stereotype.*;
import org.springframework.ui.Model; // 追加
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/")
public class SampleController {
@RequestMapping(method = RequestMethod.GET)
// @ResponseBody // このアノテーションをつけるとレンダリングが行われなくなるので注意
String get(Model model) {
model.addAttribute("message", "Hello, Spring Boot!"); // パラメタを渡す
return "sample"; // 使用するテンプレートの名前を指定する
}
}
写完这部分后再次执行。访问http://localhost:8080/,如果页面正常显示,则表示OK。
使用表单进行数值的传送和接收
因为之前只显示了预定的文本,所以我想开始自己输入。所以我要使用表单来发送和接收值。
首先,在之前创建的视图中添加一个表单。
<!-- 前略 -->
<form method="POST" th:action="@{/}">
<label for="name">Your name: </label><input type="text" name="name" />
<label for="age">Your age: </label><input type="text" name="age" />
<input type="submit" value="submit" />
</form>
<!-- 後略 -->
现在可以通过POST发送名字和年龄了。
只需要在控制器中接收这些数据,为此需要创建一个与此表单对应的类作为准备工作。
在com.example.springbootsample.controller包中定义一个PersonForm类。
package com.example.springbootsample.controller;
public class PersonForm {
private String name;
private Integer age;
// getter/setter略
}
修改SampleController以获取POST提交的表单值。
通过PersonForm实例,控制器可以获取表单的值。
生成这个实例的部分可以通过一个注解来完成。
// 前略
public class SampleController {
@RequestMapping(method = RequestMethod.GET)
String get(Model model) {
model.addAttribute("message", "Hello, Spring Boot!");
return "sample";
}
@RequestMapping(method = RequestMethod.POST)
String post(@ModelAttribute PersonForm personForm, Model model) {
String name = personForm.getName();
Integer age = personForm.getAge();
model.addAttribute("message", "your name: " + name + ", your age: " + age);
return "sample";
}
}
下面的句子用中文翻译:
继续。
仅需一种选择,请将以下内容用中文进行本地化改写:
参考
http://projects.spring.io/spring-boot/ 项目的官方网站
http://www.riem.nagoya-u.ac.jp/~ohta/etc/springboot-1.html 某大学教授的关于spring boot的网页收录
http://d.hatena.ne.jp/hagi44/20130608/1370667799 一位博主对spring boot的博客文章
http://blog.okazuki.jp/entry/2015/07/05/093915 另一位博主对spring boot的博客文章