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
目录
相关文章
|
1月前
|
XML Java 应用服务中间件
Spring Boot 两种部署到服务器的方式
本文介绍了Spring Boot项目的两种部署方式:jar包和war包。Jar包方式使用内置Tomcat,只需配置JDK 1.8及以上环境,通过`nohup java -jar`命令后台运行,并开放服务器端口即可访问。War包则需将项目打包后放入外部Tomcat的webapps目录,修改启动类继承`SpringBootServletInitializer`并调整pom.xml中的打包类型为war,最后启动Tomcat访问应用。两者各有优劣,jar包更简单便捷,而war包适合传统部署场景。需要注意的是,war包部署时,内置Tomcat的端口配置不会生效。
293 17
Spring Boot 两种部署到服务器的方式
|
17天前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
53 7
|
2月前
|
存储 JSON 前端开发
【Spring项目】表白墙,留言板项目的实现
本文主要介绍了表白墙项目的实现,包含前端和后端代码,以及测试
|
2月前
|
JSON 前端开发 Java
|
2月前
|
缓存 前端开发 Java
【Spring】——SpringBoot项目创建
SpringBoot项目创建,SpringBootApplication启动类,target文件,web服务器,tomcat,访问服务器
|
3月前
|
存储 运维 安全
Spring运维之boot项目多环境(yaml 多文件 proerties)及分组管理与开发控制
通过以上措施,可以保证Spring Boot项目的配置管理在专业水准上,并且易于维护和管理,符合搜索引擎收录标准。
89 2
|
4月前
|
设计模式 前端开发 Java
Spring MVC——项目创建和建立请求连接
MVC是一种软件架构设计模式,将应用分为模型、视图和控制器三部分。Spring MVC是基于MVC模式的Web框架,通过`@RequestMapping`等注解实现URL路由映射,支持GET和POST请求,并可传递参数。创建Spring MVC项目与Spring Boot类似,使用`@RestController`注解标记控制器类。
67 1
Spring MVC——项目创建和建立请求连接
|
4月前
|
Java 关系型数据库 MySQL
Maven——创建 Spring Boot项目
Maven 是一个项目管理工具,通过配置 `pom.xml` 文件自动获取所需的 jar 包,简化了项目的构建和管理过程。其核心功能包括项目构建和依赖管理,支持创建、编译、测试、打包和发布项目。Maven 仓库分为本地仓库和远程仓库,远程仓库包括中央仓库、私服和其他公共库。此外,文档还介绍了如何创建第一个 SpringBoot 项目并实现简单的 HTTP 请求响应。
356 1
Maven——创建 Spring Boot项目
|
4月前
|
Java 关系型数据库 MySQL
如何使用 maven 创建一个 Spring Boot项目
Maven 是一个强大的项目管理工具,通过配置 `pom.xml` 文件自动获取所需的 jar 包,提高开发效率。其核心功能包括项目构建和依赖管理。项目构建支持编译、测试、打包和发布等流程,而依赖管理则通过中央仓库、本地仓库和私有服务器获取和管理项目依赖。示例中展示了如何创建第一个 SpringBoot 项目并实现简单接口。
92 1
如何使用 maven 创建一个 Spring Boot项目
|
4月前
|
Java Apache Maven
Java/Spring项目的包开头为什么是com?
本文介绍了 Maven 项目的初始结构,并详细解释了 Java 包命名惯例中的域名反转规则。通过域名反转(如 `com.example`),可以确保包名的唯一性,避免命名冲突,提高代码的可读性和逻辑分层。文章还讨论了域名反转的好处,包括避免命名冲突、全球唯一性、提高代码可读性和逻辑分层。最后,作者提出了一个关于包名的问题,引发读者思考。
259 0
Java/Spring项目的包开头为什么是com?