springboot点击按钮跳转
时间: 2023-10-13 12:23:31 浏览: 92
在Spring Boot中,可以通过使用Thymeleaf模板引擎来实现页面跳转。具体实现步骤如下:
1. 在pom.xml文件中添加Thymeleaf和Spring Boot Web依赖:
```xml
<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>
```
2. 在controller中添加请求处理方法,并返回跳转页面的名称:
```java
@Controller
public class MyController {
@RequestMapping("/home")
public String home() {
return "home";
}
}
```
其中,@Controller注解用于标识这是一个控制器类,@RequestMapping注解用于指定请求的URL路径。
3. 在resources/templates目录下创建跳转页面的HTML文件,例如home.html:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home Page</title>
</head>
<body>
<h1>Welcome to Home Page</h1>
</body>
</html>
```
4. 在HTML文件中添加跳转按钮:
```html
<a href="/home">跳转到Home Page</a>
```
其中,href属性的值为请求处理方法中指定的URL路径。
5. 运行Spring Boot应用,访问http://localhost:8080,点击跳转按钮即可跳转到home.html页面。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)