springboot跳转jsp页面
时间: 2023-11-19 13:02:37 浏览: 117
springboot中使用Thymeleaf模板引擎来实现跳转到jsp页面。
首先,确保在pom.xml文件中引入了Thymeleaf以及Spring Boot相关的依赖。然后在application.properties文件中配置Thymeleaf的相关属性。
接下来,在Spring Boot的Controller中定义一个请求处理方法,使用@GetMapping或@PostMapping注解来映射URL路径。在方法中,通过返回一个字符串类型的视图名称来实现跳转。
例如,假设我们有一个UserController类,其中有一个方法用于处理"/user"路径的请求:
```java
@Controller
public class UserController {
@GetMapping("/user")
public String showUser(Model model) {
// 处理业务逻辑,将数据存入model对象中
model.addAttribute("username", "John Doe");
return "user"; // 返回user作为视图名称
}
}
```
注意,跳转的jsp页面的名称应与返回的视图名称对应。同时,该jsp文件需要保存在src/main/resources/templates目录下。
在jsp页面中,可以使用Thymeleaf的语法来显示从Controller传递的数据。例如,在user.jsp中可以使用如下代码显示用户名:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>User Page</title>
</head>
<body>
<h1>Welcome, <span th:text="${username}"></span>!</h1>
</body>
</html>
```
在浏览器中访问"/user"路径,就可以看到跳转到user.jsp页面并显示用户名的效果。
通过上述步骤,我们就可以在Spring Boot中使用Thymeleaf模板引擎来实现跳转到jsp页面。
阅读全文
相关推荐


















