سؤال

Basically i'm trying to set a dynamic columns for a <p:datatable>.

The content of one of my columns is a p:commandLink which used to show a dialog for text editing, i have this working like a charm in the XHTML but I need to translate it to Java for dynamic user customization and preferences.

here is what is my XHTML version:

<p:commandLink id="MRepShowButton" update=":form1:display" onclick="EditorDialog.show();"  title="Editer le compte rendu"> 
     <f:setPropertyActionListener value="#{exam}" target="#{examenListBean.selectedExamen}" />  
</p:commandLink>

and this is my Java version(not working):

CommandLink rapstatelink = (CommandLink)application.createComponent(CommandLink.COMPONENT_TYPE);
rapstatelink.setId("MRepShowButton");
rapstatelink.setUpdate(":form1:display");
rapstatelink.setOnclick("EditorDialog.show();");
rapstatelink.setTitle("Editer le rapport du patient");

ValueExpression target = ef.createValueExpression(elc, "#{exam}", Object.class);
ValueExpression value = ef.createValueExpression(elc, "#{examenListBean.selectedExamen}", Object.class);


//rapstatelink.setActionListener(new SetPropertyActionListenerHandler(**i don't know wht to do here **));
column.getChildren().add(rapstatelink);
 table.getChildren().add(column);
هل كانت مفيدة؟

المحلول

You need UICommand#addActionListener(), not UICommand#setActionListener(). The setActionListener() is a deprecated method from JSF 1.x which effectively does a <p:commandLink actionListener="..."> with a ValueBinding.

As to creating the <f:setPropertyActionListener> programmatically, there's unfortunately no JSF implementation independent way for that. Choose either of the following options:

  1. Use the JSF implementation specific class, in case of Mojarra that's the com.sun.faces.taglib.jsf_core.SetPropertyActionListenerImpl:

    link.addActionListener(new SetPropertyActionListenerImpl(target, value));
    

    In case of MyFaces that's the org.apache.myfaces.event.SetPropertyActionListener:

    link.addActionListener(new SetPropertyActionListener(target, value));
    

    Keep in mind that using JSF implementation specific classes com.sun.faces.* or org.apache.myfaces.* in your own code is a poor practice.


  2. Create a custom ActionListener implementation which does the job. Basically, just copypaste the class' source code from either Mojarra or MyFaces source code into your package. As compared to 1) this has the advantage that your web application does not break when deployed to a Java EE container which ships with the other JSF implementation bundled.


  3. Make use of EL 2.2 feature of the ability to pass method arguments in EL expressions. Then you can just do the job in action or actionListener attribute:

    link.setActionExpression(ef.createMethodExpression(elc, 
       "#{examenListBean.setSelectedExamen(exam)}", Void.class, Exam.class));
    

    (the Exam.class should represent the type of #{exam})

    This does effectively the same as

    <p:commandLink ... action="#{examenListBean.setSelectedExamen(exam)}" />
    

    Or if you really need to set an action listener:

    link.addActionListener(new MethodExpressionActionListener(ef.createMethodExpression(elc, 
       "#{examenListBean.setSelectedExamen(exam)}", Void.class, Exam.class)));
    

    This does effectively the same as

    <p:commandLink ... actionListener="#{examenListBean.setSelectedExamen(exam)}" />
    
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top