IntelliJ 创建Spring Boot项目

简介: 1). 新建工程 -> Creat New Project图1.png2). 选择模板Project SDK:点击New...选择jdkChoose Initializr Service URL 选择Custom, 链接选用http://start.
1). 新建工程 -> Creat New Project
img_4338ae6bd8a49f34d2d82e6ee2ac5953.png
图1.png
2). 选择模板
  • Project SDK:点击New...选择jdk
  • Choose Initializr Service URL 选择Custom, 链接选用http://start.spring.io/,据说不带s的快
    img_e2722acd496318d784b9f7b6105cb5aa.png
    图2.png
3). 配置
img_c7443accb0a291784b3977601ca3a761.png
图3.png
4). 选择Web -> web, (非必须选择)Template Engines -> Thymeleaf(用来替换jsp模板引擎)
img_8e17cb11ee8bfdb2241ca5844011b284.png
图4.png

img_1d197b2b462b94780a7fac3881e45002.png
图5.png
5). 选择工程名和路径
img_f8157574f649073442679c74afa42b3f.png
图6.png
6). 运行(点击绿色的三角按钮)
img_323d02f103bdd03891ee36b6e4c154a8.png
图7.png
img_ed4994373c46baee00538f53c0bdb5d2.png
图8.png
7). 浏览器打开http://localhost:8080

img_9766abe254e5fd42593edc51a84741d2.png
图9.png

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

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

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

访问http://localhost:8080/

img_9607f0f2a4e1465a3a0411bc05e943fe.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

img_fd2b21ecad3429576d57800963ef7024.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.


    img_de53e3fd33147364a196bc605d5b3ed2.png
    图11.png

    II. Ctrl+Shift+Alt+ /


    img_6fec5fd132de606ae6b99424c82363bd.png
    图12.png

    img_50a32a964e1a2d66cd1aab14722f8e1b.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修复即可,也可不修复。(此为编辑器的问题)


img_ca42de02fd53266a9a19001ff41e6224.png
图14.png
  • 效果


    img_ba93857248e78348234c63c0caf694a7.png
    图15.png
目录
相关文章
|
21天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
38 0
|
1月前
|
Java Spring
【编程笔记】在 Spring 项目中使用 RestTemplate 发送网络请求
【编程笔记】在 Spring 项目中使用 RestTemplate 发送网络请求
94 0
|
2月前
|
缓存 NoSQL Java
Spring Boot项目中热点场景详解(万字总结)
Spring Boot项目中热点场景详解(万字总结)
|
3月前
|
监控 安全 Java
Java(spring cloud)智慧工地(项目层+工地层+APP)源码
智慧工地提供工地智能管理服务,打通数据壁垒,互通管理中心各平台。实现:“可视”、“可控”、“可管”。智慧工地管理云平台是一种利用人工智能和物联网技术来监测和管理建筑工地的系统。它可以通过感知设备、数据处理和分析、智能控制等技术手段,实现对工地施工、设备状态、人员安全等方面的实时监控和管理。
31 1
|
3月前
|
Java 测试技术 Maven
Spring Boot项目打包配置详解
Spring Boot项目打包配置详解
81 0
|
3月前
|
Java Spring 容器
springcloud项目中指定扫描路径
springcloud项目中指定扫描路径
57 7
|
30天前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
130 0
|
2月前
|
监控 IDE Java
Java项目调试实战:如何高效调试Spring Boot项目中的GET请求,并通过equalsIgnoreCase()解决大小写不一致问题
Java项目调试实战:如何高效调试Spring Boot项目中的GET请求,并通过equalsIgnoreCase()解决大小写不一致问题
40 0
|
3月前
|
前端开发 安全 Java
Spring Boot项目中VO层设计:选择继承或组合的灵活实践
Spring Boot项目中VO层设计:选择继承或组合的灵活实践
92 0
|
1天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
4 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置