0

我创建了一个休眠自定义约束来匹配字段,但我无法在相应的 FieldMatch上显示错误

package org.andreadorigo.webapp.validators.costraints;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

import org.andreadorigo.webapp.validators.FieldMatchValidator;

@Target({TYPE, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface FieldMatch
{
    String message() default "{constraints.fieldmatch}";

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

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

    /**
     * @return The first field
     */
    String first();

    /**
     * @return The second field
     */
    String second();

    /**
     * Defines several <code>@FieldMatch</code> annotations on the same element
     *
     * @see FieldMatch
     */
    @Target({TYPE, ANNOTATION_TYPE})
    @Retention(RUNTIME)
    @Documented
            @interface List
    {
        FieldMatch[] value();
    }
}

字段匹配验证器

package org.andreadorigo.webapp.validators;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.andreadorigo.webapp.validators.costraints.FieldMatch;
import org.apache.commons.beanutils.PropertyUtils;

public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object>
{
    private String firstFieldName;
    private String secondFieldName;

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

    @Override
    public boolean isValid(final Object value, final ConstraintValidatorContext context)
    {
        try
        {
            final Object firstObj = PropertyUtils.getProperty(value, firstFieldName);
            final Object secondObj = PropertyUtils.getProperty(value, secondFieldName);

            return firstObj == null && secondObj == null || firstObj != null && firstObj.equals(secondObj);
        }
        catch (final Exception ignore)
        {
            // ignore
        }
        return true;
    }
}

messages_it.properties

constraints.fieldmatch=I campi richiesti non combaciano
constraints.fieldmatch.confirmEmail=Le mail inserite non coincidono
constraints.fieldmatch.retypePassword=Le password inserite non coincidono

人物类

package org.andreadorigo.webapp.entities.abstracts;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import javax.validation.constraints.Size;

import org.andreadorigo.webapp.validators.costraints.FieldMatch;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

@MappedSuperclass
@FieldMatch.List({
    @FieldMatch(first = "password", second = "retypePassword"),
    @FieldMatch(first = "email", second = "confirmEmail")
})
public abstract class Person {
.
.
.

}

userForm.jps

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<spring:message code="label.test"/>

<form:form method="post" action="/user/add/" commandName="user" >  
    <table>
        <tr>
            <td>Email</td>
            <td><form:input path="email" /></td>
            <td><form:errors path="email"></form:errors></td>
        </tr>
        <tr>
            <td>Confirm Email</td>
            <td><form:input path="confirmEmail" /></td>
            <td><form:errors path="confirmEmail"></form:errors></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><form:input path="password" /></td>
            <td><form:errors path="password"></form:errors></td>
        </tr>
        <tr>
            <td>Retype Password</td>
            <td><form:input path="retypePassword" /></td>
            <td><form:errors path="retypePassword"></form:errors></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="Add"/>
            </td>
        </tr>
    </table>
</form:form>

</body>
</html>

但是我只能在使用时收到错误消息

4

0 回答 0