[英]JSF 2 ViewScope questions
引用這篇好文章 ,
新的視圖范圍應該正好解決這些問題。 只要您一次又一次地將表單提交到同一視圖,@ ViewScoped bean就會存在。 換句話說,只要action方法返回null或甚至void,bean就會在下一個請求中出現。 導航到不同的視圖后,bean將被刪除。
我想到了這些問題:
index.xhtml
,並且如果我明確指定return "index";
或者<h:commandButton action="index.xhtml" ..>
,它基本上返回到相同的視圖,bean將再次重新創建..為什么? return "?faces-redirect=true"
或<h:commandButton action="?faces-redirect=true" ..>
,是的,我想跳過定義<h:commandButton action="?faces-redirect=true" ..>
的導航faces-config.xml
使用<redirect/>
。 如果我當前的視圖是index.xhtml,並且如果我明確指定返回“index”; 或者,基本上返回到相同的視圖,bean將再次重新創建..為什么?
如果明確指定結果(讀取:視圖),則將創建新視圖。 您必須從操作方法返回null或void(或者只是省略命令組件的action
屬性)。
我必須承認,我理解你的困惑,並且“視圖”一詞可以根據具體情況進行不同的解釋。 我想我遲早會修改鏈接文章中的措辭。
Viewscoped bean可以在重定向中存活嗎?
不可以。只有會話范圍的bean可以和flash范圍內的對象(一旦重定向完成就會立即結束,這可能是你在提出這個問題時想到的功能要求的實際需要)。
如果提供了javax.faces.ViewState,則視圖狀態可以繼續重定向。 我發現目前最簡單的解決方案是實現導航處理程序。
在faces-config.xml中:
<faces-config>
<application>
...
<navigation-handler>com.intersult.jsf.util.RedirectNavigationHandler</navigation-handler>
</application>
...
</faces-config>
Java類:
public class RedirectNavigationHandler extends ConfigurableNavigationHandler {
private NavigationHandler parent;
public RedirectNavigationHandler(NavigationHandler parent) {
this.parent = parent;
}
@Override
public void handleNavigation(FacesContext context, String from, String outcome) {
if (!context.getPartialViewContext().isPartialRequest()) {
if (outcome == null)
outcome = context.getViewRoot().getViewId();
if (!outcome.endsWith("?faces-redirect=true"))
outcome += "?faces-redirect=true";
String viewState =
context.getExternalContext().getRequestParameterMap().get(ResponseStateManager.VIEW_STATE_PARAM);
outcome += "&javax.faces.ViewState=" + viewState;
}
parent.handleNavigation(context, from, outcome);
}
}
這會產生一些影響:每個重定向都不再是一個獨立的請求。 它取決於視圖狀態,它不能被必然恢復。 所以有一個后備策略,如果它已過期,例如。 重定向視圖過期的異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.