JSR303 ConstraintValidator,如何显示消息而不显示错误页面

发布于 2024-10-18 04:46:28 字数 1113 浏览 2 评论 0原文

我正在使用 JSF 2.0 和 Glassfish v3。我正在测试 JSR303 bean 验证的功能,因此我创建了一个实现 ConstraintValidator,然后在我想要验证的属性上进行注释。

它工作正常,但显示 Glassfish 默认错误页面。我不希望显示此内容,我宁愿将消息显示在 或其他内容中。

有人知道如何实现这一目标吗?

这是我的验证器方法:

@Override
public boolean isValid(String searchArg, ConstraintValidatorContext ctx) {

    boolean searchArgCorrect = true;
    FacesMessage msg;
    if(searchArg!=null) {
        ctx.disableDefaultConstraintViolation();
        if(searchArg.length() < 3) {
            ctx.buildConstraintViolationWithTemplate("Searcharg is too short").addConstraintViolation();
            searchArgCorrect=false;

            msg = new FacesMessage(
                    FacesMessage.SEVERITY_ERROR,
                    "Searcharg is too short", null);

            throw new  ValidatorException(msg);
        }
    }

    return searchArgCorrect;
}

PS:我知道有更简单的方法来验证字符串的长度,但上面的代码片段仅用于演示/测试目的。我对验证器还有另一个计划。

Im working with JSF 2.0 and Glassfish v3. I was testing the functionality of JSR303 bean validation, so I created a validator which implements ConstraintValidator and then annotate that on a property wich I want to validate.

It works fine, but it displays a Glassfish default error page. I don't want this to be displayed, I would rather have the message displayed in a <h:outputText> or something.

Does anybody know how to achieve this?

Here is my validator method:

@Override
public boolean isValid(String searchArg, ConstraintValidatorContext ctx) {

    boolean searchArgCorrect = true;
    FacesMessage msg;
    if(searchArg!=null) {
        ctx.disableDefaultConstraintViolation();
        if(searchArg.length() < 3) {
            ctx.buildConstraintViolationWithTemplate("Searcharg is too short").addConstraintViolation();
            searchArgCorrect=false;

            msg = new FacesMessage(
                    FacesMessage.SEVERITY_ERROR,
                    "Searcharg is too short", null);

            throw new  ValidatorException(msg);
        }
    }

    return searchArgCorrect;
}

PS: I know that there are easier ways to validate the length of a string, but the above code snippet is only for demo/testing purposes. I have another plans with the validator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2024-10-25 04:46:28

您混合了两个概念:JSF 验证和 JSR 303 bean 验证。您正在实现 JSR303 bean 验证,但抛出了特定于 JSF 的 ValidatorException

该方法不应引发异常。该方法应该只返回 truefalse,具体取决于验证结果。该消息必须在 ValidationMessages.properties 中定义。然后,JSF 将在与输入组件关联的 中显示它。

另请参阅此文档使用消息创建自定义约束。


或者,如果您实际上在使用标准 JSF 验证器,那么您应该实现 javax.faces.validator.Validator 相反,用 @FacesValidator 并通过 < 在视图中声明它/代码>。它可以抛出 ValidatorException 并且消息将显示在与输入组件关联的 中。

You're mixing two concepts: JSF validation and JSR 303 bean validation. You're implementing JSR303 bean validation, but you're throwing a JSF-specific ValidatorException.

The method should not throw an exception. The method should just return true or false depending on the validation outcome. The message has to be definied in ValidationMessages.properties. JSF will then display it in a <h:message> which is associated with the input component.

See also this documentation on creating custom constraints with a message.


Or if you're actually after a standard JSF validator, then you should be implementing javax.faces.validator.Validator instead, annotate it with @FacesValidator and declare it in view by <f:validator>. It can throw a ValidatorException and the message will be displayed in <h:message> associated with the input component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文