Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 2 视图中复合构件的多个实例?_Jsf 2_Composite Component - Fatal编程技术网

Jsf 2 视图中复合构件的多个实例?

Jsf 2 视图中复合构件的多个实例?,jsf-2,composite-component,Jsf 2,Composite Component,我读过许多类似的问题,但我不知道如何解决我的问题: 我编写了一个由视图范围的、自包含的托管bean支持的复合组件。 该组件由一个自动完成文本框和一个打开对话框的按钮组成。用户可以通过名称自动完成或在树对话框中选择节点来选择项目。 支持bean实现了数据访问、树逻辑等所需的所有内容,并应将所选项作为POJO公开 现在我有两个问题: 由于树管理的复杂性,selectedObj属性由getter和setter访问,getter和setter在bean中执行一些操作:它们不仅仅是访问类字段。现在我将整个

我读过许多类似的问题,但我不知道如何解决我的问题:

我编写了一个由视图范围的、自包含的托管bean支持的复合组件。 该组件由一个自动完成文本框和一个打开对话框的按钮组成。用户可以通过名称自动完成或在树对话框中选择节点来选择项目。 支持bean实现了数据访问、树逻辑等所需的所有内容,并应将所选项作为POJO公开

现在我有两个问题:

由于树管理的复杂性,selectedObj属性由getter和setter访问,getter和setter在bean中执行一些操作:它们不仅仅是访问类字段。现在我将整个bean作为属性传递。如何使bean的selectedObj成为复合组件的value属性

如何在同一视图中使用组件的多个实例

以下是该组件的一个示例:

<cc:interface>
    <cc:attribute name="bean" type="com.yankee.OUTreeBean" required="true"/>
    <cc:attribute name="listener" method-signature="void listener()"/>
</cc:interface>
<cc:implementation>
    <p:dialog id="#{cc.id}_dialog" widgetVar="_dlg" header="Select OU" modal="true" dynamic="true" >
        <p:toolbar>
            <!-- some buttons to refresh, expand, collapse etc. -->
        </p:toolbar>
        <p:tree id="#{cc.id}_tree" value="#{cc.attrs.bean.root}" var="node"
                selectionMode="single"
                selection="#{cc.attrs.bean.selectedNode}">
            <p:ajax event="select" update="@form" listener="#{cc.attrs.listener}" oncomplete="if (!args.validationFailed) _dlg.hide()" />
            <p:treeNode>  
                <h:outputText value="#{node.OU_NAME}" />  
            </p:treeNode>  
        </p:tree>
    </p:dialog>

    <p:autoComplete id="#{cc.id}_inner" value="#{cc.attrs.bean.selectedObj}" completeMethod="#{cc.attrs.bean.completeObj}"  
                    var="obj" itemLabel="#{obj.OU_NAME}" itemValue="#{obj}" 
                    forceSelection="true" 
                    converter="ouConverter"
                    multiple="false" 
                    minQueryLength="2">
        <p:ajax event="itemSelect" listener="#{cc.attrs.listener}" update="@form"/>
    </p:autoComplete>
    <div style="float: right">
        <p:commandButton id="bSearch" icon="ui-icon-search" onclick="_dlg.show()"/>
    </div>
</cc:implementation>

几天前我也有同样的问题。就像你一样,我使用ManagedBean作为一个应该完成这项工作的对象。过了一段时间,我发现我应该创建FacesComponent。我是JSF新手,所以找到解决方案不是那么容易,但它解决了我所有的问题。这就是它的工作原理:

view.xhtml

<h:body>
    <cc:interface componentType="playerComponent">
        <cc:attribute name="playerId" required="true"/>
    </cc:interface>
    <cc:implementation>
        <c:set var="inplaceId" value="inplace-#{cc.attrs.playerId}" />
        <c:set var="outputId" value="output-#{cc.attrs.playerId}" />
        <h:form id="form-#{cc.attrs.playerId}">
            <p:inplace editor="false" widgetVar="#{inplaceId}">
                <h:inputText value="#{cc.player.name}" id="outputId"/>
                <p:commandButton onclick="#{inplaceId}.save()" action="#{cc.save}" update="#{outputId}" value="save" />
                <p:commandButton onclick="#{inplaceId}.cancel()" update="#{outputId}" value="cancel"  />
            </p:inplace>
        </h:form>
    </cc:implementation>
</h:body>
Player.java实体

public class Player {
    private name;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

正如我写的,我是JSF新手,可能应该在构造函数中使用@PostConstruct以不同的方式创建播放器对象?但是这是可行的。

如果有正确的getter和setter@Christophe好的,谢谢,但是目的是从页面的bean中隐藏组件的支持bean。MyBean应该不知道OUTreeBean的存在,因此组件应该绑定到MyBean的类型为OUP的属性,在MyBean中提供一些getter和setter,允许直接访问对象,然后通过接口实现强制执行此操作…谢谢pepuch。我还尝试将@ManagedBean转换为@FacesComponent,但我不知道如何将组件值绑定到参数,例如,如何使用@yankee,我刚刚发现自定义组件具有请求范围,甚至使用状态助手see BalusC的答案来模拟@ViewScope对我来说太复杂了,因为会有一些对象需要重新创建。相反,这个方法对于问题2似乎很有效。我仍然不明白你的方法是什么?我的问题1;-是一个内部有OU的树节点,已经是OU了。因此,我需要特定的Getter/Setter来访问选定的OUSEE OUTreeBean代码。它们不能成为使用bean MyBean的一部分:它们必须在OUTreeBean中。但是我不想像我在这里所做的那样向用户公开整个OUTreeBean。相反,我必须找到一种方法,将选定的OU作为属性公开绑定
@ManagedBean
@ViewScoped
public class MyBean implements Serializable {

    private static final long serialVersionUID = 1L;
    @ManagedProperty("#{oUTreeBean}")
    private OUTreeBean ouFilterBean;

    public void onOUChange() throws SQLException {
        // Blah blah
    }
}
<h:body>
    <cc:interface componentType="playerComponent">
        <cc:attribute name="playerId" required="true"/>
    </cc:interface>
    <cc:implementation>
        <c:set var="inplaceId" value="inplace-#{cc.attrs.playerId}" />
        <c:set var="outputId" value="output-#{cc.attrs.playerId}" />
        <h:form id="form-#{cc.attrs.playerId}">
            <p:inplace editor="false" widgetVar="#{inplaceId}">
                <h:inputText value="#{cc.player.name}" id="outputId"/>
                <p:commandButton onclick="#{inplaceId}.save()" action="#{cc.save}" update="#{outputId}" value="save" />
                <p:commandButton onclick="#{inplaceId}.cancel()" update="#{outputId}" value="cancel"  />
            </p:inplace>
        </h:form>
    </cc:implementation>
</h:body>
@FacesComponent("playerComponent")
public class PlayerComponent extends UINamingContainer {

    private Player player;

    private void init() {
        Object idObj = getAttributes().get("playerId");
        if (idObj != null) {
            // create player object
        }
    }

    public void save() {
        // save player object
    }

    public Player getPlayer() {
        if (player == null) {
            init();
        }
        return player
    }
}
public class Player {
    private name;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}