Java Spring MVC Controller:“返回转发”、“返回重定向”和“返回jsp文件”有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28697681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:39:44  来源:igfitidea点击:

Spring MVC Controller: what is the difference between "return forward", "return redirect" and "return jsp file"

javaspringhttpspring-mvcjakarta-ee

提问by Argamidon

I don't understand what I should use. I have two pages - intro.jsp(1) and booksList.jsp(2). For each page I created one Controller Class. The first page has button which opens second page:

我不明白我应该使用什么。我有两页 - intro.jsp(1) 和 booksList.jsp(2)。对于每一页,我创建了一个控制器类。第一页有打开第二页的按钮:

<form method="GET" action="/request-list">
        <input type="submit"/>
</form>

The first question is:I am not sure about correctness this button. It works well, but I have question mark after press this button.

第一个问题是:我不确定这个按钮的正确性。它运作良好,但按下此按钮后我有问号。

The second question is:When I press that button, method with next annotation is called (Controller for the second page):

第二个问题是:当我按下那个按钮时,带有下一个注释的方法被调用(第二页的控制器):

@RequestMapping(value = "/books")
@Controller
public class BooksListController {

   @RequestMapping
   public String booksList() {
      return "jsp/books/booksList";
   }
}

What should I return by this method? In other words how can I jump from first page to second one?

我应该通过这种方法返回什么?换句话说,我怎样才能从第一页跳到第二页?

  1. return "redirect:/books"; returns http://localhost:8080/books?
  2. return "jsp/books/booksList"; returns http://localhost:8080/request-list?
  3. return "forward:/books"; returns http://localhost:8080/request-list?
  1. return "redirect:/books"; returns http://localhost:8080/books?
  2. return "jsp/books/booksList"; returns http://localhost:8080/request-list?
  3. return "forward:/books"; returns http://localhost:8080/request-list?

I see that result is the same: all these Strings gave me the same page (page 2 was opened). In which cases I should use "redirect", "forward", "page.jsp"?

我看到结果是一样的:所有这些字符串都给了我相同的页面(打开了第 2 页)。在哪些情况下我应该使用“重定向”、“转发”、“page.jsp”?

Also I've read Post/Redirect/Get article. Do I have to use "redirect" after POST method handling??

我也读过发布/重定向/获取文章。POST 方法处理后是否必须使用“重定向”?

采纳答案by Marcelo Keiti

The first question is: I am not sure about correctness this button. It works well, but I have question mark after press this button.

第一个问题是:我不确定这个按钮的正确性。它运作良好,但按下此按钮后我有问号。

Ok, it's insert a question mark because you use GET http method. You need to use POST method to pass the data in the request payload.

好的,这是插入一个问号,因为您使用的是 GET http 方法。您需要使用 POST 方法在请求负载中传递数据。



return "redirect:/books";

It returns to the client (browser) which interprets the http response and automatically calls the redirect URL

它返回给客户端(浏览器),客户端(浏览器)解释 http 响应并自动调用重定向 URL

return "jsp/books/booksList";

It process the JSP and send the HTML to the client

它处理 JSP 并将 HTML 发送到客户端

return "forward:/books";

It transfer the request and calls the URL direct in the server side.

它传输请求并直接在服务器端调用 URL。



To decide which one to use you have to consider some aspects of each approach:

要决定使用哪一种,您必须考虑每种方法的某些方面:

Forward: is faster, the client browser is not involved, the browser displays the original URL, the request is transfered do the forwarded URL.

转发:更快,客户端浏览器不参与,浏览器显示原始URL,请求转发做转发的URL。

Redirect: is slower, the client browser is involved, the browser displays the redirected URL, it creates a new request to the redirected URL.

重定向:较慢,涉及客户端浏览器,浏览器显示重定向的 URL,它创建对重定向 URL 的新请求。