简体   繁体   English

其中a4j:ajax的a4j:commandButton和h:commandButton之间的区别

[英]Difference between a4j:commandButton and h:commandButton with a4j:ajax in it

I've got the following button: 我有以下按钮:

<a4j:commandButton value="#{AppMessages['general.action.cancel']}"
            disabled="#{!entityBB.expandState.editable}"
            actionListener="#{entityBB.cancel}"
            render="initialServicePanel :messages" status="ajaxStatus"
            immediate="true" />

In my case the above button does everything I need (remove the inError state from an uiComponent to rerender it and reset (cancel) everything in the form to its previous state). 就我而言,上述按钮可以满足我的所有需求(从uiComponent中删除inError状态以将其重新呈现,并将表单中的所有内容重置(取消)为之前的状态)。

When I try to use it as this: 当我尝试以这种方式使用它时:

<h:commandButton value="#{AppMessages['general.action.cancel']}"
    disabled="#{!entityBB.expandState.editable}" immediate="true">
        <a4j:ajax execute="@this" render="initialServicePanel :messages"
        listener="#{entityBB.cancel}" status="ajaxStatus" />
</h:commandButton>

it isn't working (the value gets reset but the uiComponent (an inputText) stays red though in error). 它不起作用(该值被重置,但uiComponent(inputText)虽然出错但仍保持红色)。 Why is this? 为什么是这样? I can't figure out the difference between the two of them since I thought an a4j:commandButton was just an h:commandButton with a4j:ajax in it behind the scenes. 我无法弄清楚它们之间的区别,因为我认为a4j:commandButton只是一个h:commandButton,而幕后却带有a4j:ajax。

EDIT: Now I've got another situation where the h:commandButton removes the value but keeps it in error (red border). 编辑:现在我有另一种情况,其中h:commandButton删除值,但保持错误(红色边框)。 And the a4j:commandButton removes it from in error (red border) but keeps the value.. It's the exact same input as above. 并且a4j:commandButton将其从错误中删除(红色边框),但保留该值。.与上面的输入完全相同。 I can't explain it.. Anyone? 我无法解释。有人吗?

The execute of <a4j:commandButton> defaults to @form , but the one of <a4j:ajax> defaults to @this , which means that only the "current component" is being executed (thus, the button itself). <a4j:commandButton>execute默认为@form ,但<a4j:ajax>默认为@this ,这意味着仅在执行“当前组件”(因此,按钮本身)。 This caused that UIInput#decode() of input components aren't being called during apply request values phase. 这导致在应用请求值阶段期间未调用输入组件的UIInput#decode() It's exactly that method which resets the validity back to true . 正是该方法将有效性重新设置为true

To achieve exactly the same effect with <h:commandButton><a4j:ajax> , you need to explicitly set the execute attribute of <a4j:ajax> to @form as well. 要使用<h:commandButton><a4j:ajax>达到完全相同的效果,还需要将<a4j:ajax>execute属性也显式设置为@form

<h:commandButton ... value="Cancel" immediate="true">
    <a4j:ajax execute="@form" ... />
</h:commandButton>

Update : to clear out the field, which was your initial problem and which actually wouldn't have worked on <a4j:commandButton> either (so the "does everything I need" part is wrong), head to this answer for a breakdown of the problem and both a JSF 2.x and JSF 1.x targeted solution: Input fields hold previous values only if validation failed . 更新 :要清除该字段,这是您的最初问题 ,实际上在<a4j:commandButton>也不起作用(因此“我需要的一切”部分都错了),请转到此答案以获取细分问题以及针对JSF 2.x和JSF 1.x的解决方案: 仅当验证失败时,输入字段才保留先前的值

It is not the same code. 它不是相同的代码。 The translation should be: 翻译应为:

<h:commandButton value="#{AppMessages['general.action.cancel']}"
    disabled="#{!entityBB.expandState.editable}" immediate="true"
    actionListener="#{entityBB.cancel}" >
        <a4j:ajax render="initialServicePanel :messages"
        status="ajaxStatus" />
</h:commandButton>

Main differences: 主要区别:

  • Listener is in <h:commandButton> , not in <a4j:ajax> . 侦听器位于<h:commandButton> ,而不位于<a4j:ajax>
  • Removed execute="@this" which will change the data sent to the server. 删除了execute="@this" ,这将更改发送到服务器的数据。

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

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