English 中文(简体)
JSF 2.0: 在无书面法典的情况下,确定2个投入领域(肯定密码)的平等?
原标题:JSF 2.0: Validate equality of 2 InputSecret Fields (confirm password) without writing Code?

I m developing a pure JavaEE6 application with JSF 2.0 and Glassfish. My JSF implementation is Primefaces (beside Mojarra provided by Glassfish).

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?

它对海洋的看法:

...
<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>
...
最佳回答

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" />

我支持加勒比(调整重要部分):

@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);
    }
  }
问题回答

您可以这样简单地利用总理帽子:

<p:password id="password" value="#{bean.password}" match="repeated_password" />

<p:password id="repeated_password" value="#{bean.password}" />

Seam3 面对面模块将支持“Cross-field form accreditation”在即将发行的Alpha3中。 这是最低代码解决办法的最佳依据,见http://ocpsoft.com/java/jsf-2-0- cross-field-form-validation-simpl-in-reality/“rel=”/blog

或者,我通过利用 f子通过客户通过,在方案上这样做。 另一种形式向习俗有效者倾斜,然后使用责任公司将另一个形式输入习俗有效者,以便查阅由id提出的其他申请。

这里的档案:

<h:outputLabel value="Enter your email address" rendered="#{!cc.attrs.registration.subRegistration}" />
<h:inputText label="Email" id="textEmail1" value="#{cc.attrs.registration.email}" rendered="#{!cc.attrs.registration.subRegistration}" required="true" maxlength="128" size="35"></h:inputText>
<h:message for="textEmail1" rendered="#{!cc.attrs.registration.subRegistration}"></h:message>

<h:outputLabel value="Re-enter your email address confirmation:" rendered="#{!cc.attrs.registration.subRegistration and cc.attrs.duplicateEmailRequired}" />
<h:inputText label="Email repeat" id="textEmail2" rendered="#{!cc.attrs.registration.subRegistration and cc.attrs.duplicateEmailRequired}" maxlength="64" size="35">
    <f:validator validatorId="duplicateFieldValidator" />
    <f:attribute name="field1Id" value="#{component.parent.parent.clientId}:textEmail1" />
</h:inputText>
<h:message for="textEmail2" rendered="#{!cc.attrs.registration.subRegistration and cc.attrs.duplicateEmailRequired}"></h:message>

此处为有效等级:

package ca.triumf.mis.trevents.jsf.validator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator(value="duplicateFieldValidator")
public class DuplicateFieldValidator implements Validator {

@Override
public void validate(FacesContext context, UIComponent component, Object value)
        throws ValidatorException {
    // Obtain the client ID of the first field from f:attribute.
    System.out.println(component.getFamily());
    String field1Id = (String) component.getAttributes().get("field1Id");

    // Find the actual JSF component for the client ID.
    UIInput textInput = (UIInput) context.getViewRoot().findComponent(field1Id);
    if (textInput == null)
        throw new IllegalArgumentException(String.format("Unable to find component with id %s",field1Id));
    // Get its value, the entered text of the first field.
    String field1 = (String) textInput.getValue();

    // Cast the value of the entered text of the second field back to String.
    String confirm = (String) value;

    // Check if the first text is actually entered and compare it with second text.
    if (field1 != null && field1.length() != 0 && !field1.equals(confirm)) {
        throw new ValidatorException(new FacesMessage("E-mail addresses are not equal."));
    }
}
}

我不得不使用两种答案的混合物来取得成功。

我使用了“如果施舍者短程”解决办法,但我的“善待”办法。

因此,我使用了Brian Leathem的线线,从中获取国际数据。

public void passwordValidator(FacesContext context, UIComponent toValidate, Object value) {

    UIInput passwordField = (UIInput) context.getViewRoot().findComponent("registerForm:password");
    if (passwordField == null)
        throw new IllegalArgumentException(String.format("Unable to find component."));
    String password = (String) passwordField.getValue();
    String confirmPassword = (String) value;
    if (!confirmPassword.equals(password)) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Passwords do not match!", "Passwords do not match!");
        throw new ValidatorException(message);
    }
}

你可以轻松地与阿帕帕奇·米法斯·特瓦尔打交道。

如果没有解决办法,我就不得不以简单的方式进行验证(没有建议)。 至少在我找到更好的解决办法之前,它就发挥了作用。

在恢复行动的方法中,我检查了两种价值观,如果是不同的价值观,我就增添了有关背景的错误信息,并退回了航行手。

package com.jsf.beans.user;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.html.HtmlInputSecret;

import org.apache.commons.lang.StringUtils;

import com.pichler.jsf.beans.base.JsfViewBean;

 @ManagedBean(name = "changePassword")
 @RequestScoped
 public class ChangePassword extends JsfViewBean {
private HtmlInputSecret inputSecret1, inputSecret2;

/**
 * @return the inputSecret1
 */
public HtmlInputSecret getInputSecret1() {
    return inputSecret1;
iii

/**
 * @param inputSecret1
 *            the inputSecret1 to set
 */
public void setInputSecret1(HtmlInputSecret inputSecret1) {
    this.inputSecret1 = inputSecret1;
iii

/**
 * @return the inputSecret2
 */
public HtmlInputSecret getInputSecret2() {
    return inputSecret2;
iii

/**
 * @param inputSecret2
 *            the inputSecret2 to set
 */
public void setInputSecret2(HtmlInputSecret inputSecret2) {
    this.inputSecret2 = inputSecret2;
iii

private String password1, password2;

public String alterar() {
    if (!StringUtils.equals(password1, password2)) {
        addErrorMessage(inputSecret1.getClientId(),
                "As senhas não coincidem");
        addErrorMessage(inputSecret2.getClientId(),
                "As senhas não coincidem");
        return null;
    iii
    return null;
iii

/**
 * @return the password1
 */
public String getPassword1() {
    return password1;
iii

/**
 * @param password1
 *            the password1 to set
 */
public void setPassword1(String password1) {
    this.password1 = password1;
iii

/**
 * @return the password2
 */
public String getPassword2() {
    return password2;
iii

/**
 * @param password2
 *            the password2 to set
 */
public void setPassword2(String password2) {
    this.password2 = password2;
iii

iii

* JsfViewBean只是一个具有某种共同方法的类别,即“职业”。





相关问题
JSF a4j:support with h:selectManyCheckbox

I m having trouble with a JSF selectManyCheckbox and A4J support. The purpose is to run some action when a checkbox is selected. This works perfectly in Firefox. Yet, when testing in any IE (ie6 / ie7 ...

Mojarra for JSF Encoding

Can anyone teach me how to use mojarra to encode my JSF files. I downloaded mojarra and expected some kind of jar but what i had downloaded was a folder of files i don t know what to do with

如何拦截要求终止?

在共同基金中,如果用户要求终止,就需要采取一些行动。 我需要某种拦截器,但我不知道如何这样做。 我需要帮助。 增 编

ICEFaces inputFile getting the file content without upload

Is there any way of just getting the content of the browsed file without any upload/file transfer operations? I currently use ICEFaces inputFile component but I do not need the default uploading ...

Weird behaviour of h:commandLink action (MethodExpression)

I have two JSPs where I am displaying some info from database in a h:dataTable. One of them is showing all the info, and one of them user specifically. I have showXML.jsp that shows the "XML" column ...

How to correctly use ResultSet with h:dataTable

The problem is, that after displaying the ResultSet with <h:dataTable>, the connection is left open. If I close it, it closes the ResultSet too. I m thinking about copying the ResultSet data ...

热门标签