如何使用IDEA创建SpringBoot项目
第一步
选择Spring Initializr,并且将jdk切换为你想要的版本
点击Next
第二步
对项目进行配置,这里可以选择以war包的形式,点击Next
以jar包的形式
在这里插入图片描述
以war包的形式
如果点击Next报错Artifact contains illegal characters
把artifact改为小写就好了
第三步
选择starter的窗口,这里可以选择我们需要的starter进行依赖
这里只选择web,spring web
第四步
点击finish
第五步:增加web的module
IDEA开始创建项目,在默认的项目结构中,增加
src/main/webapp
src/main/webapp/WEB-INF
点击File-Project Structure,添加web的module
添加web资源路径
E:\ideaWorkspace\springboot_study_day01\src\main\webapp
新增tomcat服务
新增web.xml
E:\ideaWorkspace\springboot_study_day01\src\main\webapp\WEB-INF\web.xml
第六步:增加JSP和JSTL的Maven依赖
在pom增加JSP和JSTL的Maven依赖
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.suda</groupId>
<artifactId>springboot_study_day01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_study_day01</name>
<description>springboot_study_day01 project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第七步:修改application.properties
# 端口
server.port=8082
# 上下路径
server.servlet.context-path=/springBootStudyDay01
# 配置视图解析器,定义视图前后缀
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
第八步:增加WEB-INF/jsp/index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>hello wrold</h1>
</body>
</html>
第八步:增加控制层代码
@Controller
@RequestMapping("/userController")
public class UserController {
@RequestMapping(value = "getUser",method = RequestMethod.GET)
public String getUser(){
System.out.println("UserController getUser begin");
return "index";
}
}
最终目录结构
启动项目
直接运行启动类SpringbootStudyDay01Application,就可以运行这个springBoot项目了
访问结果
http://localhost:8082/springBootStudyDay01/userController/getUser