springboot搭建web项目完整步骤
1、使用idea工具创建springboot项目第一步:后续直接点next,下一步直到进入依赖选择页面,Web选择Spring Web,Template Engines选择Thymeleaf(模板引擎若在项目建立之后需要改动,可在application.yml进行配置即可),SQL选择JDBC API,Mybatis,MySQL Driver2、建好项目后会自动生成pom.xml并自动...
1、使用idea工具创建springboot项目
第一步:
后续直接点next,下一步
直到进入依赖选择页面,Web选择Spring Web,Template Engines选择Thymeleaf(模板引擎若在项目建立之后需要改动,可在application.yml进行配置即可),SQL选择JDBC API,Mybatis,MySQL Driver
2、建好项目后会自动生成pom.xml并自动添加了相应的maven依赖
<?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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</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>
3、配置mysql数据源,因为引入了mysql依赖,所以这一步不能省略,使用application.yml配置,需要将application.properties删除:
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
4、环境搭建好了,可以正式开始写代码。
- HelloController.java
@Controller //可以返回视图
//自动为springboot应用进行配置
//@EnableAutoConfiguration
public class HelloController {
@RequestMapping("/myIndex")
public String index() {
System.out.println("hello.springboot的第一个controller");
return "index1";
}
}
-在resources/template路径下创建 index1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
你好,这是我的第一个springboot页面,thymeleaf
</body>
</html>
- 在所有java文件的父路径下创建springboot启动类,DemoApplication.java
//标识为springboot启动类,必须是父路径,其他包路径必须是其子路径
//@ComponentScan(basePackages = "com")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5、浏览器输入http://localhost:8080/myIndex,成功访问!
遇到的问题
浏览器报Whitelabel Error Page
controller返回的字符串找不到对应的html
解决思路:
1、url访问出错,若是controller类使用了@RequestMapping(“xx”),方法中使用@RequestMapping(“index”),那么url应是localhost:8080/xx/index
若是controller类不使用@RequestMapping,则访问的url是localhost:8080/index
2、controller注解写成了RestController,,返回的是json格式数据
3、项目启动类创建的路径不对,必须是父路径,其他包路径必须是其子路径,我就是这里出错的,排查了几个小时才得以解决,以此为戒。
更多推荐
所有评论(0)