Java 在备份组件中使用@ManagedProperty

Java 在备份组件中使用@ManagedProperty,java,jsf,dependency-injection,jsf-2.2,composite-component,Java,Jsf,Dependency Injection,Jsf 2.2,Composite Component,如何在备份组件中使用@ManagedProperty 这是partner selector复合组件。组件检查数据库中键入的合作伙伴代码,如果代码有效,则填写合作伙伴名称 组成部分: <cc:interface componentType="partnerSelComp"> <cc:attribute name="value" type="java.lang.Long"/> </cc:interface> <cc:implementation&g

如何在备份组件中使用
@ManagedProperty

这是partner selector复合组件。组件检查数据库中键入的合作伙伴代码,如果代码有效,则填写合作伙伴名称

组成部分:

<cc:interface componentType="partnerSelComp">
    <cc:attribute name="value" type="java.lang.Long"/>
</cc:interface>

<cc:implementation>
    <span id="#{cc.clientId}" style="white-space:nowrap">
        <p:inputText id="id" type="hidden" binding="#{cc.partnerId}"/>                                     
        <p:inputText id="code" binding="#{cc.code}">                     
            <p:ajax event="blur" update="id code name" listener="#{cc.validate}" />
        </p:inputText>            
        <p:inputText id ="name" disabled="true" binding="#{cc.name}" />                   
        <p:message for="code"/>
    </span>
</cc:implementation>
<my:PartnerSelComp value="#{myBean.partnerId}" service="#{partnerService}"/>
    @FacesComponent("partnerSelComp")
    public class PartnerSelComp extends UIInput implements NamingContainer {

        private InputText partnerId;
        private InputText code;    
        private InputText name; 

        @ManagedProperty("#{partnerService}")
        private PartnerService service;

        @Override
        public void encodeBegin(FacesContext context) throws IOException {
            Partner p=null;
            Long i = (Long) getValue();        

            PartnerService service = getAttributeValue("service", null );         

            if (i != null) {            
               p = findPartnerById(service.getList(), i); 
           }
           fill( (i==null) , p); // fills the code and name fields
        }

        @SuppressWarnings("unchecked")
        private <T> T getAttributeValue(String key, T defaultValue) {
            T value = (T) getAttributes().get(key);
            return (value != null) ? value : defaultValue;
        }

   ... 

    }
这是我想访问的bean。(稍后将替换为JPA查询。)

我必须使用
getAttributes().get(key)
来获取引用并将其转换为
PartnerService


谢谢您的回答。

尝试使用html配置界面加载它:

<cc:interface componentType="partnerSelComp">
    <cc:attribute name="value" type="java.lang.Long"/>
    <cc:attribute name="service" default="#{partnerService}"/>
</cc:interface>
关于直接注入@FacesComponent,在下一个JSF版本(2.3)之前是不可能的


一种可能的解决方法是使用
“@Named
而不是
@FacesComponent
,或者如果您无法做到这一点,请尝试库的一些功能。它允许注入@FacesConverter,因此您也可以将其应用于此注释。

显示您的配置。您的PartnerSelComp组件很可能位于配置未加载的包中。是的,您不能直接绑定bean,只能将其声明为属性并像您那样检索。我添加了一些可能有用的附加信息。谢谢您的帮助!背衬组件中没有注入。坏消息…2.3中的支持组件中有注入。。。好消息
    <cc:interface componentType="partnerSelComp">
        <cc:attribute name="value" type="java.lang.Long"/>
        <cc:attribute name="service"/>
    </cc:interface>

    <cc:implementation>
        <span id="#{cc.clientId}" style="white-space:nowrap">
            <p:inputText id="id" type="hidden" binding="#{cc.partnerId}"/>                                     
            <p:inputText id="code" binding="#{cc.code}">                     
                <p:ajax event="blur" update="id code name" listener="#{cc.validate}" />
            </p:inputText>            
            <p:inputText id ="name" disabled="true" binding="#{cc.name}" />                   
            <p:message for="code"/>
        </span>
    </cc:implementation>
    @FacesComponent("partnerSelComp")
    public class PartnerSelComp extends UIInput implements NamingContainer {

        private InputText partnerId;
        private InputText code;    
        private InputText name; 

        @ManagedProperty("#{partnerService}")
        private PartnerService service;

        @Override
        public void encodeBegin(FacesContext context) throws IOException {
            Partner p=null;
            Long i = (Long) getValue();        

            PartnerService service = getAttributeValue("service", null );         

            if (i != null) {            
               p = findPartnerById(service.getList(), i); 
           }
           fill( (i==null) , p); // fills the code and name fields
        }

        @SuppressWarnings("unchecked")
        private <T> T getAttributeValue(String key, T defaultValue) {
            T value = (T) getAttributes().get(key);
            return (value != null) ? value : defaultValue;
        }

   ... 

    }
<cc:interface componentType="partnerSelComp">
    <cc:attribute name="value" type="java.lang.Long"/>
    <cc:attribute name="service" default="#{partnerService}"/>
</cc:interface>
FacesContext.getCurrentInstance().getAttributes().get("service");