且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JSF AJAX验证:执行=" @这个"渲染=" @形式QUOT;不一致根据之前的请求

更新时间:2022-10-23 15:46:55

  

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

是的。


  

我做得不对,或者是这个恼人的,设计的东西关于JSF?

这是的恼人被设计的东西JSF之一。作为上相关的问题陈述我的回答是:

  

让我们回到具体的问题,我会想象这是在JSF2规范的监督。它将使更多的意义对我们来说,JSF的开发人员,当JSF规范要求如下:

     
      
  • 当的JSF需要更新/由一个AJAX请求重新渲染一个输入组件,并且该输入组件不包括在过程/ Ajax请求的执行,然后JSF应该重置输入组件的值。
  •   

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

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

I also have a text input on the same form with some validation (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.

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.

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.

I suspect what's happening is something like this:

  • with @form, radio button and text are both processed.
  • Radio button is valid so localValue is set
  • Process validations phase fails overall because of invalid text input, so localValue remains 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 localValue and then allow refreshing from 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?

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?

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
 }

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?

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

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.

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