美文网首页
基于spring boot + thymeleaf 创建web应

基于spring boot + thymeleaf 创建web应

作者: ArgusK | 来源:发表于2016-08-17 19:06 被阅读0次

环境准备

  • jdk1.8
  • gradle-2.8
  • idea15

相关技术

  • spring boot
  • spring mvc
  • thymeleaf h5页面模板

创建应用

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

spring initialzr

2.选中gradle project

gradle project

3.选中thymeleaf

support thymeleaf feature

4.点击next直到完成

应用说明

  • gradle工程配置文件
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
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

相关文章

网友评论

      本文标题:基于spring boot + thymeleaf 创建web应

      本文链接:https://www.haomeiwen.com/subject/hvpssttx.html