简体   繁体   English

在Spring Controller中重定向的最佳实践

[英]Best Practice of redirecting in Spring Controller

Here I have to controller methods for example, 这里我有控制器方法,例如,

(Case 1) One way is (案例1)一种方法是

@Controller
@requestMapping("main")
Class ControllerClass{

    @RequestMapping("first", method = RequestMethod.POST)
    public String post(Model model){
       //processing  
       return "redirect:second";
    }

    @RequestMapping("second", method = RequestMethod.GET)
    public String post(Model model){
       //processing  
       return "myview";
    }
}

And (Case 2) another way is 而(案例2)另一种方式是

@Controller
@requestMapping("main")
Class ControllerClass{

    @RequestMapping("first", method = RequestMethod.POST)
    public String post(Model model){
       //processing  
       return "redirect:/main/second";
    }

    @RequestMapping("second", method = RequestMethod.GET)
    public String post(Model model){
       //processing  
       return "myview";
    }
}

Both ways work correctly, but I want to know which one is better to avoid future problems like one I faced recently: 两种方式都可以正常工作,但我想知道哪一种更好,以避免像我最近遇到的未来问题:

When I forwarded the request to /main/first from another controller, I got a 404 error in code which is using Case 1. 当我将请求从另一个控制器转发到/ main / first时 ,我在使用Case 1的代码中遇到404错误。

As per Spring Documentation: 根据Spring文档:

The redirect: prefix 重定向:前缀

While the use of RedirectView works fine, if the controller itself creates the RedirectView, there is no avoiding the fact that the controller is aware that a redirection is happening. 虽然RedirectView的使用工作正常,但如果控制器本身创建RedirectView,则无法避免控制器意识到正在发生重定向的事实。 This is really suboptimal and couples things too tightly. 这实际上并不是最理想的,而是将事情过于紧密。 The controller should not really care about how the response gets handled. 控制器不应该真正关心如何处理响应。 In general it should operate only in terms of view names that have been injected into it. 通常,它应该仅根据已注入其中的视图名称进行操作。

The special redirect: prefix allows you to accomplish this. 特殊的重定向:前缀允许您完成此操作。 If a view name is returned that has the prefix redirect:, the UrlBasedViewResolver (and all subclasses) will recognize this as a special indication that a redirect is needed. 如果返回的视图名称具有前缀redirect:,则UrlBasedViewResolver(以及所有子类)会将此识别为需要重定向的特殊指示。 The rest of the view name will be treated as the redirect URL. 视图名称的其余部分将被视为重定向URL。

The net effect is the same as if the controller had returned a RedirectView, but now the controller itself can simply operate in terms of logical view names. 净效果与控制器返回RedirectView的效果相同,但现在控制器本身可以简单地按照逻辑视图名称进行操作。 A logical view name such as redirect:/myapp/some/resource will redirect relative to the current Servlet context, while a name such as redirect: http://myhost.com/some/arbitrary/path will redirect to an absolute URL. 逻辑视图名称(例如redirect:/ myapp / some / resource)将相对于当前Servlet上下文重定向,而重定向等名称: http//myhost.com/some/arbitrary/path将重定向到绝对URL。

Most Real time enterprise project prefers to use case 2 in all controllers they use,so that the inter calling between different controller is fine. 大多数实时企业项目更喜欢在他们使用的所有控制器中使用案例2,因此不同控制器之间的内部调用很好。

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

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