0

我的应用程序在这里遇到了一个小问题。我想检查字段密码和确认密码是否匹配,所以我尝试像在这个问题的第一个答案中那样做:Cross field validation with Hibernate Validator (JSR 303)

问题是它实际上不起作用,我不知道为什么。请帮我!这是我在这里的第一篇文章,所以请不要对我太苛刻。

这是我的注释:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints;

import java.lang.annotation.*;
import javax.validation.Constraint;
import javax.validation.Payload;

/**
 *
 * @author lukasz
 */
@Documented
@Constraint(validatedBy = FieldMatchValidator.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldMatch {

String message() default "{pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints.FieldMatch}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

String first();

String second();
}

这是我的验证器类:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints;

import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.apache.commons.beanutils.BeanUtils;

/**
 *
 * @author lukasz
 */
public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object> {

    private String firstFieldName;
    private String secondFieldName;

    @Override
    public void initialize(FieldMatch constraintAnnotation) {
        firstFieldName = constraintAnnotation.first();
        secondFieldName = constraintAnnotation.second();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        try {
            String sFirstField = BeanUtils.getProperty(value, firstFieldName);
            String sSecondField = BeanUtils.getProperty(value, secondFieldName);
            if(sFirstField.equals(sSecondField)){
                return true;
            }
        } catch (IllegalAccessException ex) {
            Logger.getLogger(FieldMatchValidator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
        Logger.getLogger(FieldMatchValidator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
        Logger.getLogger(FieldMatchValidator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}

这是我的豆子:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.lodz.p.zsk.ssbd2012.ssbd12.Beans;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints.CheckEmail;
import pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints.FieldMatch;
import pl.lodz.p.zsk.ssbd2012.ssbd12.entities.Account;
import pl.lodz.p.zsk.ssbd2012.ssbd12.mok.endpoint.MokEndpointLocal;

/**
 *
 * @author krzys
 * @author lukasz
 */

@ManagedBean
@RequestScoped
@FieldMatch(first = "password", second = "password2", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.ValidationConstraints.FieldMatch}")
public class RegisterBean {

    /**
     * Creates a new instance of RegisterBean
     */

    @EJB
    MokEndpointLocal endpoint;

    @Size(min = 3, max = 16, message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.login.size}")
    @Pattern(regexp = "[a-zA-Z0-9]+", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.login.invalid}")
    private String login;

    @Size(min = 6, max = 64, message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.password.size}")
    @Pattern(regexp = "[a-zA-Z0-9]+", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.password.invalid}")
    private String password;

    private String password2;

    @Pattern(regexp = "[A-Ż][a-ż]+", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.name.invalid}")
    private String name;

    @Pattern(regexp = "[A-Ż][a-ż]+", message = "{pl.lodz.p.zsk.ssbd2012.ssbd12.Beans.RegisterBean.surname.invalid}")
    private String surname;

    @CheckEmail
    private String email = "";


    /**
     * @return the login
     */
    public String getLogin() {
        return login;
    }

    /**
     * @param login the login to set
     */
    public void setLogin(String login) {
        this.login = login;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String register ()
    {
//       Account account = new Account();
//       account.setLogin(login);
//       account.setHaslo(password);
//       account.setImie(name);
//       account.setNazwisko(surname);
//       account.setEmail(email);
//       endpoint.register(account);
       return "registerSucces";
    }

    /**
     * @return the surname
     */
    public String getSurname() {
        return surname;
    }

    /**
     * @param surname the surname to set
     */
    public void setSurname(String surname) {
        this.surname = surname;
    }
}

这是JSF:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <h:outputStylesheet name="menu.css" library="css" />
    <title>#{messages.registration}</title>
</h:head>

<h:body>
    <ui:composition template="./../resources/mainTemplate.xhtml">

        <h4>#{messages.registration}</h4>
    <ui:define name="content">
        <h4>#{messages.registration}</h4>
        <h1>#{messages.registrationInfo1}</h1>
    #{messages.registrationInfo2}
    <h:form>
        <h2>
            <h:outputText value="#{messages.loginForm}"/>
            <h:inputText id="login" value="#{registerBean.login}" />  
        </h2>
        <h2>
            <h:outputText value="#{messages.passwordForm}" />
            <h:inputSecret id="password" value="#{registerBean.password}" />
        </h2>
        <h2>
            <h:outputText value="#{messages.repeatPasswordForm}"/> 
            <h:inputSecret id="confirm" value="#{registerBean.password2}" />
        </h2>
        <h2>
            <h:outputText value="#{messages.nameForm}"/>
            <h:inputText id="name" value="#{registerBean.name}" />
        </h2>
        <h2>
            <h:outputText value="#{messages.surnameForm}"/>
            <h:inputText id="surname" value="#{registerBean.surname}" />
        </h2>
        <h2>
            <h:outputText value="#{messages.emailForm}"/>
            <h:inputText id="email" value="#{registerBean.email}" />
        </h2>
        <h2>    
            <h:commandButton value="#{messages.send}" action="#{registerBean.register()}" />
        </h2>
    </h:form>
    </ui:define>
    </ui:composition>
</h:body>
</html>
4

1 回答 1

5

JSF 在验证阶段不会自动触发类级别的约束。您只能使用字段级别的约束(此外,并非所有字段都由 JSF 评估,而只是那些在您的 facelet 中的字段)。

如果要使用 bean 验证,可以手动执行验证:

Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Test>> violations = validator.validate( this, Default.class );

您可以在 bean 的注册方法或更新模型阶段之后执行此操作(但我从未尝试过)。

无论如何,您可以使用 JSF 验证而不是 bean 验证或直接在注册方法中检查密码:

public String registration() {
    ...

    if ( !password.equals(password2) ) {
        FacesContext.getCurrentInstance().addMessage( null, new FacesMessage( "Passwords do not match" ) );
        return null;
    }

    ...
}
于 2012-08-16T14:54:06.283 回答