springboot搭建web项目完整步骤

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,成功访问!localhost:8080/

遇到的问题

浏览器报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、项目启动类创建的路径不对,必须是父路径,其他包路径必须是其子路径,我就是这里出错的,排查了几个小时才得以解决,以此为戒。

  • 14
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值