Primefaces将显示多个动态内容面板 [英] Primefaces multiple dynamic content panels to be displayed

查看:88
本文介绍了Primefaces将显示多个动态内容面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Web应用程序中创建一种简单的可自定义新闻查看页面,例如 Google的但更简单,带有框架.每个框架对象仅具有其标题和要添加为其内容的网址.

I'm trying to do a kind of simple customizable news view page into my web application, like Google's one but much simpler, with frames. Each frame object simply has his title and an url to be added as its content.

我正在使用最新版本的 JSF和primefaces .因此,我的后备bean(@ViewScoped)可以访问存储在@SessionScoped bean中的登录用户,并且该用户已加载了相应的帧.

I'm using JSF and primefaces, both on their newest versions. So my backing bean, which is @ViewScoped, has access to the logged user, which is stored in a @SessionScoped bean, and that user has his corresponding frames loaded.

问题,因为我发现唯一的方法是使用c:forEach标记.我就是这样的:

Problem comes when I try to iterate it over the @ViewScoped bean, because the only way I find to do it is with a c:forEach tag. That's the way I do:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui"
xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<h:form>
    <p:panel header="Noticias">
        <h:panelGrid columns="#{fn:length(navegableHomeManager._UserFrames)}"
            width="100%">
            <c:forEach var="frame" items="#{navegableHomeManager._UserFrames}">
                <p:column>
                    <p:panel header="#{frame._Name}" closable="true"
                        style="width:95%;height:500px;" id="#{frame._Name}">
                        <p:ajax event="close"
                            listener="#{navegableHomeManager.actionFrameClosed}" />
                        <ui:include src="#{frame._Path}" />
                    </p:panel>
                </p:column>
            </c:forEach>
        </h:panelGrid>
    </p:panel>
</h:form>

该迭代显然不适用于navegableHomeManager bean,因为它是@ViewScoped.因此,将在每次迭代中重新构建Bean.我所达到的解决方案在navegableHomeManagerloggedBean之间使用了另一个@SessionScoped bean,这样框架就可以在那里存储了,并且我可以在迭代中正确访问它们.上面的代码就是这样.

That iteration obviusly does not work with navegableHomeManager bean because it is @ViewScoped. So the bean will be rebuilt in each of the iterations. The solution I've reached uses another @SessionScoped bean between the navegableHomeManager and the loggedBean, so that way the frames are stored there and I can have access to them properly into the iteration. That's working with the code above.

但是,我认为每次以这种方式进行迭代时,都不必强制使用@SessionScoped bean(为每种情况创建一个特定的bean). 这就是为什么我尝试使用组件来避免迭代的原因.

However I don't think it should be compulsory to use a @SessionScoped bean (creating an specific bean for every single case) every time I want to iterate in that way. That's why I have tried using components to avoid the iteration.

<p:dataGrid columns="#{fn:length(navegableHomeManager._UserFrames)}"
            value="#{navegableHomeManager._UserFrames}" var="frame">
    <p:column>
        <p:panel header="#{frame._Name}" closable="true"
                    style="width:95%;height:500px;">
            <p:ajax event="close"
                        listener="#{navegableHomeManager.actionFrameClosed}" />
            <ui:include src="#{frame._Path}" />
        </p:panel>
    </p:column>
</p:dataGrid>

当bean是@ViewScoped@SessionScoped时,这都不起作用,因为即使设置了frames属性,ui:include标记也已经构建,没有目标路径,所以我无法渲染目标路径是动态的.我认为,由于ui:includec:forEach同时应用,因此使用c:forEach标记确实是解决此问题的唯一方法.

It doesn't work neither when the bean is @ViewScoped or @SessionScoped, because even the frames properties are set, the ui:include tag has already been built with no destination path, so I can't render the destination path dinamically. I think that, as ui:include is being applied at the same time as c:forEach, using a c:forEach tag is really the only way to go through this.

汇集您的想法.

更新

在这里,我发布了更多的 xhtml 代码以帮助理解上下文.新页面被集成到模板中.这是目标页面(/system/home/index.xhtml ):

Here I post more xhtml code to help in the understanding of the context. The new's page is integrated into a template. That's the page which is targeted (/system/home/index.xhtml):

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
template="/templates/general_template.xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">

<ui:define name="metadata">
    <f:event type="preRenderView"
        listener="#{navegableHomeManager.initialize}" />
</ui:define>

<ui:define name="general_content">
    <ui:include src="/system/home/home_view.xhtml" />
</ui:define>

这就是我也尝试这样做的方式,但是没有使c:forEach标签起作用:

And that's the way I also tried to do it, but not making the c:forEach tag to work:

<context-param>
    <param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>
    <param-value>/system/home/index.xhtml</param-value>
</context-param>

这就是支持bean的头文件:

And that's the backing bean header:

@ManagedBean
@ViewScoped
@URLMapping(id = "home", pattern = "/home", viewId = "/system/home/index.xhtml")
public class NavegableHomeManager extends SystemNavegable {

/**
 * 
 */
private static final long serialVersionUID = 6239319842919211716L;

@ManagedProperty(value = "#{loggedBean}")
private LoggedBean _LoggedBean;

//More stuff

推荐答案

粘贴到<c:forEach>.它可以完成您要找的工作. <ui:include>在视图构建期间运行,因此,迭代标记也应在视图构建期间运行.

Stick to <c:forEach>. It does the job you're looking for. The <ui:include> runs during view build time, so the iteration tag should also run during view build time.

关于@ViewScoped bean问题,您有2个选择:

As to the @ViewScoped bean problem, you've 2 options:

  1. 关闭特定视图的部分状态保存:

  1. Turn off partial state saving for the particular view:

<context-param>
    <param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>
    <param-value>/news.xhtml</param-value>
</context-param>

  • 将其更改为@RequestScoped bean,并使用<f:param>@ManagedProperty通过请求参数来维护回发状态.

  • Change it to a @RequestScoped bean and use <f:param> and @ManagedProperty to maintain state across postbacks by request parameters.

    如果可以使用JSF 2.2,那么还有第三种选择:只需升级到JSF 2.2.他们已经修复了视图范围内的豆子和部分状态保存中的鸡蛋问题.

    There would be a third option if JSF 2.2 is available: just upgrade to JSF 2.2. They've fixed the chicken-egg problem in view scoped beans and partial state saving.

    这篇关于Primefaces将显示多个动态内容面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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