【问题标题】:get jsf compoment id in backing bean在 backing bean 中获取 jsf 组件 id
【发布时间】:2015-01-13 09:53:12
【问题描述】:

我有两个这样的 jsf 组件:

 <a4j:commandButton id="cmdtop"

                   disabled="#{ViewDocument.locked or !(documentAttachmentBean.canEditAttachment )}"
                   action="#{documentAttachmentBean.onCmdIncreaseAttachmentCodeClick}"

                   ...


</a4j:commandButton>

<a4j:commandButton id="cmddown"
                   disabled="#{ViewDocument.locked or !(documentAttachmentBean.canEditAttachment )}"
                   action="#{documentAttachmentBean.onCmdDecreaseAttachmentCodeClick}">    
    ...                     

</a4j:commandButton>

在我的带有 canEditAttachment() 方法的支持 bean 中,我想检查它是否已为 cmdtop 或 cmddown 调用? 像这样的东西:

    public boolean getCanEditAttachment()
{
    if (somecode.getElementId.equals"cmdtop")
 //do something
 return true ;
  else if (somecode.getElementId.equals"cmddown")
  //do something else
   return false ;

}

我怎样才能找到 canEditAttachment() 是和谁一起调用的?

【问题讨论】:

    标签: java jsf jsf-2 richfaces


    【解决方案1】:

    JSF 中有一些全局变量。 component 就是其中之一。您可以使用component 变量访问当前正在渲染的组件。

    xhtml

    <h:commandButton id="cmdtop"
                     disabled="#{myBackingBean.canEditAttachment(component.id)}"
                     value="Button 1"/>
    
    <h:commandButton id="cmddown"
                     disabled="#{myBackingBean.canEditAttachment(component.id)}"
                     value="Button 2"/>
    

    java

    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ViewScoped;
    
    @ManagedBean
    @ViewScoped
    public class MyBackingBean {
    
        public boolean canEditAttachment(String componentId) {
            if ("cmdtop".equalsIgnoreCase(componentId))
                return true;
            else if ("cmddown".equalsIgnoreCase(componentId))
                return false;
    
            return true;
        }
    
    }
    

    【讨论】:

    • 谢谢!有用 !但是请您说一下其他全局变量吗?
    • 它们被命名为隐式对象。在this blog post 有一个列表。
    【解决方案2】:

    我相信这只适用于 JSF 2.0 。 JSF 2.0 添加了隐式变量,使解析组件更容易。

    喜欢

    disabled="#{myBackingBean.canEditAttachment(component.id)}"

    如果您正在处理使用低于 2.0 的 JSF 的过时遗留代码,这将不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-06
      • 2018-06-11
      • 2014-08-11
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      相关资源
      最近更新 更多