开发者

Java facelets dynamic loading and composite component attributes

Currently I'm trying to implement webpart technology with JavaServer Faces 2.0 with Facelets view technology for educational purposes. I have created facelet templates, facelet custom components and made a few facelet "template" clients. But in one thing I'm stuck. I can't dynamically load controls and put them cc:attributes.

If I have a page for example with static text or bind it with ManagedBean the property ui:include everything works well.

<ui:define name="right-column">
    right-column asd
    <h:link outcome="asdf" value="link_get">
        <f:param name="aa" value="123" />
        <f:param name="a" value="123 dd + 20" />
    </h:link>
    <h:commandLink action="asdf?faces-redirect=true" value="asdf">
        <f:param name="aa" value="123" />
    </h:commandLink><br />
    <ui:include src="./resources/Controls/CategoryTree.xhtml"/><br />
    This works even if I put src with MenageBean property.
    <ui:include src="#{browseProducts.incudePath}"/>
</ui:define>

Here is my facelet control (data binding is inside this control within #{TreeBean.root}:

<!-- NO INTERFACE -开发者_开发问答->
<cc:interface>
</cc:interface>

<!-- IMPLEMENTATION -->
<cc:implementation>
<!--    This is a very simply scaffolding for Category Three Control.
    -->
    <p:tree value="#{TreeBean.root}" var="node"
            expandAnim="FADE_IN" collapseAnim="FADE_OUT">
        <p:treeNode>
            <h:outputText value="#{node}" />
        </p:treeNode>
    </p:tree>

</cc:implementation>

But I have problem when ui:include points to a control with cc:attribute. I don't know how to initialize this attribute from backing bean and do "stuff".

For example I have this page:

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./resources/layout/Template.xhtml"
                xmlns:sc="http://java.sun.com/jsf/composite/Controls">

    <ui:define name="right-column">
        <sc:dummy ItemCount="10" />
        <ui:include src="./resources/Controls/dummy.xhtml" />
    </ui:define>
</ui:composition>

Here goes the composite control:

<cc:interface>
        <cc:attribute name="ItemCount" required="true"
                      shortDescription="This attribute is meaningful "  />
    </cc:interface>
    <!-- IMPLEMENTATION -->
    <cc:implementation>
    <!-- How to pass ItemCount to my dummy bean to create so many items in
    list as ItemCount value -->
        <ui:repeat value="#{dummy.dummyList}" var="dummyItem">
            <h:outputText value="#{dummyItem}" />
        </ui:repeat>
    </cc:implementation>

And backing bean code:

public ArrayList<String> getDummyList() {
    //Here I try to get dummy list work.
    dummyList = new ArrayList<String>(getDummyCount());
    for (int i=0;i< getDummyCount();i++){
        dummyList.add(i + "" + i);
    }
    return dummyList;
}

How can this be done?


I think you have two problems:

  1. calling a method with parameter from a composite component
  2. specifying some parameter to an included page

For 1., since jsf 2, you could call the method directly, specifying the parameter (which should be part of method signature):

        <ui:repeat value="#{dummy.getDummyList(cc.attrs.dummyCode)}" var="dummyItem">
            <h:outputText value="#{dummyItem}" />
        </ui:repeat>

But I suspect you are trying to use a backing for something that it isn't designed for. Maybe you'll be interested in writing backing java code for your composite component, which is different. It's difficult to master if you are a beginner, though. I'd first try to design my pages and bean interactions differently. I don't know which problem you are trying to solve, but at first look, this solution looks too complicated.

For 2., you should have a look at ui:param. A quick google search gives me this: http://www.jsfcentral.com/articles/facelets_3.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜