【发布时间】:2012-01-12 02:28:42
【问题描述】:
在 register.xhtml 页面中,我有 2 个inputText 密码组件并确认密码如下:
<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:
@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,它具有类似的电子邮件和确认功能验证功能,它工作得很好。但是,我不知道为什么它不能在这里工作。 passwordStr 始终是 null,即使我已经输入了密码。
如果有人能告诉我我在这里做错了什么,我将不胜感激。
最好的问候, 詹姆斯·特兰
【问题讨论】:
-
也许验证器方法应该是
validateConfirmPassword而不是validateConfirmEmail。您发布了正确的代码吗? -
@VineetReynolds:感谢您纠正我:P。我打错了代码。我已经用正确的代码更新了我的问题。
标签: java jsf-2 primefaces validation