springboot web点击在本页面内跳转
时间: 2023-12-20 08:03:55 浏览: 234
在Spring Boot中,可以使用Thymeleaf模板引擎来实现在同一页面内进行跳转。具体步骤如下:
1. 在pom.xml文件中添加Thymeleaf依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
2. 在Spring Boot配置文件中添加Thymeleaf配置:
```properties
spring.thymeleaf.cache=false
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
```
3. 在模板文件中使用Thymeleaf标签进行页面跳转,例如:
```html
<button onclick="window.location.href='/home'">跳转到首页</button>
```
4. 在Controller类中添加对应的RequestMapping处理方法,例如:
```java
@GetMapping("/home")
public String home(Model model) {
// 处理业务逻辑
return "home";
}
```
5. 在templates目录下创建home.html文件,用于显示跳转后的页面内容。
注意:在Thymeleaf中,可以使用${...}语法来引用后台传递的变量,例如:
```html
<h1 th:text="${title}">默认标题</h1>
```
这样可以在后台处理完业务逻辑后,将标题传递到前端页面进行渲染。
阅读全文
相关推荐

















