环境准备
- jdk1.8
- gradle-2.8
- idea15
相关技术
- spring boot
- spring mvc
- thymeleaf h5页面模板
创建应用
1.启动idea,new > project > spring initializr, next

2.选中gradle project

3.选中thymeleaf

4.点击next直到完成
应用说明
- gradle工程配置文件

注意:使用向导创建的工程默认是apply plugin: 'eclipse' 将其修改为 'idea' 并重新import, 这样侧边栏会出现gradle工具栏,方便build和run application
由于gradle是采用groovy语言编写的,如果不熟悉可以先了解一下groovy语言。
- 程序入口代码
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- 编写Controller

- 编写前端模板
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Title</title>
</head>
<body>
<form action="#" th:action="@{/add}" th:object="${user}" method="post">
<label>Name</label>
<input type="text" th:field="*{name}" />
<label>Age</label>
<input type="text" th:field="*{age}" />
<br />
<input type="submit" value="add" />
</form>
</body>
</html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
表示这是一个thymeleaf模板。
@{/add}
是URL表达式,对应controller接收到的RequestMapping
${user}
是bean对象
*{name}
是bean对象user的某个属性
编译、运行应用
- gradle build
- java -jar ...

访问应用
浏览器输入网址 http://localhost:8081, 出现以下页面

注意:端口可以在application.properties
中配置
server.port=8081
网友评论