jsf - 如何实现 <f :setPropertyActionListener> programmatically

标签 jsf primefaces commandlink

基本上我正在尝试为 <p:datatable> 设置动态列.

我的一个专栏的内容是p:commandLink它曾经显示用于文本编辑的对话框,我在 XHTML 中的工作就像一个魅力,但我需要将它转换为 Java 以实现动态用户自定义和首选项。

这是我的 XHTML 版本:

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

这是我的 Java 版本(不工作):

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);

最佳答案

你需要 UICommand#addActionListener() , 不是 UICommand#setActionListener() . setActionListener()是 JSF 1.x 中弃用的方法,它有效地执行 <p:commandLink actionListener="...">ValueBinding .

关于创建 <f:setPropertyActionListener>以编程方式,不幸的是没有 JSF 实现独立的方式。选择以下选项之一:

  1. 使用 JSF 实现特定类,在 Mojarra 的情况下是 com.sun.faces.taglib.jsf_core.SetPropertyActionListenerImpl :

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

    如果是 MyFaces,则为 org.apache.myfaces.event.SetPropertyActionListener :

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

    请记住,使用 JSF 实现特定类 com.sun.faces.*org.apache.myfaces.*在你自己的代码中是一种糟糕的做法。


  2. 创建自定义 ActionListener完成工作的实现。基本上,只需从 Mojarra 中复制粘贴类的源代码即可。或 MyFaces源代码到你的包中。与 1) 相比,这具有以下优势:当您的 Web 应用程序部署到与其他 JSF 实现捆绑在一起的 Java EE 容器时,它不会中断。


  3. 利用 EL 2.2 的功能在 EL 表达式中传递方法参数。然后你就可以在action中完成工作了或 actionListener属性:

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

    (Exam.class 应该代表 #{exam} 的类型)

    这实际上与

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

    或者如果你真的需要设置一个 Action 监听器:

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

    这实际上与

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

关于jsf - 如何实现 <f :setPropertyActionListener> programmatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20040052/

相关文章:

css - p :layout and p:layoutUnit ignore the style

jquery - 更新后如何滚动到 JSF 组件的底部?

javascript - Javascript 未在包含的 Facelets 页面中被调用

validation - Primefaces 范围 slider 最大值和最小值

java - 更新和处理在 JSF 2.0 Primefaces 3.4 中无法正常工作

java - 将请求参数从托管 Bean 传递到 jsf 页面

jsf - ConfirmDialog中的PrimeFaces commandButton不调用后备bean

ajax - JSF Ajax Link 在链接 Action 之前执行部分ajax渲染

ajax - JSF 2.0,在命令链接上使用 ajax 时格式错误的 XML

java - p :commandLink action does not fire