繁体   English   中英

如何在JSF中验证唯一性并向用户发送消息?

[英]How to validate uniqueness in JSF and get a message to the user?

我需要检查应用程序中字段的唯一性。

我尝试使用Hibernate @Unique约束,但它显示了堆栈跟踪,但在用户表单中没有错误消息。

我的下一个解决方案是编写自定义验证器,但我认为有更好的解决方案。

在JSF中有更简单的方法吗?

(JSF 2.1 + RichFaces)

我为我的基本实体破解了一个快速的脏工作解决方案,用于JSF 2.1唯一密钥验证:

用法:

<p:inputText id="name" value="#{employeesController.currentEmployee.name}">
    <f:validator validatorId="uniqueColumnValidator" />
    <f:attribute name="currentEntity" value="#{employeesController.currentEmployee}" />
    <f:attribute name="uniqueColumn" value="name" />
</p:inputText>

验证器:

@RequestScoped
@FacesValidator("uniqueColumnValidator")
public class UniqueColumnValidator implements Validator, Serializable {

    @PersistenceContext
    protected EntityManager em;

    /**
     * generic unique constraint validator for AbstractBaseEntity entities<br />
     * requires the following additional attributes on the form element ("<f:attribute>"):<br />
     * - "currentEntity" the entity instance (used for getting the class and guid)<br />
     * - "uniqueColumn" the column where the new value will be checked for uniqueness
     */
    @Override
    public void validate(final FacesContext context, final UIComponent comp, final Object newValue) throws ValidatorException {

    AbstractBaseEntity currentEntity = (AbstractBaseEntity) comp.getAttributes().get("currentEntity");
    String uniqueColumn = (String) comp.getAttributes().get("uniqueColumn");

    boolean isValid = false;
    try {
        em.createQuery(
                "FROM " + currentEntity.getClass().getSimpleName()
                + " WHERE " + uniqueColumn + " = :value"
                + " AND guid <> :guid", currentEntity.getClass())
                .setParameter("value", value)
                .setParameter("guid", currentEntity.getGuid())
                .getSingleResult();
    } catch (NoResultException ex) {
        isValid = true; // good! no result means unique validation was OK!
    }

    if (!isValid) {
        FacesMessage msg = Messages.createError("must be unique", uniqueColumn);
        context.addMessage(comp.getClientId(context), msg);
        throw new ValidatorException(msg);
    }
    }
}

您可以手动验证bean的ajax请求。 手动验证的一个例子如下。

ClassValidator<TestClass> classValidator = new ClassValidator<TestClass>(
              TestClass.class );

InvalidValue[] invalidValues = classValidator
        .getInvalidValues(testObject);

for (InvalidValue invalidValue : invalidValues) {
       System. out .println( "Property Name: "
               + invalidValue.getPropertyName() + " Message: "
               + invalidValue.getMessage());
}

您可以使用<a4j:support />标记捕获ajax请求

您可以在textfield使用<f:ajax />onblur事件,并在您的支持bean中验证所有非nullmodel值。

<h:message id="errorMessageTag" for="fieldId"/>
<h:inputText id="fieldId" value="#{beanName.modelName.variableName}">
    <f:ajax event="blur" listener="#{beanName.property}" render="errorMessageTag" />
</h:inputText>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM