Primefaces以编程方式创建FeedReader

时间:2013-10-20 18:28:11

标签: jsf primefaces

我正在设计一个带有程序控制面板的仪表板(如iGoogle)。到现在为止还挺好。在某些面板中,我想显示Feed,因此我使用p:feedReader。

如果我创建一个带有内置馈送器的面板的页面,它可以正常工作。所以我正在使用正确的库。

当我尝试以编程方式创建Feedreader时,它不会显示Feed标题和内容。

我的测试页:

<!DOCTYPE html>
<html xmlns="http://www.w3c.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:p="http://primefaces.org/ui">
<f:view contentType="text/html">
    <h:head>
        <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
        <meta http-equiv="refresh" content="600" />
        <link type="text/css" rel="stylesheet" href="styles/app.css" />
        <title>Test Panel</title>
    </h:head>
    <h:body>
        <f:event type="preRenderView" listener="#{panelBean.Init}" />
        <h:form id="frmPortal">

            <p:growl id="growl" showDetail="true" />

            <p:panel id="head" style="width: 1900; height: 100px; border: none; background: transparent;">

            </p:panel>

        <p:commandButton id="Test" value="Test" action="#{panelBean.Test}" style="margin-left: 20px;" />
        <p:commandButton id="Update" value="Update" action="#{panelBean.Test}" style="margin-left: 20px;" update="Feed1"/>

        </h:form>
    </h:body>
</f:view>
</html>

我的豆子:

@ManagedBean(name="panelBean")
@ViewScoped
public class PanelBean implements Serializable {
    private static final long serialVersionUID = 403660770777505739L;
    private boolean renderPanel;
    private FeedReader feedReader;

    public void Init() {
        UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
        UIComponent component = viewRoot.findComponent("frmPortal");

        if (component == null) {
            System.out.println("Component is null");
        } else {
            System.out.println("Component is NOT null");
            Panel p = new Panel();
            p.setClosable(true);
            p.setHeader("Test");
            p.setId("Panel1");
            p.setVisible(true);

            feedReader = new FeedReader();
            feedReader.setId("Feed1");
            feedReader.setRendered(true);
            feedReader.setSize(5);
            feedReader.setValue("http://barrapunto.com/barrapunto.rss");
            feedReader.setVar("feed");

            HtmlOutputText outPutText = new HtmlOutputText();
            outPutText.setValue("#{feed.title}");
            p.getChildren().add(feedReader);            

            component.getChildren().add(p);

            RequestContext context = RequestContext.getCurrentInstance();  
            context.update("frmPortal");
        }
        renderPanel = true;
    }

    public boolean isRenderPanel() {
        return renderPanel;
    }

    public void setRenderPanel(boolean renderPanel) {
        this.renderPanel = renderPanel;
    }

    public void Test() {
        UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
        UIComponent component = viewRoot.findComponent("frmPortal");

        if (component == null) {
             System.out.println("Component is null");
        } else {

             System.out.println(component.getChildren().get(0).getId());
             System.out.println(component.getChildren().get(1).getId());
             System.out.println(component.getChildren().get(2).getId());
             System.out.println(component.getChildren().get(3).getId());
             System.out.println(component.getChildren().get(4).getId());
             Panel panel = (Panel) component.getChildren().get(4);

             FeedReader feed = (FeedReader) panel.getChildren().get(0);
             if (feed == null) {
                  System.out.println("FeedReader is null");
             } else {
                  System.out.println(feed.getId() + " - Size: " + feed.getSize() + " - Value: " + feed.getValue() + " - Var: " + feed.getVar());
             }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你可以尝试这段代码:

<h:form id="frmPortal">

    <p:panelGrid id="panelGrid" columns="1">
     <p:growl id="growl" showDetail="true" />

    </p:panelGrid>
    <p:commandButton id="Test" value="Test" action="#{panelBean.Test}" style="margin-left: 20px;" />
    <p:commandButton id="Update" value="Update" action="#{panelBean.Test}" style="margin-left: 20px;" update="Feed1"/>

</h:form>
通过@Augusto代码扩展了支持bean:

public void Init(){
    UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
    UIComponent component = viewRoot.findComponent("frmPortal");

    if (component != null) {
        p = new Panel();
        p.setClosable(true);
        p.setHeader("Test");
        p.setId("Panel1");
        p.setVisible(true);

        feedReader = new FeedReader();
        feedReader.setId("Feed1");
        feedReader.setRendered(true);
        feedReader.setSize(5);
        feedReader.setValue("http://barrapunto.com/barrapunto.rss");
        feedReader.setVar("feed");  

        FacesContext context = FacesContext.getCurrentInstance();
        ELContext elContext = context.getELContext();

        HtmlOutputText outputText = new HtmlOutputText();
        outputText.setId("Header");
        ValueExpression valueExpression = context.getApplication()
        .getExpressionFactory()
        .createValueExpression(elContext, "#{feed.title}", Object.class);
        outputText.setValueExpression("value", valueExpression);
        p.getChildren().add(feedReader);            
        component.getChildren().add(p);

        RequestContext reqContext = RequestContext.getCurrentInstance();  

        reqContext.update("Feed1");
    }
}

如果无效,请尝试不使用反馈阅读器。同时为什么只使用一个饲料阅读器和多个面板?

答案 1 :(得分:0)

OP解决方案。

谢谢大家,特别感谢Herry。我找到了答案。事情就这样了:

public void Init() {

    UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
    UIComponent component = viewRoot.findComponent("frmPortal");

    if (component == null) {
        System.out.println("Component is null");
    } else {
        System.out.println("Component is NOT null");
        p = new Panel();
        p.setClosable(true);
        p.setHeader("Test");
        p.setId("Panel1");
        p.setVisible(true);

        feedReader = new FeedReader();
        feedReader.setId("Feed1");
        feedReader.setRendered(true);
        feedReader.setSize(5);
        feedReader.setValue("http://barrapunto.com/barrapunto.rss");
        feedReader.setVar("feed");

        FacesContext context = FacesContext.getCurrentInstance();
        ELContext elContext = context.getELContext();

        HtmlOutputText outputText = new HtmlOutputText();
        outputText.setId("Header");
        ValueExpression valueExpression = context.getApplication()
                .getExpressionFactory()
                .createValueExpression(elContext, "#{feed.title}", Object.class);
        outputText.setValueExpression("value", valueExpression);
//          outputText.setValue("#{feed.title}");
        feedReader.getChildren().add(outputText);

        p.getChildren().add(feedReader);            

        component.getChildren().add(p);

        RequestContext reqContext = RequestContext.getCurrentInstance();  

        reqContext.update("Feed1");
    }

    renderPanel = true;
}