Java Facelets 动态加载和复合组件属性

发布于 2024-10-15 03:14:57 字数 2936 浏览 2 评论 0原文

目前,我正在尝试使用 JavaServer Faces 2.0 和 Facelets 视图技术来实现 Webpart 技术,以用于教育目的。我创建了facelet 模板、facelet 自定义组件并制作了一些facelet“模板”客户端。但在一件事上我陷入了困境。我无法动态加载控件并将它们放入 cc:attributes

例如,如果我有一个包含静态文本的页面或将其与 ManagedBean 属性 ui:include 绑定,则一切正常。

<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>

这是我的 Facelet 控件(数据绑定位于 #{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>

但是当 ui:include 指向带有 的控件时,我遇到问题cc:attribute。我不知道如何从支持 bean 初始化此属性并执行“操作”,

例如我有这个页面:

<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>

这是复合控件:

<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>

以及支持 bean 代码:

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;
}

如何做到这一点。 ?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗心人 2024-10-22 03:14:57

我认为你有两个问题:

  1. 从复合组件调用带有参数的方法,
  2. 指定包含页面的某些参数

对于1.,自jsf 2以来,你可以直接调用该方法,指定参数(应该是方法签名的一部分) :

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

但我怀疑您正在尝试使用并非设计用途的支撑物。也许您会对为复合组件编写支持 Java 代码感兴趣,这是不同的。但是,如果您是初学者,则很难掌握。我首先尝试以不同的方式设计我的页面和 Bean 交互。我不知道你想解决哪个问题,但乍一看,这个解决方案看起来太复杂了。

对于 2.,您应该查看 ui:param。快速谷歌搜索给了我这个: http://www.jsfcentral.com/articles/facelets_3.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文