Spring MVC Controller:“return forward”,“return redirect”和“return return”之间的区别是什么?和“返回jsp文件” [英] Spring MVC Controller: what is the difference between "return forward", "return redirect" and "return jsp file"
问题描述
我不明白我应该使用什么。
我有两个页面--intro.jsp(1)和booksList.jsp(2)。对于每个页面,我创建了一个Controller类。
第一页有按钮打开第二页:
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:
<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?
-
returnredirect:/ books;返回http:// localhost:8080 / books?
-
returnjsp / books / booksList;返回http:// localhost:8080 / request-list?
-
returnforward:/ books;返回http:// localhost:8080 / request-list?
return "redirect:/books"; returns http://localhost:8080/books?
return "jsp/books/booksList"; returns http://localhost:8080/request-list?
return "forward:/books"; returns http://localhost:8080/request-list?
我看到结果是一样的:所有这些字符串给了我相同的页面(第2页打开)。
在哪些情况下我应该使用redirect,forward,page.jsp?
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"?
我还读过发布/重定向/获取文章。在POST方法处理后我是否必须使用重定向?
Also I've read Post/Redirect/Get article. Do I have to use "redirect" after POST method handling??
推荐答案
第一个问题是:我不确定这个按钮的正确性。
效果很好,但按下此按钮后我有问号。
The first question is: I am not sure about correctness this button. It works well, but I have question mark after press this button.
好的,它会插入一个问号,因为你使用GET http方法。您需要使用POST方法传递请求有效负载中的数据。
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.
return "redirect:/books";
它返回客户端(浏览器),解释http响应并自动调用重定向URL
It returns to the client (browser) which interprets the http response and automatically calls the redirect URL
return "jsp/books/booksList";
它处理JSP并将HTML发送到客户端
It process the JSP and send the HTML to the client
return "forward:/books";
它传输请求并直接在服务器端调用URL。
It transfer the request and calls the URL direct in the server side.
要决定使用哪一种,你必须考虑每种方法的某些方面:
To decide which one to use you have to consider some aspects of each approach:
转发:速度更快,客户端浏览器不参与,浏览器显示原始URL,转发请求转发URL。
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.
这篇关于Spring MVC Controller:“return forward”,“return redirect”和“return return”之间的区别是什么?和“返回jsp文件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!