我有一个 h:commandLink
它调用一个方法来更改绑定(bind)到 radio 输入的属性。
我在同一个表单上也有一个文本输入,并带有一些验证(required=true)。
如果我将文本输入留空,然后单击 h:commandLink
与 execute="@this
,单选按钮按预期从模型属性更新,因为文本输入永远不会被处理并且验证永远不会触发。
但是,如果首先我点击不同的 h:commandLink
与 execute="@form"
,然后是 execute="@this"
的链接,验证消息消失但单选按钮值不会从模型更新,即使单选按钮的 UIInput 从未处于无效状态。
我发现 execute="@this"
很烦人当我的意图是 @this
时,行为会有所不同,具体取决于我之前所做的事情。是强制从模型中更新所有内容,并忽略来自组件的任何提交的值。
我怀疑正在发生的事情是这样的:
@form
, 单选按钮和文本都被处理。 localValue
设置localValue
保持设置,不会被清除或传播到 value
resetValue()
或重新处理有问题的组件(例如 execute="@this radio"
)以清除 localValue
然后让 bean 刷新。 我的问题是:
感觉这可能只是这个问题的另一个例子
How can I populate a text field using PrimeFaces AJAX after validation errors occur?
不幸的是,我觉得我最近发现了很多这样的东西。 :-(
下面的代码示例:
<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>
还有这个 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?
是的。
Am I doing something wrong, or is this one of the annoying-by design things about JSF?
这是 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.
我只是不记得我是否曾经针对 JSF 规范报告过这个问题。编辑:我报告了它: JSF spec issue 1060 .