gpt4 book ai didi

java - JSF h :inputText validation and f:ajax render

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:11:25 25 4
gpt4 key购买 nike

一个非常简单的 JSF 应用程序:

  • InputText 元素分配了 Validator。
  • f:ajax 用于通过 blur 事件呈现下一个元素 (phoneNumber)。
  • 仅当 inputText 通过 validator 且 isValid boolean 值设置为 true 时才会显示 PhoneNumber

这是代码片段

<h:form id="invOrdersWizForm">                                  
<h:inputText id="name" maxlength="9" styleClass="ordLabelNarrow"
validator="#{person.validatePerson}"
value="#{person.name}">
<f:ajax render="phoneLabel" event="blur"/>
</h:inputText>
<h:outputText id="phoneLabel"
rendered="#{person.isValid}"
styleClass="ordLabelWide" value="#{person.phoneNumber}" />
</h:form>

受管Bean

public void validatePerson(FacesContext context, UIComponent toValidate, Object value) {
name = ((String) value).toUpperCase();
phoneNumber = "12345678";
isValid = true;
}

问题是,出于某种原因,phoneNumber 根本没有呈现。

我能让它工作的唯一方法是将 f:ajax 更改为呈现@form

<h:inputText id="name" maxlength="9" styleClass="ordLabelNarrow"
validator="#{person.validateSecurityCode}"
value="#{person.name}">
<f:ajax render="@form" event="blur"/>
</h:inputText>

或者从 phoneNumber 中移除渲染

    rendered="#{person.isValid}"

由于某些原因,具有特定元素 Id 和基于 managedBean 属性呈现的 f:ajax 无法共存。

有什么想法或建议吗?

注意:当我使用监听器而不是 validator 时也会发生这种行为

最佳答案

f:ajax 在客户端运行。 render 中指定的元素必须已经存在于客户端 HTML DOM 树中。将它放在例如 h:panelGroup 中,而不是始终呈现给客户端。

<h:panelGroup id="phoneLabel">
<h:outputText rendered="#{person.isValid}" value="#{person.phoneNumber}" />
</h:panelGroup>

关于java - JSF h :inputText validation and f:ajax render,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4144231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com