您的位置:首页 > 编程语言 > Java开发

使用Spring boot访问HTML页面

2020-07-14 04:56 337 查看

最近在实例应用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页面了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: