JSF 2.0:验证2个InputSecret字段的相等性(确认密码)而不编写代码? [英] JSF 2.0: Validate equality of 2 InputSecret Fields (confirm password) without writing Code?
问题描述
我正在使用JSF 2.0和Glassfish开发纯JavaEE6应用程序。
我的JSF实现是Primefaces(除了由Glassfish提供的Mojarra)。
I'm developing a pure JavaEE6 application with JSF 2.0 and Glassfish. My JSF implementation is Primefaces (beside Mojarra provided by Glassfish).
我想验证JSF表单中2个密码字段的值是否相等。
使用Seam,有一个整洁的组件< s:validateEquality for =pw1/>
。
我想在没有Seam的情况下做同样的事情,只需使用JSF(或者可能是JSF库的一个组件)。到目前为止,我只看到了使用自定义验证器验证表单的示例。但我想比较这些字段而不编写Java代码或Javascript代码。
这可能吗?
I want to verify if the values of 2 password fields in a JSF form are equal.
With Seam, there is the neat component <s:validateEquality for="pw1"/>
.
I want do to the same without Seam, just using JSF (or maybe a component of a JSF library). Until now i only saw examples which validate the form with a custom validator. But i would like to compare the fields without writing Java code or Javascript code.
Is that possible?
这与Seam一样:
...
<h:inputSecret id="passwort" value="#{personHome.instance.password}"
redisplay="true" required="true">
<f:validateLength minimum="8"/>
<a:support event="onblur" reRender="passwortField" bypassUpdates="true" ajaxSingle="true" />
</h:inputSecret>
...
<h:inputSecret id="passwort2" required="true" redisplay="true">
<!-- find the JSF2.0-equivalent to this tag: -->
<s:validateEquality for="passwort"/>
<a:support event="onblur" reRender="passwort2Field" bypassUpdates="true" ajaxSingle="true" />
</h:inputSecret>
...
推荐答案
这就是方式我终于做到了,我喜欢它,因为它很简单。唯一的问题是它不是真的可以重复使用,但因为我只需要在一个案例中使用它,我宁愿保存一些LOC并以这种方式执行。
我认为的片段:
This is the way i finally did it, which i like cause it's short and easy. The only problem is that it's not really re-usable, but as i only need this in one case, i rather save some LOCs and do it this way. Snippet from my view:
<h:inputSecret id="password" value="#{personHome.person.password}">
<f:ajax event="blur" render="passwordError" />
</h:inputSecret>
<h:message for="password" errorClass="invalid" id="passwordError" />
<h:inputSecret id="password2" validator="#{personHome.validateSamePassword}">
<f:ajax event="blur" render="password2Error" />
</h:inputSecret>
<h:message for="password2" errorClass="invalid" id="password2Error" />
我的支持Bean(只是重要部分):
My Backing Bean (just the important part):
@Named @ConversationScoped
public class PersonHome {
private Person person;
public Person getPerson() {
if (person == null) return new Person();
else return person;
}
public void validateSamePassword(context:FacesContext, toValidate:UIComponent, value:Object) {
String confirmPassword = (String)value;
if (!confirmPassword.equals(person.getPassword()) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Passwords do not match!", "Passwords do not match!")
throw new Validatorexception(message);
}
}
这篇关于JSF 2.0:验证2个InputSecret字段的相等性(确认密码)而不编写代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!