【问题标题】:How to get command link value(display name) from backing bean?如何从支持 bean 获取命令链接值(显示名称)?
【发布时间】:2013-08-03 21:32:06
【问题描述】:

我的 xhtml 中有一个 p:commandLink,其值在“显示”/“隐藏”之间切换。 有什么方法可以从支持 bean 中获取这个 commandlink 的值吗? 我的意思是,我想知道命令链接当前显示的是什么值,即显示/隐藏?

【问题讨论】:

  • 相反,您可以在 commandLink 上的方法调用中传递参数。
  • 如果您需要本地化您的 web 应用程序并因此提供不同语言的按钮标签,例如 value="#{msg['button.label']}",该怎么办?需要知道按钮的值绝对不是一个好的解决方案,因为您还必须考虑所有这些本地化值。我建议重新表述你的问题,而不是询问如果多个按钮调用相同的方法,如何区分按下的按钮。
  • @BalusC-我同意你的观点,你已经正确理解了我的要求。从我正在调用的命令链接和 actionListener 方法中,我想区分它当前是否显示“显示”/“隐藏” .因为基于此,我必须向用户显示一个弹出对话框。例如如果是“显示”,我希望对话框出现。但是,如果“隐藏”,我只想绕过调用此对话框的 bean 代码。仅供参考,我正在从后端显示对话框。是否可以假设存在没有其他语言支持吗?
  • @PankajKathiriya-您能提供一个例子吗?例如,在我的 actionlistener 方法中,当 commandLink 读取为“Show”时,我想传递“Show”?

标签: jsf-2 primefaces


【解决方案1】:

重要的是,调用组件仅在ActionEvent 参数中可用:

<h:commandLink id="show" value="Show it" actionListener="#{bean.toggle}" />
<h:commandLink id="hide" value="Hide it" actionListener="#{bean.toggle}" />

public void toggle(ActionEvent event) {
    UIComponent component = event.getComponent();
    String value = (String) component.getAttributes().get("value");
    // ...
}

但是,这是一个糟糕的设计。可本地化的文本绝对不应用作业务逻辑的一部分。

相反,either 挂钩组件 ID:

String id = (String) component.getId();

传递一个方法参数(EL 2.2 or JBoss EL required):

<h:commandLink id="show" value="Show it" actionListener="#{bean.toggle(true)}" />
<h:commandLink id="hide" value="Hide it" actionListener="#{bean.toggle(false)}" />

public void toggle(boolean show) {
    this.show = show;
}

或者甚至直接调用setter方法而不需要额外的动作监听方法:

<h:commandLink id="show" value="Show it" actionListener="#{bean.setShow(true)}" />
<h:commandLink id="hide" value="Hide it" actionListener="#{bean.setShow(false)}" />

【讨论】:

    【解决方案2】:

    正如@BalusC 建议的那样,您的方法不是一个好的解决方案。但是,如果您真的想这样做,您可以将组件 (p:commandLink) 绑定到您的 backingbean,如 What is the advantages of using binding attribute in JSF? 中所示。 组件绑定后,可以从p:commandLink访问value属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-24
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      相关资源
      最近更新 更多