【发布时间】:2010-09-16 13:26:38
【问题描述】:
考虑一个页面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>
...
按钮动作绑定到控制器方法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"
}
}
我的第一个问题是:上面说的有意义吗?这是使用 redirect() 的正确方法吗?
如果myCondition 为真,我想将用户重定向到http://www.myTarget.com/thePage.html。如果myCondition 为假,他将不得不留在myPage.xhtml。
如果是这样,我想更好地了解会发生什么...在 Firefox 中使用 Live HTTP Headers 时,我观察到单击按钮时会发生这种情况
- 一个 POST 到 webapp/myPage.xhtml 发生
- 服务器回复 302 已临时移动 - 位置:www.myTarget.com/thePage.html
- 浏览器 GET 的 www.myTarget.com/thePage.html
到目前为止,这是预期的行为吗?
我现在觉得烦人的是 webapp/myPage.xhtml 被调用了。更具体地说,这里再次调用了 preRenderView 事件。 (我在 preRenderView-listener 中有一些代码应该只执行一次。)
上面说的有道理吗?有没有人看到改进的方法?
谢谢! J.
【问题讨论】: