简体   繁体   English

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

[英]What is the right way to handle redirect in Spring Boot?

After research on subjects concerning differences between spring boot and previous spring mvc frameworks. 经过研究有关弹簧靴和以前的弹簧mvc框架之间差异的主题。 I am confused and need some help! 我很困惑,需要一些帮助! How to do redirect to an html page since normally trying to redirect without the page target having a controller method returns HTTP 404. 如何重定向到html页面,因为通常在页面目标没有控制器方法的情况下尝试重定向将返回HTTP 404。

if I have this controller method below that handles registration confirm: 如果我下面有处理注册的控制器方法,请确认:

@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();
    }

I am getting http 404 error not found. 我收到找不到HTTP 404错误的提示。 Is it a must to have a Controller method specific for wrong.html page? 是否必须具有特定于错误.html页面的Controller方法? if it is not required, how to run it that way. 如果不是必需的,该如何运行。

After your response is redirected to '/wrong.html', spring need to know how to handle '/wrong.html'. 将您的响应重定向到“ /wrong.html”后,spring需要知道如何处理“ /wrong.html”。 Maybe, You made no handler for '/wrong.html', so spring doesn't know how to handle it and responses 404. 也许,您没有为'/wrong.html'做任何处理程序,所以spring不知道如何处理它并响应404。

This answer might help you : How to serve .html files with Spring 这个答案可能会对您有所帮助: 如何在Spring中提供.html文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Spring Boot 中处理异常的最佳方法是什么? - What is the best way to handle exception in Spring Boot? 解决该并发问题的正确方法是什么? 春季靴 - What is the right way to architect this concurrency issue? Spring Boot Spring Boot:使用 httpheaders 实现基本身份验证的正确方法是什么 - Spring Boot: What is the Right Way to Implement Basic Auth with httpheaders 在 spring-webflux 中处理错误的正确方法是什么 - what is the right way to handle errors in spring-webflux 在Spring Boot中处理异常的正确方法 - Correct way to handle exceptions in Spring Boot 什么是在 Spring 引导中向用户发送电子邮件而不启用“访问不太安全的应用程序”的正确方法? - What is the right way to send emails to users in Spring Boot without enabling “Access for less secure apps”? 在此Spring Boot \\ MVC控制器中处理REST样式映射的正确方法是什么? - What is the correct way to handle REST style mapping in this Spring Boot\MVC controller? 在Java中处理NaN的正确方法是什么? - What is the right way to handle NaN in Java? 部署Spring引导应用程序的正确方法是什么 - What is the correct way to deploy a spring boot application 在Java,Spring和Maven中工作的正确方法是什么? - what is the right way to work in java, spring and maven?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM