gpt4 book ai didi

jsf - 如何实现 programmatically

转载 作者:行者123 更新时间:2023-12-01 05:22:38 25 4
gpt4 key购买 nike

基本上我正在尝试为 <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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com