简体   繁体   English

重定向不适用于 Spring 引导应用程序中的 Thymeleaf

[英]Redirect not working with Thymeleaf in Spring Boot app

I'am trying to create a simple app on Spring Boot: Controller:我正在尝试在 Spring 上创建一个简单的应用程序启动:Controller:

 @Controller
 public class RootController {

    @RequestMapping ("/")
    public String root() {
         return "login";
    }

    @RequestMapping(path = "/joinChart", method = RequestMethod.GET)
    public String joinChart(@RequestParam (defaultValue = "") String username) {
          return "redirect:chat?username=" + username;
    }
 }

root-method is working well.根方法运行良好。 But when I try to redirect on login-page, it return 404-ERROR.但是当我尝试在登录页面上重定向时,它返回 404-ERROR。

项目

When you do当你这样做

return "login";

in your root() handler method, you're actually just returning a view name (typically handled by a JstlView but here handled by ThymeleafView since you're using Thymeleaf), but that Spring MVC's web infrastructure (with your Servlet web container) will try to find and render. in your root() handler method, you're actually just returning a view name (typically handled by a JstlView but here handled by ThymeleafView since you're using Thymeleaf), but that Spring MVC's web infrastructure (with your Servlet web container) will尝试查找和渲染。

When you do当你这样做

return "redirect:chat?username=" + username;

in your joinChart handler method, you're actually returning a redirect view name that the server will render as a 303 response with a Location header.在您的joinChart处理程序方法中,您实际上返回了一个重定向视图名称,服务器将使用Location header 将其呈现为 303 响应。 The client will be instructed to send a new request to /chat .将指示客户端向/chat发送新请求。 If you don't have a request mapping for that, obviously the server will return a 404.如果您没有相应的请求映射,显然服务器将返回 404。

You need to add a request mapping for /chat您需要为/chat添加请求映射

@RequestMapping ("/chat")
public String chat() {
     return "chat";
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM