IntelliJ 创建Spring Boot项目

本文详细介绍了如何使用Spring Boot快速搭建一个完整的Web应用项目,并演示了如何通过Thymeleaf模板引擎显示数据。
1). 新建工程 -> Creat New Project
3110861-cd5826f678f1a0fc.png
图1.png
2). 选择模板
  • Project SDK:点击New...选择jdk
  • Choose Initializr Service URL 选择Custom, 链接选用http://start.spring.io/,据说不带s的快
    3110861-0c017dff5cc59881.png
    图2.png
3). 配置
3110861-08102d8cfe30cde4.png
图3.png
4). 选择Web -> web, (非必须选择)Template Engines -> Thymeleaf(用来替换jsp模板引擎)
3110861-0a0cdc28600e2c7a.png
图4.png

3110861-baac57e600680192.png
图5.png
5). 选择工程名和路径
3110861-c10c36cbd403000a.png
图6.png
6). 运行(点击绿色的三角按钮)
3110861-0bb5637bc9649299.png
图7.png
3110861-46762d4ba351757a.png
图8.png
7). 浏览器打开http://localhost:8080

3110861-499158819e8ae49f.png
图9.png

原因
项目中没有静态页面及控制器.

8). 创建控制器
  • HelloController.kt
@Controller
@EnableAutoConfiguration
class HelloController {

    @RequestMapping("/")
    @ResponseBody
    fun index(): String {
        return "Hello World!"
    }
}

访问http://localhost:8080/

3110861-93ec9fdc4d06547c.png
图10.png

9). 返回页面
  • index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    首页内容
</body>
</html>
  • HelloController.kt
@Controller
@EnableAutoConfiguration
class HelloController {

    @RequestMapping("/index.html")
    fun index() : String {
        return "index"
    }
}

访问http://localhost:8080/index.html

3110861-361b7e45c0947911.png
图10.png

10). 刷新配置
  • 修改pom.xml文件
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional>
   <scope>true</scope>
</dependency>
<build>
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <configuration>
            <fork>true</fork>
         </configuration>
      </plugin>
   </plugins>
</build>
  • 修改idea
    I. Ctrl+Alt+S. Build,Execution,Deployment -> Compiler, 勾选Build project automatically.


    3110861-f4daa631e937d33c.png
    图11.png

    II. Ctrl+Shift+Alt+ /


    3110861-83a5a9ac46adb646.png
    图12.png

    3110861-ec31bb6439e6ea8b.png
    图13.png
  • 重新部署项目即可实现修改html刷新重载,修改kotlin代码重新部署
11). 使用模板引擎
  • 数据类Student
/**
 * 数据类
 */
data class Student (
        val name: String,
        val age: Int
)
  • 控制器Controller
@Controller
class HelloController {
    @RequestMapping("/students.html")
    fun students(map: MutableMap<String, Any>): String {
        val list = ArrayList<Student>()
        for (i in 0..9) {
            list.add(Student("张三$i", 23+i))
        }
        // 返回给页面的数据
        map["sList"] = list
        return "students"
    }
}
  • students.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>学生</title>
</head>
<body>
    所有学生
    <ul th:each="stu,stuSta:${sList}">
        <li>
            序号:<span th:text="${stuSta.index}"></span><br>
            姓名:<th:block th:text="${stu.name}"/><br>
            年龄:<div th:text="${stu.age}"></div><br>
        </li>
    </ul>
</body>
</html>

写完之后html代码报红线,使用Alt+Enter修复即可,也可不修复。(此为编辑器的问题)


3110861-e0f6463ae6baac04.png
图14.png
  • 效果


    3110861-3cb8d756f86c80c8.png
    图15.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值