JSF AJAX 验证:execute="@this" render="@form" 根据先前的请求不一致

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8449838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:05:18  来源:igfitidea点击:

JSF AJAX validation: execute="@this" render="@form" inconsistent depending on prior requests

ajaxvalidationjsf

提问by wrschneider

I have an h:commandLinkthat 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:commandLinkwith execute="@this, the radio button updates from the model property as expected, because the text input is never processed and validation never fires.

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

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

但是,如果在一个不同的我首先点击h:commandLinkexecute="@form"然后与链接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 @thiswas to force everything to update from the model and ignore any submitted values from the components.

我发现令人讨厌的是,execute="@this"根据我之前所做的事情,行为会有所不同,当时我的意图@this是强制从模型更新所有内容并忽略来自组件的任何提交值。

I suspect what's happening is something like this:

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

  • with @form, radio button and text are both processed.
  • Radio button is valid so localValueis set
  • Process validations phase fails overall because of invalid text input, so localValueremains set and does not get cleared or propagate to value
  • 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 localValueand then allow refreshing from bean.
  • with @form,单选按钮和文本都被处理。
  • 单选按钮有效,因此localValue已设置
  • 由于无效的文本输入,流程验证阶段整体失败,因此localValue保持设置并且不会被清除或传播到value
  • 解决这个问题的唯一方法是显式调用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
 }

采纳答案by BalusC

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:

  • 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.

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

  • 当 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