我目前正在嘗試在ajax請求期間動態地將新組件添加到JSF組件樹。如何在Ajax請求期間動態添加JSF2中的組件
實際上,我在AjaxBehaviorListener中的UIViewRoot組件中添加了一個子組件,該組件在ajax請求過程中在服務器端觸發。
問題在於新組件未呈現。看起來這個組件在渲染響應階段沒有考慮到。
你能幫我解決這個問題嗎?
問候,
紀堯姆
我目前正在嘗試在ajax請求期間動態地將新組件添加到JSF組件樹。如何在Ajax請求期間動態添加JSF2中的組件
實際上,我在AjaxBehaviorListener中的UIViewRoot組件中添加了一個子組件,該組件在ajax請求過程中在服務器端觸發。
問題在於新組件未呈現。看起來這個組件在渲染響應階段沒有考慮到。
你能幫我解決這個問題嗎?
問候,
紀堯姆
,而不是試圖Ajax請求時動態添加組件,嘗試定義組件的前期,並設置它的渲染標籤爲false。然後可以使用ajax請求填充組件的內容,並翻轉呈現的屬性以顯示屬性。
此解決方案在ajax請求添加組件之前知道的情況下工作。 但是,如果您無法知道要添加哪個組件,則不起作用。
我也許找到了一個解決方案:
我的解決方案是實現我的自定義PartialViewContext和使用方法startInsertAfter或PartialResponseWriter的startInsertBefore。
它正在工作,但您必須將組件添加爲瞬態。 (uiComponent.setTransient(Boolean.TRUE);)
問候,
紀堯姆
這僅僅是一個例子!你可以用很多方式實現'addOutputText()'。例如,你可以傳遞一些擴展類似'AbstractComponentFactory'的對象。這個工廠比創建某種'UIComponent'('public UIComponent AbstractComponentFactory.createComponent()')。我希望這是可以理解的。只需要一些想象力。 – 2011-09-26 13:30:35
這個工作對我來說:要在其下動態地添加其他的UIComponents應該
豆拿着結合爲UIComponent請求範圍,否則它可以拋出一些討厭的例外(不要問我爲什麼):
@ManagedBean
@RequestScoped
public class AddressEditorBean {
// session bean reference for holding id number line and model
@ManagedProperty(value = "#{addressValueBean}")
private AddressValueBean address;
public String addOutputText() {
HtmlOutputText text = new HtmlOutputText();
int c = address.getC();
text.setValue("new text! " + c);
text.setId("id" + c++);
address.setC(c); // hold id number line in sessionbean
address.getComps().add(text); // hold new uicomponent in session bean to be able to rebuild it
panel.getChildren().clear(); // need to clear children and add them all again,
panel.getChildren().addAll(address.getComps()); // otherwise there are problems with duplicate children (bug?)
return "success";
}
public HtmlPanelGroup getPanel() {
return panel;
}
public void setPanel(HtmlPanelGroup pane) {
if (panel == null) {
this.panel = pane;
if (panel != null) {
panel.getChildren().addAll(address.getComps());
}
} else {
this.panel = pane;
}
}
}
從頁面的代碼段。我dynnamically組件添加到<h:panelGroup>
<h:form>
<h:panelGroup id="section" binding="#{addressEditorBean.panel}">
</h:panelGroup>
<h:commandButton value="add new text" action="#{addressEditorBean.addOutputText}">
<f:ajax execute="@this" render="section" event="action"/>
</h:commandButton>
</h:form>
在會話bean我認爲實際的動態模型,這樣我可以重新加載頁面後重建它:
@ManagedBean
@SessionScoped
public class AddressValueBean extends ValueBean<Address> {
private int c = 0;
private List<UIComponent> comps = new ArrayList<UIComponent>();
public AddressValueBean() {
setValue(new Address());
}
public int getC() {
return c;
}
public void setC(int cn) {
this.c = cn;
}
public List<UIComponent> getComps() {
return comps;
}
public void setComps(List<UIComponent> comps) {
this.comps = comps;
}
}
其實,你將如何改變組件的屬性(或者某些屬性)爲了在AJAX請求期間呈現它?你會使用支持豆嗎? – 2010-08-18 09:02:32
你只是想讓一些EL的評估從false更改爲true。這可能是一個支持bean的布爾屬性,或任何任意複雜的位(表達式語言) – 2010-09-07 03:55:59