JSF AJAX验证:执行=“@ this”render =“@ form”不一致,具体取决于先前的请求

时间:2022-05-16 16:42:19

I have an h:commandLink that calls a method that changes a property bound to a radio input.

我有一个h:commandLink,它调用一个方法来改变绑定到无线电输入的属性。

I also have a text input on the same form with some validation (required=true).

我还在同一个表单上有一个文本输入,带有一些验证(required = true)。

If I leave the text input blank, and click on h:commandLink with execute="@this, the radio button updates from the model property as expected, because the text input is never processed and validation never fires.

如果我将文本输入留空,并使用execute =“@ this单击h:commandLink,则单选按钮会根据预期从模型属性更新,因为文本输入永远不会被处理,并且验证永远不会触发。

However, if first I click on a different h:commandLink with execute="@form", then the link with execute="@this", the validation messages go away but the radio button value does not update from the model even though the UIInput for the radio button was never in an invalid state.

但是,如果我首先使用execute =“@ form”单击另一个h:commandLink,然后使用execute =“@ this”链接,验证消息将消失但单选按钮值不会从模型更新,即使单选按钮的UIInput从未处于无效状态。

I'm finding it annoying that the execute="@this" behaves differently depending on what I had done previously, when my intent with @this was to force everything to update from the model and ignore any submitted values from the components.

我发现执行=“@ this”的行为有所不同,这取决于我以前做过的事情,当我对@this的意图是迫使所有内容从模型更新并忽略组件中提交的任何值时。

I suspect what's happening is something like this:

我怀疑发生的事情是这样的:

  • with @form, radio button and text are both processed.
  • 使用@form,单选按钮和文本都被处理。
  • Radio button is valid so localValue is set
  • 单选按钮有效,因此设置了localValue
  • Process validations phase fails overall because of invalid text input, so localValue remains set and does not get cleared or propagate to value
  • 由于文本输入无效,进程验证阶段总体失败,因此localValue保持设置状态并且不会被清除或传播到值
  • the only way out of this is to explicitly call resetValue() or to re-process the component in question (e.g., execute="@this radio") to clear out localValue and then allow refreshing from bean.
  • 除此之外的唯一方法是显式调用resetValue()或重新处理有问题的组件(例如,execute =“@ this radio”)以清除localValue,然后允许从bean刷新。

My questions are:

我的问题是:

  • is my understanding of the lifecycle correct?
  • 我对生命周期的理解是否正确?
  • Am I doing something wrong, or is this one of the annoying-by design things about JSF?
  • 我做错了什么,或者这是关于JSF的烦人设计事情之一?

It feels like this may just be another example of this question

感觉这可能只是这个问题的另一个例子

How can I populate a text field using PrimeFaces AJAX after validation errors occur?

在验证错误发生后,如何使用PrimeFaces AJAX填充文本字段?

Unfortunately I feel like I'm finding lots of those lately. :-(

不幸的是,我觉得我最近发现了很多这些。 :-(

Code example below:

代码示例如下:

<h:form>
<h:messages/>

Radio = 
<h:selectOneRadio value="#{testBean.radioValue}" id="radio" binding="#{radio}">
    <f:selectItem itemValue="foo" itemLabel="Foo"/>
    <f:selectItem itemValue="bar" itemLabel="Bar"/>
</h:selectOneRadio>

<br></br>
radio bean value = <h:outputText value="#{testBean.radioValue}"/>
<br></br>
radio UIInput localValue = <h:outputText value="#{radio.localValue}"/>
<br></br>
radio UIInput value = <h:outputText value="#{radio.value}"/>

<br></br>

String w/validation = <h:inputText value="#{testBean.stringValue}" required="true" />

<br></br>

<ul>
<li>
<h:commandLink actionListener="#{testBean.changeRadioValue}" value="execute = @form">
    <f:ajax render="@form" execute="@form"/>
</h:commandLink>
</li>

<li>
<h:commandLink actionListener="#{testBean.changeRadioValue}" value="execute = @this">
    <f:ajax render="@form" execute="@this"/>
</h:commandLink>
</li>

    <li>
<h:commandLink actionListener="#{testBean.changeRadioValue}" value="execute = @this radio">
    <f:ajax render="@form" execute="@this radio"/>
</h:commandLink>
</li>   
</ul>

</h:form>

And this bean:

这豆子:

@ManagedBean
@ViewScoped
public class TestBean {

private String radioValue = "foo";
private String stringValue;

public void changeRadioValue() {
    radioValue = "bar";
}
 // + getters/setters
 }

1 个解决方案

#1


6  

is my understanding of the lifecycle correct?

我对生命周期的理解是否正确?

Yes.

是。


Am I doing something wrong, or is this one of the annoying-by design things about JSF?

我做错了什么,或者这是关于JSF的烦人设计事情之一?

It's one of the "annoying by-design things" of JSF. As stated in my answer on that related question:

这是JSF的“烦人的设计之物”之一。正如我在该相关问题的答案中所述:

Coming back to the concrete problem, I'd imagine that this is an oversight in the JSF2 specification. It would make much more sense to us, JSF developers, when the JSF specification mandates the following:

回到具体问题,我想这是JSF2规范中的一个疏忽。当JSF规范要求以下内容时,JSF开发人员会更有意义:

  • When JSF needs to update/re-render an input component by an ajax request, and that input component is not included in the process/execute of the ajax request, then JSF should reset the input component's value.
  • 当JSF需要通过ajax请求更新/重新呈现输入组件,并且该输入组件未包含在ajax请求的进程/执行中时,JSF应该重置输入组件的值。

I only don't remember anymore if I have ever reported this against JSF spec. Edit: I reported it: JSF spec issue 1060.

如果我曾经针对JSF规范报告这个,我只是不记得了。编辑:我报告了它:JSF规范问题1060。

#1


6  

is my understanding of the lifecycle correct?

我对生命周期的理解是否正确?

Yes.

是。


Am I doing something wrong, or is this one of the annoying-by design things about JSF?

我做错了什么,或者这是关于JSF的烦人设计事情之一?

It's one of the "annoying by-design things" of JSF. As stated in my answer on that related question:

这是JSF的“烦人的设计之物”之一。正如我在该相关问题的答案中所述:

Coming back to the concrete problem, I'd imagine that this is an oversight in the JSF2 specification. It would make much more sense to us, JSF developers, when the JSF specification mandates the following:

回到具体问题,我想这是JSF2规范中的一个疏忽。当JSF规范要求以下内容时,JSF开发人员会更有意义:

  • When JSF needs to update/re-render an input component by an ajax request, and that input component is not included in the process/execute of the ajax request, then JSF should reset the input component's value.
  • 当JSF需要通过ajax请求更新/重新呈现输入组件,并且该输入组件未包含在ajax请求的进程/执行中时,JSF应该重置输入组件的值。

I only don't remember anymore if I have ever reported this against JSF spec. Edit: I reported it: JSF spec issue 1060.

如果我曾经针对JSF规范报告这个,我只是不记得了。编辑:我报告了它:JSF规范问题1060。