Jsf 如何通过Id获取commandButton?

Jsf 如何通过Id获取commandButton?,jsf,primefaces,Jsf,Primefaces,我想根据事先选择的图标更改commandButton的ActionListener: <p:dialog id="dialog" header="Add Memo" widgetVar="dialogMemo" resizable="false" > <h:form> <h:panelGrid columns="2" cellpadding="5"> <h:out

我想根据事先选择的图标更改commandButton的ActionListener:

        <p:dialog id="dialog" header="Add Memo" widgetVar="dialogMemo" resizable="false" >
        <h:form>
            <h:panelGrid columns="2" cellpadding="5">  
                <h:outputLabel for="commentInput" value="Comment:" />  
                <p:inputTextarea id="commentInput" value="#{dashboardBean.getCurrentComment()}" rows="6" cols="25" label="commentInput"/>
                <p:watermark for="commentInput" value="Enter your memo..."/>  
                <h:outputLabel for="selectShare" value="Share Memo: " />  
                <p:selectBooleanCheckbox id="selectShare" /> 

                <h:outputLabel for="choosePriority" value="Priority:" />  
                <p:selectOneMenu id="choosePriority" value="#{dashboardBean.currentPriority}" label="choosePriority">  
                    <f:selectItem itemLabel="Low Priority" itemValue="1" />  
                    <f:selectItem itemLabel="Medium Priority" itemValue="2" />  
                    <f:selectItem itemLabel="High Priority" itemValue="3" />  
                </p:selectOneMenu>

                <p:commandButton id="submitDialog" icon="ui-icon-check" value="Confirm" ajax='false' type="submit" action="#{dashboardBean.getLastMemo()}"/>
                <p:commandButton icon="ui-icon-close" onclick="dialogMemo.hide();" value="Cancel"/>
            </h:panelGrid>
        </h:form>
    </p:dialog>
    <p:layout fullPage="true">
        <p:layoutUnit id="leftPanel" position="west" size="250" header="My Memos" resizable="false" closable="false" collapsible="false">
            <h:form id="form"> 
                <p:commandButton id="addMemo" icon="ui-icon-plus" onclick="dialogMemo.show();" type="submit" action="#{dashboardBean.getEditControl}"/>  
                <p:dashboard id="dashboardId" model="#{dashboardBean.model}" binding="#{dashboardBean.dashboard}">
                </p:dashboard>  
            </h:form>
        </p:layoutUnit>           
</h:body>
但是“button.getId()”不起作用

但是“button.getId()”不起作用

我推测这是因为
按钮
实际上是
null
,因此试图调用
getId()
方法引发了
NullPointerException
?令人惊讶的是,您这样表述问题,“button.getId()不起作用”而不是“
view.findComponent()
返回
null
”(这反过来又产生了一个非常明显的后果,即任何试图访问它的行为都会引发
NullPointerException
)。如果您确实不知道它为什么抛出
NullPointerException
,那么我强烈建议您暂停一下JSF,先学习基本Java


回到具体问题,当客户端ID
submitDialog
在组件树中不存在时,
findComponent
将返回
null
。实际上,它位于
组件的
NamingContainer
中。按钮的客户端ID更可能是
formId:submitDialog
,其中
formId
是您必须分配的按钮父窗体的ID。找到它的一个简单方法是检查按钮生成的HTML表示的ID

另请参见此相关问题:只需将答案中的“ajax”替换为
findComponent

但是“button.getId()”不起作用

我推测这是因为
按钮
实际上是
null
,因此试图调用
getId()
方法引发了
NullPointerException
?令人惊讶的是,您这样表述问题,“button.getId()不起作用”而不是“
view.findComponent()
返回
null
”(这反过来又产生了一个非常明显的后果,即任何试图访问它的行为都会引发
NullPointerException
)。如果您确实不知道它为什么抛出
NullPointerException
,那么我强烈建议您暂停一下JSF,先学习基本Java


回到具体问题,当客户端ID
submitDialog
在组件树中不存在时,
findComponent
将返回
null
。实际上,它位于
组件的
NamingContainer
中。按钮的客户端ID更可能是
formId:submitDialog
,其中
formId
是您必须分配的按钮父窗体的ID。找到它的一个简单方法是检查按钮生成的HTML表示的ID


另请参见此相关问题:只需将答案中的“ajax”替换为
findComponent

我不确定您这样做是否正确。 若你们想得到组件实例,更好的方法不是通过id得到,而是通过添加

<p:commandButton id="submitDialog" icon="ui-icon-check" 
value="Confirm" ajax='false' type="submit" 
action="#{dashboardBean.getLastMemo()}" binding="#{dashboardBean.component}" />

这样,bean中的属性将始终具有所需的组件实例。
请注意,放置组件绑定的bean的范围应该是视图,以避免副作用。

我不确定这样做是否正确。 若你们想得到组件实例,更好的方法不是通过id得到,而是通过添加

<p:commandButton id="submitDialog" icon="ui-icon-check" 
value="Confirm" ajax='false' type="submit" 
action="#{dashboardBean.getLastMemo()}" binding="#{dashboardBean.component}" />

这样,bean中的属性将始终具有所需的组件实例。
请注意,放置组件绑定的bean的范围应该是视图,以避免副作用。

条件表达式导致运行时异常,因为该表达式不表示有效的方法表达式。绑定并不能解决OP的特殊问题,如果bean不在请求范围内,绑定可能会使问题变得更糟。方法表达式不允许使用此类表达式。是我的错。如果你不介意的话,我会把它从答案中删除。但是,只要组件上的操作侦听器的更改是视图范围的,那么最好使用具有绑定属性的视图范围bean,而不是通过从视图中获取id来更改组件。在任何情况下,在bean属性上使用
绑定
是可疑的,只有在绝对没有其他合理方法的情况下才应该使用。条件表达式会导致运行时异常,因为该表达式不表示有效的方法表达式。绑定并不能解决OP的特殊问题,如果bean不在请求范围内,绑定可能会使问题变得更糟。方法表达式不允许使用此类表达式。是我的错。如果你不介意的话,我会把它从答案中删除。但是,就组件上操作侦听器的更改将是视图范围而言,最好使用具有绑定属性的视图范围bean,而不是通过从视图中获取id来更改组件。在任何情况下,在bean属性上使用
binding
都是可疑的,只有在绝对没有其他合理方法的情况下才应该使用。