gpt4 book ai didi

java - 在 Spring Boot 中处理重定向的正确方法是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 10:43:09 25 4
gpt4 key购买 nike

经过对有关 Spring Boot 和以前的 Spring MVC 框架之间差异的主题的研究。我很困惑,需要一些帮助!如何重定向到 html 页面,因为通常在没有具有 Controller 方法的页面目标的情况下尝试重定向会返回 HTTP 404。

如果我有下面的 Controller 方法来处理注册确认:

@RequestMapping(value = "/registrationConfirm", method = RequestMethod.GET)
public String confirmRegistration(final HttpServletRequest request, Model
model, @RequestParam("token") final String token){
Locale locale = request.getLocale();
final String result = userService.validateConfirmationToken(token);
if (result.equals("valid")) {
final User user = userService.getUser(token);
authWithoutPassword(user);
model.addAttribute("message",
messages.getMessage("message.accountVerified", null, locale));
return "redirect:/home.html?lang=" + locale.getLanguage();
}

model.addAttribute("message", messages.getMessage("auth.message." +
result, null, locale));
model.addAttribute("expired", "expired".equals(result));
model.addAttribute("token", token);
return "redirect:/wrong.html?lang=" + locale.getLanguage();
}

我收到 http 404 错误,未找到。是否必须有一个特定于错误.html 页面的 Controller 方法? 如果不需要,如何运行它。

最佳答案

当你的响应被重定向到'/wrong.html'后,spring需要知道如何处理'/wrong.html'。也许,你没有为 '/wrong.html' 创建处理程序,所以 spring 不知道如何处理它并响应 404。

这个答案可能对您有帮助:How to serve .html files with Spring

关于java - 在 Spring Boot 中处理重定向的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48719715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com