2012-08-23 14 views
7

मैं अपने वेबपृष्ठ पर जेएसएफ <h:commandButtons> गतिशील रूप से जोड़ने की कोशिश कर रहा हूं, और अब तक मैं उन्हें प्रदर्शित कर रहा हूं, लेकिन मैं पैरामीटर के साथ कार्रवाई सेट नहीं कर सकता, जैसे कि मैं स्थिर में पृष्ठ:
action="#{bean.function(parameter)}"। (यह निश्चित रूप से ईएल-2.2 का उपयोग कर रहा है)
आसपास देखकर मुझे लगता है कि मुझे MethodExpression बनाना है, लेकिन यह मेरे लिए अस्पष्ट है और मैं इस पर अधिक जानकारी नहीं पा रहा हूं। अगर कोई धुंध के माध्यम से एक प्रकाश चमक सकता है और समझा सकता है कि यह कैसे किया जा सकता है, तो इसकी सराहना की जाएगी।डायनेमिक जेएसएफ (2.0) कमांडबटन - तर्क के साथ कार्रवाई सेट करें

संपादित करें: तो अब मैं इस

public void displayNode(String childName){ 
//lots of messy code instantiating JSF components 

if(activeEmployee.getParent() != null){ 
     HtmlCommandButton parent = new HtmlCommandButton(); 
     HtmlOutputText parentLabel = new HtmlOutputText(); 

     parentLabel.setId("label" + count++); //I really hate having to use count 
     parentLabel.setValue("Parent: "); 

     parent.setId("Parent" + count++); 
     String parentName = activeEmployee.getParent().getName(); 
     parent.setValue(parentName); 
     MethodExpression expression = createMethodExpression("#{tree.displayNode('" + parentName + "')}", 
                   null, String.class); 
     parent.setActionExpression(expression); 

     newDiv.getChildren().add(parentLabel); 
     newDiv.getChildren().add(parent); 
    } 
+0

क्या आप कमांड बटन दिखाने/छुपाने के लिए प्रस्तुत संपत्ति का उपयोग नहीं कर सकते? – RajV

+0

नहीं मैं गतिशील रूप से अन्य संबंधित नोड्स प्रदर्शित करने के लिए बटन के साथ पेड़ नोड्स बना रहा हूं –

उत्तर

17

उपयोग ExpressionFactory#createMethodExpression() है।

public static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes) { 
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    return facesContext.getApplication().getExpressionFactory().createMethodExpression(
     facesContext.getELContext(), expression, returnType, parameterTypes); 
} 

निम्नलिखित कार्रवाई विधि उदाहरण:

public abstract MethodExpression createMethodExpression(
             ELContext context, 
             java.lang.String expression, 
             java.lang.Class<?> expectedReturnType, 
             java.lang.Class<?>[] expectedParamTypes) 

यहाँ एक सुविधा विधि है

public void submit1() 
public String submit2() 
public void submit3(String argument) 
public String submit4(String argument) 
public void submit5(String argument1, Long argument2) 
public String submit6(Long argument1, String argument2) 

तो इस प्रकार बनाया जा सकता है:

createMethodExpression("#{bean.submit1}", null); 
createMethodExpression("#{bean.submit2}", String.class); 
createMethodExpression("#{bean.submit3('foo')}", null, String.class); 
createMethodExpression("#{bean.submit4('foo')}", String.class, String.class); 
createMethodExpression("#{bean.submit5('foo', 0)}", null, String.class, Long.class); 
createMethodExpression("#{bean.submit6(0, 'foo')}", String.class, Long.class, String.class); 

ध्यान दें कि ईएल अभिव्यक्ति बिल्कुल वही है जैसा आप सामान्य दृश्य फ़ाइल में उपयोग करेंगे।


अद्यतन यहाँ एक SSCCE जो बिलाव 7.0.27 पर Mojarra 2.1.12 के साथ मेरे लिए ठीक काम करता है।

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
> 
    <h:head> 
     <title>SO question 12098611</title> 
    </h:head> 
    <h:body> 
     <h:form binding="#{bean.form}"> 
      <h:commandButton value="add" action="#{bean.add}" /> 
     </h:form> 
    </h:body> 
</html> 
@ManagedBean 
@RequestScoped 
public class Bean { 

    private UIForm form; 

    public void add() { 
     String id = "button" + form.getChildCount(); 
     UICommand button = new HtmlCommandButton(); 
     button.setId(id); 
     button.setValue(id); 
     button.setActionExpression(createMethodExpression(String.format("#{bean.submit('%s')}", id), null, String.class)); 
     form.getChildren().add(button); 
    } 

    public void submit(String arg) { 
     System.out.println("submit: " + arg); 
    } 

    public UIForm getForm() { 
     return form; 
    } 

    public void setForm(UIForm form) { 
     this.form = form; 
    } 

    public static MethodExpression createMethodExpression(String expression, Class<?> returnType, Class<?>... parameterTypes) { 
     FacesContext facesContext = FacesContext.getCurrentInstance(); 
     return facesContext.getApplication().getExpressionFactory().createMethodExpression(
      facesContext.getELContext(), expression, returnType, parameterTypes); 
    } 

} 

असंबंधित ठोस समस्या के लिए, ऊपर के सभी एक गरीब अभ्यास है। यह भी देखें How does the 'binding' attribute work in JSF? When and how should it be used?

+1

एक और बार, आप आदमी हैं। मैं इंटरनेट पर इस जानकारी की तलाश में था क्योंकि मैंने इसे बनाया है लेकिन जेएसएफ 1.2 का उपयोग कर रहा हूं। –

+0

मुझे लगता है कि मैंने समय-समय पर माना है कि मुझे 'MethodExpression' की आवश्यकता है क्योंकि मैंने इसे' button.setActionExpression (...) 'का उपयोग करने से पहले देखा है। स्पष्ट तर्कों के अलावा 'setActionExpression() 'और' setAction()' का उपयोग करने के बीच क्या अंतर है? –

+1

@ जेक: 'सेटएक्शन() '[बहिष्कृत] है (http://docs.oracle.com/javaee/6/api/javax/faces/component/UICommand.html#setAction (javax.faces.el.Method बाइंडिंग)) "स्थगित ईएल" का उपयोग करके जेएसएफ 1.x का बचे हुए, जिसने 'वैल्यू बाइंडिंग', 'मेथड बाइंडिंग' और इसी तरह (जिसे बाद में जेएसपी ईएल के साथ एकीकृत किया गया था) के साथ अपने स्वयं के ईएल कार्यान्वयन का उपयोग किया। ** ** ** जेएसएफ 2.x में किसी भी बहिष्कृत एपीआई का उपयोग न करें। वे हटाने के लिए विषय हैं। ईएल पर कुछ इतिहास के लिए http://stackoverflow.com/questions/4812755/difference-between-jsp-el-jsf-el-and-unified-el भी देखें। – BalusC

संबंधित मुद्दे