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

标签 java spring spring-mvc spring-boot thymeleaf

经过对有关 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/

相关文章:

java - 如何在命令提示符中更改类路径?

Java Mockito : Cannot doReturn child classes?

java - 如何在java spring MVC中为相应行插入按钮功能

spring - 使用 @OneToMany 或 @ManyToMany 定位未映射的类 : com. example.soasec.entities.User.roles[com.example.soasec.entities.Role]

spring - 通过向表添加行在 thymeleaf 中创建动态列表对象

java - Spring Security 中 JSON 的主要对象

java - Spring security spring webflow 如何使用用户角色成功登录页面后重定向?

java - fillRect 或drawImage 未在java 的更新方法中激活

java - 使用 Query for FirestoreRecyclerAdapter 时,我没有调用 get() 但需要设置 Query.get(Source.SERVER)

java - 如何创建 org.springframework.util.concurrent.ListenableFuture 的完整实例