JSF 2.0只允许您验证一个字段的输入,例如检查它是否是一定长度.它不允许您有一个表单,"输入城市和州,或只输入邮政编码".
你是怎么得到这个的?我只对涉及JSF验证阶段的答案感兴趣.我对管理Bean中的验证逻辑不感兴趣.
我可以用一个验证器验证两个相互依赖的字段吗?
<h:form>
<h:inputText value="#{logRegBean.person.name}" >
<f:validator validatorId="loginCorrectValidator" />
</h:inputText>
<h:inputSecret value="#{logRegBean.person.password}" />
<h:commandButton action="#{logRegBean.login}" />
</h:form>
Run Code Online (Sandbox Code Playgroud)
我想在数据库中搜索用户,如果有用户,我将测试密码(在db和输入中)是否匹配.但是如何在一个验证器中访问密码字段呢?我试图通过评估其他字段中的int值createValueExpression(),但看起来我无法在那段时间访问该值,因为我总是得到空字符串.
我常常对这两个阶段产生怀疑.以下是我的理解:
应用请求值
更新模型值
我在想我的理解是正确的.但是,阅读一些文章让我感到困惑.我想在这两个阶段让我更清楚.请澄清我.
如果我正确地将 BalusC 2006 年伟大的帖子http://balusc.blogspot.ch/2006/09/debug-jsf-lifecycle.html 中包含的信息与 Optimus Prime 更早的帖子http://cagataycivici.wordpress.com/2005 /12/28/jsf_component_s_value_local/我得到以下信息:
我的理解:
问题:
在这里,它被作者提及。
如果[COMPONENT]标记为有效,则两者都返回相同的值,即提交,转换和验证的值。
考虑一个非常简单的代码段:
<h:form>
<h:inputText value="#{bean.inputValue}"
binding="#{bean.htmlInputText}"
validator="nameValidator" /><br/>
<h:commandButton value="Submit" action="#{bean.action}" />
</h:form>
Run Code Online (Sandbox Code Playgroud)
与@RequestScoped支持豆-
public Integer inputValue = 5;
public HtmlInputText htmlInputText;
public void action(){
System.out.println(" getSubmittedValue() "+htmlInputText.getSubmittedValue());
System.out.println(" isLocalValueSet() "+ htmlInputText.isLocalValueSet());
System.out.println(" getValue() " + htmlInputText.getValue());
System.out.println(" getLocalValue() " +htmlInputText.getLocalValue());
}
Run Code Online (Sandbox Code Playgroud)
按下提交按钮后,输出为-
getSubmittedValue() null AS EXPECTED, since Conversion & Validation succeded
isLocalValueSet() false
getValue() 25 AS EXPECTED, since Conversion & Validation succeded
getLocalValue() null Why NULL? IN WHAT CONTEXT HAS THE AUTHOR SAID SO
Run Code Online (Sandbox Code Playgroud)