基于spring boot + thymeleaf 创建web应

2016-08-17  本文已影响0人  ArgusK

环境准备

相关技术

创建应用

1.启动idea,new > project > spring initializr, next

spring initialzr

2.选中gradle project

gradle project

3.选中thymeleaf

support thymeleaf feature

4.点击next直到完成

应用说明

gradle configuration

注意:使用向导创建的工程默认是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的某个属性

编译、运行应用

  1. gradle build
  2. java -jar ...
application running

访问应用

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

index page

注意:端口可以在application.properties中配置

server.port=8081

上一篇 下一篇

猜你喜欢

热点阅读