springboot跳转到html页面
时间: 2023-04-30 21:02:57 浏览: 94
在 Spring Boot 中跳转到 HTML 页面可以使用控制器(Controller)来实现。首先,在项目中创建一个控制器类,并在其中添加一个方法,该方法将返回一个字符串,该字符串表示 HTML 页面的路径。然后,使用 @RequestMapping 注解将该方法映射到特定的 URL 上。在 HTML 页面中使用 thymeleaf 模板引擎来进行渲染。
示例代码:
```
@Controller
public class MyController {
@RequestMapping("/")
public String index() {
return "index";
}
}
```
在上面的代码中,当用户访问根 URL 时(即 http://localhost:8080/ ),将会调用 index() 方法,并返回字符串 "index"。这个字符串表示模板文件的名称,文件应该放在 resources/templates 目录下。
需要注意的是,在项目的 pom.xml 中引入 thymeleaf 依赖,并且在 application.properties 中配置 thymeleaf 模板引擎。
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
```
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
```
这样你就可以通过访问根URL访问到index.html页面了。