重定向和导航/转发之间的区别是什么?什么时候使用什么? [英] What is the difference between redirect and navigation/forward and when to use what?

查看:173
本文介绍了重定向和导航/转发之间的区别是什么?什么时候使用什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSF中的导航有什么区别

What is difference between a navigation in JSF

FacesContext context = FacesContext.getCurrentInstance();
context.getApplication().getNavigationHandler().handleNavigation(context, null, url);

和重定向

HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.sendRedirect(url);

以及如何决定何时使用什么?

and how to decide when to use what?

导航问题是页面URL不会更改,除非将faces-redirect=true添加到导航URL的查询字符串中.但是,在我的情况下,如果我想重定向到非JSF页面(如纯HTML页面),则添加faces-redirect=true会引发错误.

The issue with navigation is that page URL does not change unless faces-redirect=true is added to the query string of the navigation URL. However, in my case appending faces-redirect=true throws error if I want to redirect to a non-JSF page like a plain HTML page.

另一种方法是BalusC在 JSF 2.0重定向错误中建议的

And another option is as BalusC suggested at JSF 2.0 redirect error

推荐答案

首先,术语重定向"是在Web开发领域中的一种操作,该操作是向客户端发送仅带有Location标头的空HTTP响应.客户端必须在其上发送全新的GET请求的新URL.所以基本上:

First of all, the term "redirect" is in web development world the action of sending the client an empty HTTP response with just a Location header with therein the new URL on which the client has to send a brand new GET request. So basically:

  • 客户端向somepage.xhtml发送HTTP请求.
  • 服务器使用Location: newpage.xhtml标头发送回HTTP响应
  • 客户端向newpage.xhtml发送HTTP请求(这将反映在浏览器地址栏中!)
  • 服务器发送回HTTP响应,内容为newpage.xhtml.
  • Client sends a HTTP request to somepage.xhtml.
  • Server sends a HTTP response back with Location: newpage.xhtml header
  • Client sends a HTTP request to newpage.xhtml (this get reflected in browser address bar!)
  • Server sends a HTTP response back with content of newpage.xhtml.

您可以使用Web浏览器的内置/附加开发人员工具集对其进行跟踪.在Chrome/IE9/Firebug中按F12,然后检查网络"部分以查看它.

You can track it with the webbrowser's builtin/addon developer toolset. Press F12 in Chrome/IE9/Firebug and check the "Network" section to see it.

JSF导航处理程序不发送重定向.相反,它使用目标页面的内容作为HTTP响应.

The JSF navigationhandler doesn't send a redirect. Instead, it uses the content of the target page as HTTP response.

  • 客户端向somepage.xhtml发送HTTP请求.
  • 服务器发送回HTTP响应,内容为newpage.xhtml.
  • Client sends a HTTP request to somepage.xhtml.
  • Server sends a HTTP response back with content of newpage.xhtml.

但是,由于原始HTTP请求是somepage.xhtml,因此浏览器地址栏中的URL保持不变.如果您熟悉基本Servlet API ,那么您应该了解这与

However as the original HTTP request was to somepage.xhtml, the URL in browser address bar remains unchanged. If you are familiar with the basic Servlet API, then you should understand that this has the same effect as RequestDispatcher#forward().

关于是否从JSF引擎盖下拉HttpServletResponse并调用sendRedirect()是否正确;不,那不是正确的用法.您的服务器日志将被IllegalStateException弄乱,因为这样您就不会告诉JSF您已经接管了响应处理的控制,因此JSF不应执行其默认响应处理工作.实际上,您应该执行 FacesContext#responseComplete() 之后.

As to whether pulling the HttpServletResponse from under the JSF hoods and calling sendRedirect() on it is the proper usage; no, that isn't the proper usage. Your server logs will get cluttered with IllegalStateExceptions because this way you aren't telling JSF that you've already taken over the control of the response handling and thus JSF shouldn't do its default response handling job. You should in fact be executing FacesContext#responseComplete() afterwards.

此外,每当您每次需要从JSF工件(例如托管bean)中的javax.servlet.*包中导入某些内容时,都应该绝对停止编写代码,并三思而后行,是否真的以正确的方式做事,并问自己是否存在对于您要实现的目标和/或任务是否真正属于JSF托管Bean,这还不是标准JSF方式"(在某些情况下,简单的

Also, everytime whenever you need to import something from javax.servlet.* package in a JSF artifact like a managed bean, you should absolutely stop writing code and think twice if you're really doing things the right way and ask yourself if there isn't already a "standard JSF way" for whatever you're trying to achieve and/or if the task really belongs in a JSF managed bean (there are namely some cases wherein a simple servlet filter would have been a better place).

在JSF中执行重定向的正确方法是在操作结果中使用faces-redirect=true查询字符串:

The proper way of performing a redirect in JSF is using faces-redirect=true query string in the action outcome:

public String submit() {
    // ...
    return "/newpage.xhtml?faces-redirect=true";
}

或使用

Or using ExternalContext#redirect() when you're not inside an action method such as an ajax or prerender listener method:

public void listener() throws IOException {
    // ...
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.redirect(ec.getRequestContextPath() + "/newpage.xhtml");
}

(是的,您不需要在IOException上加上try-catch,只需让异常通过throws,servlet容器便会处理它)

(yes, you do not need to put a try-catch around it on IOException, just let the exception go through throws, the servletcontainer will handle it)

或使用

关于导航处理程序对纯HTML"文件失败的原因,这仅仅是因为导航处理程序只能处理JSF视图,而不能处理其他文件.您应该先使用ExternalContext#redirect().

As to why the navigation handler fails for "plain HTML" files, that is simply because the navigation handler can process JSF views only, not other files. You should be using ExternalContext#redirect() then.

  • How to navigate in JSF? How to make URL reflect current page (and not previous one)
  • When should I use h:outputLink instead of h:commandLink?

这篇关于重定向和导航/转发之间的区别是什么?什么时候使用什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆