简体   繁体   English

设置为“禁用”时,JSF a4j:commandButton不起作用

[英]JSF a4j:commandButton not working when 'disabled' is set

When I include a 'disabled' attribute on an a4j:commandButton, the button's action is not performed. 当我在a4j:commandButton上包含“ disabled”属性时,不会执行按钮的动作。 Taking the 'disabled' attribute out causes it to work properly. 删除“禁用”属性会使它正常工作。 I am not doing any special validation (that I'm aware of) and am not seeing any validation error messages. 我没有做任何特殊的验证(我知道),也没有看到任何验证错误消息。

Here is part of my page: 这是我页面的一部分:

<t:dataTable id="myTable"
             var="region"
             value="#{MyPageBackingBean.regions}"
             width="100%">

...

<a4j:commandButton value="Update"
                   action="#{region.doUpdate}"
                   oncomplete="alert('done');"
                   disabled="#{!empty region && region.messageEmpty}"
                   immediate="true"/>

...

</t:dataTable>

Any ideas? 有任何想法吗? Thanks! 谢谢!

Edit: 编辑:

I tried setting preserveDataModel="true" on the t:dataTable to no avail. 我尝试将t:dataTable上的prepareDataModel =“ true”设置为无效。

I also made a test having an a4j:commandButton and text box with no data table, but the backing bean action is still not being fired: 我还进行了一个测试,该测试具有a4j:commandButton和没有数据表的文本框,但仍未触发backing bean操作:

      <h:form>
     <a4j:region>
        <a4j:outputPanel id="testregion">
        <h:messages id="messages"/>

                          <a4j:status>
                             <f:facet name="start">
                                <h:graphicImage value="/images/progress_indicator.gif"/>
                             </f:facet>
                          </a4j:status>

                       <h:inputTextarea
                             rows="5"
                             value="#{MyPageBackingBean.myValue}"
                             style="width:100%; border: 1px solid #99CCFF;">
                          <a4j:support event="onkeyup"
                                       reRender="testregion"
                                       eventsQueue="messageModificationQueue"
                                       ignoreDupResponses="true"
                                       requestDelay="500"/>
                       </h:inputTextarea>

                       <a4j:commandButton id="doDelete"
                                          value="Delete"
                                          action="#{MyPageBackingBean.dummy}"
                                          reRender="testregion"
                                          disabled="#{empty MyPageBackingBean.myValue}"/>
                    <h:outputText value="#{MyPageBackingBean.myValue}"/>
        </a4j:outputPanel>
     </a4j:region>
  </h:form>

Here is the new backing bean code used for testing: 这是用于测试的新的支持bean代码:

private String m_myValue = null;
   public String getMyValue()
   {
      return m_myValue;
   }
   public void setMyValue(String value)
   {
      m_myValue = value;
   }
   private String mystr2 = null;
   public String dummy()
   {
      mystr2 = "hello";
      return null;
   }

Thanks! 谢谢!

In HTML world, the disabled attribute causes the name - value attribute pair of any HTML input element ( input , select , textarea and button ) not being sent to the server side. 在HTML世界中, disabled属性导致任何HTML输入元素( inputselecttextareabutton )的name - value属性对都不会发送到服务器端。

In JSF world, the presense of the name attribute is been used to identify the bean action to be invoked at the server side. 在JSF世界中, name属性的存在被用来标识要在服务器端调用的bean操作。 However, during apply request values phase of the form submit JSF also checks if the component's disabled (and rendered ) attribtue evaluates true before taking any actions. 但是,在表单提交的“申请请求值”阶段,JSF还会在执行任何操作之前检查组件的disabled (和rendered )属性是否为true

The #{region} is here an iterated table row object of #{MyPageBackingBean.regions} whose isMessageEmpty() getter by default returns true . #{region}在这里是#{MyPageBackingBean.regions}isMessageEmpty()行对象,其isMessageEmpty() getter默认情况下返回true In the new request of the form submit the #{MyPageBackingBean.regions} is apparently empty which effectlively makes the button disabled . 在表单的新请求中,提交#{MyPageBackingBean.regions}显然为空,这实际上使该按钮处于disabled JSF won't invoke the associated action then. 然后,JSF将不会调用关联的操作。

To the point, you need to make sure that #{MyPageBackingBean.regions} returns exactly the same datamodel in the subsequent request. 到目前为止,您需要确保#{MyPageBackingBean.regions}在后续请求中返回完全相同的数据模型。 Easiest fix is to place the MyPageBackingBean bean in session scope so that it don't get reinitialized in the subsequent request, but that has more negative side effects as well. 最简单的解决方法是将MyPageBackingBean bean放置在会话范围内,这样就不会在后续请求中将其重新初始化,但这也会带来更多负面影响。 Another fix is to rearrange the datamodel loading so that it happens in the bean constructor. 另一个解决方法是重新安排数据模型的加载,使其在bean构造函数中发生。 As you're already using Tomahawk's <t:dataTable> , you need to set its preserveDataModel attribute to true . 由于您已经在使用Tomahawk的<t:dataTable> ,因此需要将其preserveDataModel属性设置为true For more hints about using datatables, you may find this article useful as well: Using Datatables . 有关使用数据表的更多提示,您可能会发现本文也很有用: 使用数据表

One other thing to note about the disabled property for an a4j:commandButton: if the disabled property is set to true, then the ajax hook for the button's onclick event is never rendered into the final HTML. 关于a4j:commandButton的disabled属性要注意的另一件事:如果disabled属性设置为true,则按钮的onclick事件的ajax钩子永远不会呈现到最终HTML中。 That is, you get a button like this: 也就是说,您将获得如下所示的按钮:

<input type="button" onclick="return false" ... />

instead of this: 代替这个:

<input type="button" onclick="A4J.AJAX.Submit('....');return false" ... />

So, if you want to do something like this: 因此,如果您想执行以下操作:

  1. Render page with disabled a4j:button 禁用a4j:button的渲染页面
  2. Enable a4j:button on client side based on user interaction 根据用户交互在客户端启用a4j:button
  3. Click on a4j:button to call server side action 单击a4j:按钮以调用服务器端操作

Then you will instead need to do something like this: 然后,您将需要执行以下操作:

  1. Render page with a4j:button and disabled="#{true}" 呈现页面并带有a4j:button和disable =“#{true}”
  2. Have the user interaction trigger a re-render of the a4j:button with disabled="#{false}" 让用户交互触发禁用了=“#{false}”的a4j:button的重新渲染
  3. Click on the a4j:button to call the server side action 单击a4j:按钮以调用服务器端操作

Here's a simple example: 这是一个简单的例子:

<h:selectOneCheckbox value="#{myAction.booleanProperty}">
  <a4j:support event="onclick" reRender="button1" />
</h:selectOneCheckbox>
<a4j:commandButton id="button1" action="#{myAction.doSomething}" 
  disabled="#{myAction.booleanProperty eq false}" value="click me"
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM