使用Spring boot访问HTML页面

最近在实例应用Spring boot 时,访问前段页面一直报404错误,为了防止以后继续跳这个坑,还是写个文章记录下来吧。
本文章主要讲怎么通过spring boot 进入HTML页面。

1.pom文件的依赖:
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--mybatis起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- MySQL连接驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--测试的起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 配置使用redis启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--添加热部署的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

		<!-- 注意必须要引入这个依赖!!!-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

    </dependencies>
2.配置文件

在application.yml文件添加SpringBoot的相关配置,
注:也可以不写该配置,默认读取的是resources/templates/下的页面,下方配置读取的是resources/resources/下的页面

spring:
  thymeleaf:
    prefix: classpath:/resources/
3.创建页面
<!-- classpath:resources/resources
     若第2步没有配置,则此文件的 classpath 应为:resources/templates -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
this is index html  resources
</body>
</html>
4 写跳转方法
@Controller 
public class MapperController {
	
	/**
     * 访问路径为:http://localhost:8080/getIndex
     * @return
     */
    @RequestMapping("/getIndex")
    public String getIndex(){
        System.out.println("开始访问index页面");
        return "index";
    } 
}
5.访问页面

在这里插入图片描述
这样就可以成功访问html页面了。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值