thymeleaf模板在接口调用成功后跳转另一个界面
时间: 2024-04-09 19:33:25 浏览: 21
在Thymeleaf模板中,你可以使用重定向或转发来实现接口调用成功后跳转到另一个页面。
如果你想要进行重定向,可以在控制器方法中返回一个重定向的字符串。例如:
```java
@Controller
public class MyController {
@Autowired
private MyService myService;
@GetMapping("/my-endpoint")
public String myEndpoint() {
// 调用接口方法
boolean success = myService.callApi();
if (success) {
// 接口调用成功,重定向到另一个页面
return "redirect:/another-page";
} else {
// 接口调用失败,返回当前页面或错误页面
return "error";
}
}
@GetMapping("/another-page")
public String anotherPage() {
// 返回另一个页面的模板名称
return "another-page";
}
}
```
在上面的例子中,`myEndpoint()` 方法是一个GET请求的控制器方法,调用了一个名为`callApi()`的服务方法。如果接口调用成功,它将返回重定向字符串"redirect:/another-page",将用户重定向到"/another-page"路径。如果接口调用失败,你可以返回当前页面或错误页面的模板名称。
另外,如果你想要进行转发而不是重定向,可以将返回字符串改为转发的路径,如`return "forward:/another-page"`。
请确保你的Thymeleaf模板配置正确,并且"/another-page"路径对应于另一个页面的模板。