简体   繁体   English

我可以使用转发而不是重定向来提高效率吗?

[英]can i use forward instead of redirect for efficiency?

I have read the difference between forward and redirect. 我已经阅读了转发和重定向之间的区别。

http://grails.asia/grails-redirect-vs-forward http://grails.asia/grails-redirect-vs-forward

Sorry for the naive question but it seemed to me that since redirect goes back to browser and browser issues the new request, forward seems more efficient. 很抱歉,这个问题很幼稚,但在我看来,由于重定向返回到浏览器并且浏览器发出了新请求,因此转发似乎更有效。 So i was wondering why use redirect at all when the same purpose is achieve by using forward which is more efficient? 所以我想知道为什么在通过使用更有效的forward实现相同目的时完全使用重定向? Are there situations when the right thing to use is redirect instead of forward. 在某些情况下,使用的正确方法是重定向而不是转发。 Thanks for help! 感谢帮助!

The main difference here is the browser getting "touched" in redirect (mentioned already). 这里的主要区别是在重定向(已经提到)得到“感动”的浏览器。

It's easy to demonstrate it for the login use-case. 在登录用例中很容易演示它。

A user gets a /login page and submits a login form to /doLogin , that results in his /profile page being shown. 用户获得/login页面,然后向/doLogin提交登录表单,这将导致显示/profile页面。 The login action validates the user input. 登录操作将验证用户输入。

Here comes the critical difference: if the action performs forward operation, than the user is going to see /doLogin in the url-box. 关键区别在于:如果该操作执行forward操作,则用户将在url框中看到/doLogin That's why the action should perform redirect , so that the user sees /profile in the url-box AND so the browser can process back-navigation properly. 这就是该操作应执行redirect的原因,以便用户在url框中看到/profile ,并且浏览器可以正确处理向后导航。

For other actions which do not require any state change which should be considered in browser history, you could use forward . 对于不要求应在浏览器历史记录被认为是任何状态变化等动作,你可以使用forward

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

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