java JSF h:inputText 验证和 f:ajax 渲染
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4144231/
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
JSF h:inputText validation and f:ajax render
提问by lawardy
A very simple JSF applicaton:
一个非常简单的 JSF 应用程序:
- InputText element is assigned with Validator.
- f:ajax is used to render next element (phoneNumber) by using
blur
event. - PhoneNumber will only be displayed if inputText pass the validator and isValid boolean value is set to true
- InputText 元素分配有验证器。
- f:ajax 用于通过
blur
事件渲染下一个元素(电话号码)。 - 仅当 inputText 通过验证器并且 isValid 布尔值设置为 true 时才会显示 PhoneNumber
Here is the code snippet
这是代码片段
<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>
ManagedBean
托管Bean
public void validatePerson(FacesContext context, UIComponent toValidate, Object value) {
name = ((String) value).toUpperCase();
phoneNumber = "12345678";
isValid = true;
}
The problem is, for some reason, the phoneNumber is not rendered at all.
问题是,由于某种原因,电话号码根本没有呈现。
The only way that I can make it work is by changing f:ajax to render @form
我可以使它工作的唯一方法是更改 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>
Or remove rendered from phoneNumber
或者从 phoneNumber 中移除渲染
rendered="#{person.isValid}"
For some reason f:ajax with specific element Id and rendered based on managedBean Attribute cannot co-exist.
出于某种原因,具有特定元素 Id 和基于 managedBean 属性呈现的 f:ajax 不能共存。
Any idea or advice guys?
有什么想法或建议吗?
NOTE: this behaviour also happen when I use listener instead of validator
注意:当我使用侦听器而不是验证器时也会发生这种行为
回答by BalusC
The f:ajax
operates at the client side. The element which is specified in render
must be already present in the client side HTML DOM tree. Put it in for example a h:panelGroup
instead which is always rendered to the client side.
将f:ajax
在客户端运行。中指定的元素render
必须已经存在于客户端 HTML DOM 树中。把它放在例如 a 中h:panelGroup
,它总是呈现给客户端。
<h:panelGroup id="phoneLabel">
<h:outputText rendered="#{person.isValid}" value="#{person.phoneNumber}" />
</h:panelGroup>