为什么不能从组件绑定中获取提交的值? [英] Why can I not get the submitted value from component binding?
问题描述
在register.xhtml页面中,我有2个inputText
组件用于密码并确认密码,如下所示:
In register.xhtml page, I have 2 inputText
components for password and confirm password as following:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:form>
<h:outputText style="font-weight: bold" value="Password: " />
<p:password feedback="true" minLength="9"
binding="#{mrBean.passwordComponent}"
id="password" value="#{mrBean.password}"/>
<p:message for="password" id="passwordMsg" />
<h:outputText style="font-weight: bold" value="Confirm password: " />
<p:password feedback="false" minLength="9"
id="confirmPassword" value="#{mrBean.confirmPassword}"
validator="#{mrBean.validateConfirmPassword}>
<f:attribute name="oriPassword" value="#{mrBean.passwordComponent.submittedValue}"/>
<p:ajax process="password confirmPassword" update="confirmPasswordMsg" />
</p:password>
<p:message for="confirmPassword" id="confirmPasswordMsg" />
</h:form>
</html>
这是我的mrBean:
And this is my mrBean:
@ManagedBean
@RequestScoped
public class MrBean {
private String password;
private String confirmPassword;
private UIInput passwordComponent;
public void validateConfirmPassword(FacesContext context, UIComponent toValidate,
Object value) throws ValidatorException {
String passwordStr = (String) toValidate.getAttributes().get("oriPassword");
String confirmPasswordStr = (String) value;
if (!confirmPasswordStr.equals(passwordStr)) {
FacesMessage message = new FacesMessage("The 2 passwords do not match.");
throw new ValidatorException(message);
}
}
}
在另一页中,我也有一个类似的bean,它具有类似的电子邮件验证功能& ConfirmEmail,它完美地工作.但是,我不知道为什么它不能在这里工作.即使我已经输入了密码,passwordStr
始终是null
.
In another page, I also have a similar bean with similar validate function for email & confirmEmail and it works perfectly. However, I have no idea why it couldn't work here. The passwordStr
is always null
even though I have already entered the password.
如果有人可以告诉我我做错了什么,我将不胜感激.
I'd be very grateful if someone could show me what I have done wrong here.
最诚挚的问候, 詹姆斯·特兰
Best regards, James Tran
推荐答案
JSF组件按照它们在组件树中出现的顺序进行处理.在验证阶段,对于每个组件,提交的值将通过getSubmittedValue()
进行检索,转换和验证.如果在转换和验证期间未发生任何异常,则提交的值将被设置为null
,而转换后的和已验证的值将由setValue()
设置为本地值.
JSF components are processed in the order they appear in the component tree. During validations phase, for each component the submitted value will be retrieved by getSubmittedValue()
, converted and validated. If no exceptions occurred during conversion and validation, then the submitted value will be set to null
and the converted and validated value will be set as local value by setValue()
.
您正在尝试引用当时已已处理过的组件的提交值.当该值的转换/验证失败时,提交的值将仅为非null
.您需要引用其本地值.
You're trying to reference the submitted value of the component which has already been processed at that point. The submitted value will only be non-null
when conversion/validation failed for that value. You need to reference its local value instead.
<f:attribute name="oriPassword" value="#{mrBean.passwordComponent.value}"/>
另请参见:
- 如何通过ajax验证两个密码字段?(此示例反过来进行,因此您无需其他属性即可确认密码)
- How validate two password fields by ajax? (this example does the other way round, so that you don't need an additional property for confirm password)
See also:
这篇关于为什么不能从组件绑定中获取提交的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!