1、现在有许多公司都在使用spring boot的开发框架,因此本篇博客主要介绍如何使用IDEA创建一个简单的spring boot项目。
2、打开idea ,点击file-new-Project,进入idea的新建项目。
3、然后出现如下图所示的页面,在弹出的页面中我们选择左侧的spring initalizr,jdk版本选择自己安装的版本,我这里选择1.8,URL选择默认的就可以了。然后点击next进入下一个界面。
4、下一个页面:在Group栏输入组织名,Artifact就是项目名。其它的直接选择默认就可以了。
5、点击next进入下一个页面之后,由于需要构建一个web项目通过访问Controller进行验证项目是否搭建成功。所以需要选择web 勾选Spring Web Starter。
6、再点击next之后显示如下信息。
7、点击finish之后,等一段时间,idea会打开一个新的界面,里面就是咱们创建项目的工作页面了。(如果你是第一次创建spring boot项目的话,需要等一段时间,耐心等待就好。)
8、如下图所示,这里就是咱们项目的启动类了。
双击打开这个类,下面就是spring boot启动类的代码啦。
package com.b505.newdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class NewdemoApplication {
public static void main(String[] args) {
SpringApplication.run( NewdemoApplication.class, args );
}
}
现在启动项目,spring boot项目在没有配置的情况下默认是使用的8080端口,所以启动完毕之后,访问http://localhost:8080/,会出现入下图所示的界面,这是因为还有写Controller接口,所以下面咱们就写一个Controller接口
9、新建一个java类,形式如下图所示,双击DemoController进行编辑
package com.b505.newdemo.newdemoController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DemoController {
@RequestMapping("index")
public String index(){
return "index.html";
}
}
10、这里还需要进行对spring boot项目进行配置和依赖的添加,需要在pom.xml中添加thymeleaf依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
还需要在applocation.properties中添加一个配置:
spring.thymeleaf.prefix=classpath:/templates/
11、然后就是需要在templates中添加一个html页面,这里我随便写了一个,命名为index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<p>哈哈</p>
</body>
</html>
最后启动项目,访问http://localhost:8080/index,就可以正常访问咱们的项目了。最后希望我的博客对初学spring boot的小白提供一些帮助。