問題描述
我遇到了類似于 this 的導航案例問題.簡而言之,我正在嘗試使用 ajax 呈現(xiàn)的 h:commandLink
將導航從一個頁面重定向到另一個頁面.這是支持 bean
I'm stuck in a navigation case problem similar to this one.
In a few words, I'm trying to redirect navigation from one page to another, using an ajax rendered h:commandLink
.
Here's the backing bean
@ManagedBean
public class StartBean {
public void search(){
FacesContext
.getCurrentInstance()
.getExternalContext()
.getFlash()
.put("result", "hooray!")
;
}
public String showResult(){
return "result?faces-redirect=true";
}
}
和起始頁
<h:body>
<h:form prependId="false">
<h:commandButton value="Click" action="#{startBean.search}">
<f:ajax execute="@this" render="@form"/>
</h:commandButton>
<br/>
<h:commandLink
action="#{startBean.showResult()}"
rendered="#{flash.result != null}"
value="#{flash.result}"
/>
</h:form>
</h:body>
而 result
頁面只是顯示一條消息.兩個頁面都在 Web 模塊上下文根目錄中.碰巧 h:commandLink
在ajax提交后正確顯示,但單擊它會導致頁面刷新.它不會像預期的那樣重定向到 result
頁面.之后,如果頁面被重新加載(F5),result
頁面就會顯示出來.這似乎是一個渲染周期的問題.
whereas result
page is just showing a message. Both pages are on web module context root.
It happens that the h:commandLink
is correctly displayed after ajax submit, but clicking on it causes a page refresh. It doesn't redirect towards the result
page, as expected.
After it, if page is reloaded (F5), result
page is shown. It seems to be a rendering cycle matter.
有什么建議嗎?
提前致謝.
推薦答案
所有輸入和命令組件的 rendered
屬性在提交表單時重新評估.因此,如果它評估 false
,則 JSF 根本不會調用該操作.search()
方法的請求/響應完成后,F(xiàn)lash 作用域終止.當您發(fā)送 showResult()
的請求時,它不再存在于 Flash 范圍內.我建議將 bean 放在視圖范圍內,并將 rendered
屬性綁定到它的屬性.
The rendered
attribute of all input and command components is re-evaluated when the form is submitted. So if it evaluates false
, then JSF simply won't invoke the action. The Flash scope is terminated when the request/response of the search()
method is finished. It isn't there in the Flash scope anymore when you send the request of the showResult()
. I suggest to put the bean in the view scope and bind the rendered
attribute to its property instead.
@ManagedBean
@ViewScoped
public class StartBean {
private String result;
public void search(){
result = "hooray";
}
public String showResult(){
return "result?faces-redirect=true";
}
public String getResult() {
return result;
}
}
與
<h:commandLink
action="#{startBean.showResult}"
rendered="#{startBean.result != null}"
value="#{startBean.result}"
/>
另見:
- commandButton/commandLink/ajax action/listener 方法未調用或輸入值未更新
這篇關于JSF 2:ajax 調用后的頁面重定向的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!