2013-03-14 4 views
6

मैं एक समग्र घटक लागू कर रहा हूं और मुझे एक समस्या मिली जो मुझे समाधान नहीं मिला।JSF2.0 - वैकल्पिक विधि अभिव्यक्ति के साथ समग्र घटक

मैंने अपने गुण निर्दिष्ट किए हैं जो पृष्ठ लेखक द्वारा पारित किए जा सकते हैं या नहीं, लेकिन मैं एक विधि विशेषता (क्रिया के लिए एक विधि अभिव्यक्ति) निर्दिष्ट नहीं कर सका, अगर यह पारित नहीं किया गया था, तो समग्र घटक नहीं है समग्र में विधि विशेषता का उपयोग करें: कार्यान्वयन टैग।

यहाँ मेरी कोड:

<composite:interface> 
    <composite:attribute name="namePrompt" required="true"/> 
    <composite:attribute name="actionMethod" method-signature="java.lang.String action()" required="false"/> 
    <composite:attribute name="showComponent" default="false"/> 
</composite:interface> 

<composite:implementation> 
    <div> 
     <p:commandLink actionListener="#{cc.attrs.actionMethod}" 
         rendered="#{cc.attrs.showComponent}" 
         > 
      <h:outputText value="#{cc.attrs.namePrompt}"/>  
     </p:commandLink> 
    </div> 
</composite:implementation> 

जब इसे का उपयोग, मैं "actionMethod" विशेषता निर्दिष्ट नहीं किया। इस तरह:

<util:foo namePrompt="SomeName" showComponent="true"/> 

लेकिन मैं त्रुटि संदेश मिलता है:

javax.faces.FacesException: Unable to resolve composite component from using page using EL expression '#{cc.attrs.actionMethod}' 

वहाँ यह करने के लिए कोई तरीका है?

+0

आप 'actionListener' को वैकल्पिक विशेषता पारित कर सकते हैं। आप मामले का क्या उपयोग करते हैं, आपको क्या लगता है कि यह तब होना चाहिए जब 'actionListener' को हल नहीं किया गया है और' कमांडलिंक 'प्रदान किया गया है? – partlov

+0

हां लिंक घटक प्रदान किया जाता है, लेकिन जब लिंक घटक में क्लिक क्रिया होती है तो यह शिकायत करता है कि मैंने एक्शन विधि विधि के लिए एक विधि परिभाषित नहीं की है। लेकिन यह मेरा इरादा है, कभी-कभी मैं actionMethod विशेषता के लिए एक क्रिया विधि को परिभाषित नहीं करना चाहता हूं। क्या यह संभव है? मैंने सोचा कि 'आवश्यक = झूठी' समस्या को हल करें। –

उत्तर

10

आप दो p:commandLink तत्वों को बनाने और सशर्त अपने पैरामीटर की परिभाषा के अनुसार उन्हें प्रस्तुत करना होगा:

<p:commandLink actionListener="#{cc.attrs.actionMethod}" rendered="#{!empty cc.getValueExpression('actionMethod') and cc.attrs.showComponent}"> 
    <h:outputText value="#{cc.attrs.namePrompt}"/> 
</p:commandLink> 
<p:commandLink rendered="#{empty cc.getValueExpression('actionMethod')}"> 
    <h:outputText value="#{cc.attrs.namePrompt}"/> 
</p:commandLink> 
+0

हाँ यह काम करता है। आपके समय एवं मदद के लिए धन्यवाद। –

3

एक अन्य समाधान एक कार्रवाई विधि के साथ, एक स्वयं के घटक प्रकार बनाने के लिए है। उदाहरण:

@FacesComponent("myButton") 
public class MyButton extends UINamingContainer { 

    public MyButton() { 
    } 

    public String action() { 
     MethodExpression me = (MethodExpression) this.getAttributes().get("actionMethod"); 
     if (me != null) { 
      try { 
       Object result = me.invoke(FacesContext.getCurrentInstance().getELContext(),  null); 
       if (result instanceof String) { 
        return (String) result; 
       } 
      } catch (ValidatorException ve) { 
       throw ve; 
      } 
     } 
     return null; 
    } 
} 
1

एक ही सटीक त्रुटि थी और भी मेरे घटक पर एक वैकल्पिक कार्रवाई विधि है करने के लिए आवश्यक:

<composite:interface componentType="myButton"> 
    <composite:attribute name="namePrompt" required="true"/> 
    <composite:attribute name="actionMethod" method-signature="java.lang.String action()" required="false"/> 
    <composite:attribute name="showComponent" default="false"/> 
</composite:interface> 

<composite:implementation> 
    <div> 
     <p:commandLink actionListener="#{cc.action()}" rendered="#{cc.attrs.showComponent}"> 
      <h:outputText value="#{cc.attrs.namePrompt}"/>  
     </p:commandLink> 
    </div> 
</composite:implementation> 

और componentType तरह देखने के लिए है।

इसलिए मैंने विधि-हस्ताक्षर के साथ समग्र विशेषता पर एक डिफ़ॉल्ट पैरामीटर जोड़ने का प्रयास किया है, जो संबंधित FacesComponent क्लास पर एक विधि को इंगित करता है और यह बहुत अच्छा काम करता है!

घटक:

<composite:interface componentType="myButton"> 
    <composite:attribute name="namePrompt" required="true"/> 
    <composite:attribute name="actionMethod" method-signature="java.lang.String action()" required="false" default="#{cc.dummyAction}"/> 
    <composite:attribute name="showComponent" default="false"/> 
</composite:interface> 

<composite:implementation> 
    <div> 
     <p:commandLink action="#{cc.attrs.actionMethod}" 
         rendered="#{cc.attrs.showComponent}" 
         > 
      <h:outputText value="#{cc.attrs.namePrompt}"/>  
     </p:commandLink> 
    </div> 
</composite:implementation> 

FacesComponent वर्ग:

@FacesComponent("myButton") 
public class MyButton extends UINamingContainer { 

    public MyButton() { 
    } 

    public String dummyAction() { 
     return ""; 
    } 

} 
3

बदलें java.lang.Object लिए विधि हस्ताक्षर वापसी प्रकार और डिफ़ॉल्ट मान के रूप में जोड़ें "अशक्त"।

<composite:interface> 
    <composite:attribute name="namePrompt" required="true"/> 
    <composite:attribute name="actionMethod" method-signature="java.lang.Object action()" required="false" default="null"/> 
    <composite:attribute name="showComponent" default="false"/> 
</composite:interface> 

<composite:implementation> 
    <div> 
     <p:commandLink actionListener="#{cc.attrs.actionMethod}" 
         rendered="#{cc.attrs.showComponent}" 
         > 
      <h:outputText value="#{cc.attrs.namePrompt}"/>  
     </p:commandLink> 
    </div> 
</composite:implementation> 
विधि के बिना

: विधि के साथ

<util:foo namePrompt="SomeName" showComponent="true"/> 

:

<util:foo actionMethod="#{someBean.someMethod()}" namePrompt="SomeName" showComponent="true"/> 
संबंधित मुद्दे