在 Spring Boot 中,你可以使用控制器返回视图的名称,从而跳转到指定的 HTML 页面。这通常是通过 Spring MVC 的 @Controller 注解和 @RequestMapping(或 @GetMapping 等)来完成的。返回的视图名称会映射到 src/main/resources/templates 目录下的模板文件,比如 Thymeleaf 模板引擎。
下面是实现跳转的步骤:
0. 文件结构
1. 配置 Thymeleaf 依赖
首先,如果你使用 Thymeleaf 来渲染 HTML 页面,你需要在 pom.xml 中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
pom.xml
<?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>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.webapp</groupId>
<artifactId>wapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>wapp</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 编写控制器类
你需要在MyController.class控制器中定义一个方法,该方法返回视图的名称。假设你有一个名为 index.html 的页面:
MyController.class
package com.webapp.wapp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/")
public class MyController {
@GetMapping("/home")
public String home() {
return "redirect:/index.html";
}
@GetMapping("/about")
public String about() {
return "about";
}
@GetMapping("/greet")
public String greet(@RequestParam(name = "name", required = false, defaultValue = "Guest") String name,
Model model) {
model.addAttribute("username", name);
return "greet";
}
}
3. 配置 HTML 文件
将 index.html 文件放在 src/main/resources/templates/ 目录下。Spring Boot 会自动寻找这个目录下的 HTML 文件进行渲染。
例如,src/main/resources/static/index.html 可以是:
src\main\resources\static\index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home Page</title>
</head>
<body>
<h1>Welcome to the Home Page</h1>
</body>
</html>
src\main\resources\templates\about.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>About Page</title>
</head>
<body>
<h1>About Us</h1>
<p>This is the about page of our application.</p>
</body>
</html>
src\main\resources\templates\greet.html
http://localhost:8080/greet?name=Taka
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Greeting Page</title>
</head>
<body>
<h1>Welcome, <span th:text="${username}">User</span>!</h1>
<p>This is your personalized greeting page.</p>
</body>
</html>
4. 访问方法并跳转到页面
启动 Spring Boot 应用后,访问 http://localhost:8080/home,它将会自动跳转并渲染 index.html 页面。
如果一切配置正确,访问 /home 路径时,Spring Boot 会自动将控制器返回的视图名(即 "index")映射到 src/main/resources/templates/index.html 并渲染这个页面。
http://localhost:8080/greet?name=Taka
注意:
- 你需要确保正确配置了模板引擎(如 Thymeleaf)并将 HTML 文件放在
templates目录下。 - 如果你使用了其他模板引擎,步骤基本类似,但可能需要调整配置。
通过这种方式,你就可以在访问指定 URL 时跳转到对应的 HTML 页面了。