ideaspringboot页面跳转,页面1跳2,2跳3
时间: 2024-04-11 10:28:03 浏览: 84
在Spring Boot中实现页面跳转有多种方式,以下是其中一种常用的方法:
1. 创建控制器类(Controller):在控制器类中定义不同的请求处理方法,用于处理页面跳转逻辑。
```java
@Controller
public class MyController {
@RequestMapping("/page1")
public String page1() {
return "page1";
}
@RequestMapping("/page2")
public String page2() {
return "page2";
}
@RequestMapping("/page3")
public String page3() {
return "page3";
}
}
```
2. 创建页面文件:在src/main/resources/templates目录下创建对应的HTML文件。
- page1.html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Page 1</title>
</head>
<body>
<h1>Page 1</h1>
<a href="/page2">Go to Page 2</a>
</body>
</html>
```
- page2.html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Page 2</title>
</head>
<body>
<h1>Page 2</h1>
<a href="/page3">Go to Page 3</a>
</body>
</html>
```
- page3.html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Page 3</title>
</head>
<body>
<h1>Page 3</h1>
</body>
</html>
```
3. 配置视图解析器:在application.properties文件中配置Thymeleaf视图解析器。
```properties
spring.mvc.view.prefix=/templates/
spring.mvc.view.suffix=.html
```
4. 启动应用程序:运行Spring Boot应用程序,并访问相应的URL即可实现页面跳转。
- 访问http://localhost:8080/page1 将跳转到页面1;
- 在页面1中点击链接"Go to Page 2",将跳转到页面2;
- 在页面2中点击链接"Go to Page 3",将跳转到页面3。
这样就完成了页面之间的跳转。你可以根据需要自定义更多的页面和跳转逻辑。
阅读全文
相关推荐


















