java 如何在 JSF2.0 中使用 ExternalContext.redirect()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3727110/
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
How to use ExternalContext.redirect() in JSF2.0?
提问by Jan
Consider a page webapp/myPage.xhtml:
考虑一个页面webapp/myPage.xhtml:
...
<h:form id="myForm">
...
<h:selectOneMenu id="..." required="true" value="#{myController.aValue}">
<f:selectItems value="#{...}" var="..." itemValue="#{...}" itemLabel="#{...}"/>
</h:selectOneMenu>
...
<h:commandButton value="Go for it!" action="#{myController.goForIt(...)}"/>
...
</h:form>
...
The button action is bound to a controller method MyController.goForIt():
按钮操作绑定到控制器方法MyController.goForIt():
@ManagedBean(name = "myController")
@RequestScoped
public class MyController {
public String goForIt(...){
if (myCondition){
try {
FacesContext.getCurrentInstance().getExternalContext()
.redirect("http://www.myTarget.com/thePage.html");
} catch (IOException e) {
...
}
}
return "myPage.xhtml"
}
}
My first question is: Does the above makes sense? Is this a correct way of using redirect()?
我的第一个问题是:以上是否合理?这是使用redirect() 的正确方法吗?
I want to redirect the user to http://www.myTarget.com/thePage.html
in case myCondition
is true. If myCondition
is false, he will have to stay on myPage.xhtml
.
我想将用户重定向到http://www.myTarget.com/thePage.html
以防万一myCondition
。如果myCondition
是假的,他将不得不留下来myPage.xhtml
。
If so, I would like to understand better what happens... When using Live HTTP Headersin Firefox, I observe that when clicking the button
如果是这样,我想更好地了解会发生什么......在 Firefox 中使用Live HTTP Headers时,我观察到单击按钮时
- a POST to webapp/myPage.xhtmloccurs
- the server replies with 302 Moved Temporarily - Location: www.myTarget.com/thePage.html
- the browser GET's www.myTarget.com/thePage.html
- POST 到 webapp/myPage.xhtml发生
- 服务器回复302 Moved Temporously - 位置:www.myTarget.com/thePage.html
- 浏览器 GET 的www.myTarget.com/thePage.html
So far, Is this the expected behaviour?
到目前为止,这是预期的行为吗?
What I find annoying now is that webapp/myPage.xhtmlis called. More specifically, that the preRenderView-event is called here again. (I have a bit of code in the preRenderView-listener that should be executed only once.)
我现在觉得烦人的是webapp/myPage.xhtml被调用。更具体地说,这里再次调用preRenderView事件。(我在 preRenderView-listener 中有一些代码应该只执行一次。)
Does the above make sense? Does anybody see a way to improve this?
上面说的有道理吗?有没有人看到改善这一点的方法?
Thank you! J.
谢谢!J。
回答by BalusC
So far, Is this the expected behaviour?
到目前为止,这是预期的行为吗?
A redirect basically instructs the client to fire a newHTTP request on the URL as specified in the Location
response header. So yes, the behaviour as you're seeing is correct.
重定向基本上指示客户端在响应标头中指定的 URL 上触发新的HTTP 请求Location
。所以是的,您所看到的行为是正确的。
However, your URL is wrong. It's relative to the current request URL. So if the current URL is for example http://example.com/context/page.jsf, then this redirect will basically point to http://example.com/context/www.myTarget.com/thePage.htmlwhich is obviously wrong. When the new URL concerns a different domain, you should redirect to an absoluteURL. In other words, prefix it with the http:// scheme.
但是,您的网址是错误的。它相对于当前的请求 URL。因此,如果当前的URL是例如http://example.com/context/page.jsf,那么,此重定向将基本指向http://example.com/context/www.myTarget.com/thePage.html这是显然是错误的。当新 URL 涉及不同的域时,您应该重定向到绝对URL。换句话说,在它前面加上 http:// 方案。
What I find annoying now is that webapp/myPage.xhtml is called.
我现在觉得烦人的是 webapp/myPage.xhtml 被调用。
This should in theory not happen when the redirect has taken place. Try adding return null
to the try
block.
这在理论上不应该在重定向发生时发生。尝试添加return null
到try
块中。