SpringBoot中Controller之间跳转并携带参数,下面是具体步骤
1. 使用Controller注解
@Controller
2. 添加thymeleaf依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3. 添加配置
spring:
thymeleaf:
prefix: classpath:/templates/
mode: html5
encoding: utf-8
suffix: .html
4. 在resources下的templates页面下放置你要跳转的html即可
下面是请求方法:
@PostMapping("/user/query")
public String query(@RequestBody User user, RedirectAttributes attr, HttpServletResponse response) throws IOException {
if (null == user) {
response.setCharacterEncoding("UTF-8");
response.getWriter().write(JsonUtils.toJson("错误"));
return null;
}
if (merchantOrderVO.getHtml() == 1) {
attr.addFlashAttribute("user",user);
return "redirect:/user/queryHtml";
}
return "user.html";
}
/**
* 返回网页模板
*
* @param
* @return
*/
@RequestMapping("/user/queryHtml")
public String createOrderHtml(@ModelAttribute("user") User user) {
if (null == user) {
return "404.html";
}
return "user.html";
}