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

时间:2018-02-10 10:27:48

标签: java spring spring-mvc spring-boot thymeleaf

在研究春季靴子和之前的春季mvc框架之间的差异的主题后。我很困惑,需要一些帮助! 如何重定向到html页面,因为通常尝试重定向而页面目标没有控制器方法会返回HTTP 404.

如果我有以下处理注册确认的控制器方法:

@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错误。是否必须具有特定于wrong.html页面的Controller方法?  如果不需要,如何以这种方式运行。

1 个答案:

答案 0 :(得分:0)

将您的回复重定向到' /wrong.html'后,Spring需要知道如何处理' /wrong.html'。也许,你没有为' /wrong.html'制作处理程序,所以Spring并不知道如何处理它和回复404.

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

相关问题