动态 <ui:include src>取决于 &lt;ui:repeat var&gt;不包括任何东西 [英] Dynamic &lt;ui:include src&gt; depending on &lt;ui:repeat var&gt; doesn&#39;t include anything

查看:15
本文介绍了动态 <ui:include src>取决于 &lt;ui:repeat var&gt;不包括任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JSF 的新手,我正在努力动态呈现包含的页面.我的代码如下所示:

菜单豆

@ViewScoped公共类 MenuBean 实现了 Serializable {私人菜单项[] 菜单项 = 新菜单项[] {new MenuItem("page_1", "/page_1.xhtml"),new MenuItem("page_2", "/page_2.xhtml"),};私人字符串 selectedItemLabel;//...}

菜单项

public class MenuItem 实现了 Serializable {私有字符串标签;私人字符串页面;//...}

index.xhtml

 <h:panelGroup 渲染="#{menuBean.selectedItemLabel eq menuItem.label}" layout="block"><h:outputText value="#{menuBean.selectedItemLabel}"/><ui:include src="#{menuItem.page}"/></h:panelGroup></ui:repeat>

结果是渲染了 2 个按钮.每当我单击任何按钮时,条件呈现的 panelGroup 内的标签都会出现,但包含的页面没有.如果我从第一个 ui:repeat 更改 'menuItem1' var,它会起作用,但它确实是不可预测的.例如,如果我将 setSelectedItemLabel 参数硬编码为page_1",那么当我点击 button_1 page_1 时会显示,但即​​使我点击 button_2 page_2 (!?) 也会显示...

解决方案

您正面临视图构建时间与视图渲染时间的问题.这基本上是相同的问题,在 JSTL in JSF2 Facelets 中已经详细回答...有道理吗? 在那个答案中,替换JSTL"使用ui:include".就此而言, 在视图渲染期间运行,而 之前已经在视图构建期间运行.>

您会明白,唯一的解决方案是将 替换为在视图构建期间运行的另一个标记,例如 JSTL ..

请注意,我不保证它会解决您想到的具体功能需求.使用 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>取决于 &lt;ui:repeat var&gt;不包括任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆