簡體   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