问题描述
我是JSF的新手,我正在努力使用动态渲染包括页面。我的代码如下所示:MenuBean
@ViewScoped
public class MenuBean implements Serializable {
private MenuItem [] menuItems = new MenuItem [] {
new MenuItem(page_1,/page_1.xhtml),
新的MenuItem(page_2,/page_2.xhtml),
};
private String selectedItemLabel;
// ...
}
MenuItem
public class MenuItem implements Serializable {
private String label;
private String page;
// ...
}
index.xhtml
< ui:repeat var =menuItemvalue =#{menuBean.menuItems}>
< h:panelGroup rendered =#{menuBean.selectedItemLabel eq menuItem.label}layout =block>
< h:outputText value =#{menuBean.selectedItemLabel}/>
< ui:include src =#{menuItem.page}/>
< / h:panelGroup>
< / ui:repeat>
结果是2个按钮呈现。每当我点击任何按钮,条件显示面板组中的标签出现,但包含的页面不会。如果我从第一个ui更改'menuItem1'var:重复它的工作,但它是真的不可预测的。例如,如果我将setSelectedItemLabel参数硬编码为page_1,那么当我点击button_1 page_1时,即使我点击button_2 page_2(!?)被显示...
您正在面对视图构建时间vs查看渲染时间问题。这本质上是相同的问题,已经在在这个答案中,用ui:include替换JSTL。至此,< ui:repeat>
在视图渲染时间运行,而< ui:include>
已经在视图构建时间之前运行。
你会明白,唯一的解决方案是替换< ui:repeat> / code>另一个在视图构建时运行的标签,例如JSTL
< c:forEach>
。
请注意,我不保证它会解决您所考虑的具体功能需求。使用JSTL可能会有不良的副作用(这是可解释的,可理解的和可解决的)。
另请参见:
I am new to JSF and I am struggling with dynamicaly rendering included pages. My code looks like this:
MenuBean
@ViewScoped
public class MenuBean implements Serializable {
private MenuItem[] menuItems = new MenuItem[] {
new MenuItem("page_1", "/page_1.xhtml"),
new MenuItem("page_2", "/page_2.xhtml"),
};
private String selectedItemLabel;
//...
}
MenuItem
public class MenuItem implements Serializable {
private String label;
private String page;
//...
}
index.xhtml
<ui:repeat var="menuItem" value="#{menuBean.menuItems}">
<h:panelGroup rendered="#{menuBean.selectedItemLabel eq menuItem.label}" layout="block">
<h:outputText value="#{menuBean.selectedItemLabel}" />
<ui:include src="#{menuItem.page}" />
</h:panelGroup>
</ui:repeat>
The result is 2 buttons rendered. Whenever I click any button the label inside conditionaly rendered panelGroup appears but included page doesn't. If I change 'menuItem1' var from first ui:repeat it works but it is really unpredictable. For example if I hardcode setSelectedItemLabel parameter to 'page_1' then when I click to button_1 page_1 is displayed but even if I click to button_2 page_2 (!?) is displayed...
You're facing a view build time vs view render time problem. This is essentially the same problem which is already answered in detail in JSTL in JSF2 Facelets... makes sense? In that answer, replace "JSTL" with "ui:include". To the point, the <ui:repeat>
runs during view render time, while the <ui:include>
has already run during view build time before.
You'll understand that the only solution is to replace the <ui:repeat>
by another tag which runs during view build time, such as JSTL <c:forEach>
.
Note that I don't guarantee that it will solve the concrete functional requirement which you've in mind. Using JSTL may have undesirable "side effects" (which are however explainable, understandable and workaroundable).
See also:
这篇关于动态< ui:include src>取决于< ui:repeat var>不包括任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!