关于在一个字段上使用 FacesValidator(JSF 验证)和 Bean 验证的问题

发布于 2024-12-06 19:12:47 字数 477 浏览 0 评论 0原文

我有一个电子邮件字段 我想验证它是否是有效的电子邮件,并验证它在数据库中不存在,所以我在 bean 中使用了来自 hibernate 的 @Email 作为有效的电子邮件模式:

@NotBlank(message = "{email.required}")
@Email(message = "{invalid.email}")
@Size(max = 25, message = "{long.value}")

在 jsf 页面中:

<f:validator binding="#{emailExistValidator}" />

我的问题:

  1. 首先发生什么、面部验证器还是 bean 验证器?
  2. 您如何看待 bean 验证和 Bean 验证之间的这种混合? FacesValidator,这是不好的做法吗?如果是的话,你有什么建议?

i have an email field
which i want to validate that it's valid email, and validate that it doesn't exist in the database, so i used for valid email pattern the @Email from hibernate in the bean:

@NotBlank(message = "{email.required}")
@Email(message = "{invalid.email}")
@Size(max = 25, message = "{long.value}")

and in the jsf page:

<f:validator binding="#{emailExistValidator}" />

my questions:

  1. What happens first, the faces validator or the bean validation ?
  2. What do you think about this mixing between bean validation and
    FacesValidator, is it bad practice,if so what do you suggest ?

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

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

发布评论

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

评论(1

凉城已无爱 2024-12-13 19:12:47

1- 首先发生什么,是面部验证器还是 bean 验证?

JSF 转换和验证将按照在组件上声明的顺序首先运行,其中 required 首先运行。如果其中任何一个失败,则完全跳过剩余的 JSF 验证和所有 Bean 验证,否则执行所有 Bean 验证。


2-您对 bean 验证和 FacesValidator 之间的这种混合有何看法?这是不好的做法吗?如果是,您有何建议?

很难回答。它完全取决于功能需求和验证器的目的以及验证与模型或视图的紧密程度。

如果每次设置模型属性时都需要运行验证(因此,它与模型紧密相关),那么 Bean 验证将是首选。但是,如果验证只需要运行一次,例如在注册期间(因此,它与特定视图紧密相关),那么 JSF 验证将是首选。

例如,如果您正在检查电子邮件语法模式,那么 bean 验证会更有意义,例如:

@Pattern(regexp = "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)", message = "Email is not in valid format")
private String email;

如果您正在根据数据库检查电子邮件是否存在,那么 JSF 验证器会更有意义,因为您不需要它不必要在每个属性集上执行。数据库调用本身并不便宜。

再次强调,这是主观的。查看什么最适合业务需求。例如,也许您需要在每个属性集上验证它。

1- What happens first, the faces validator or the bean validation?

JSF conversion and validation will run first in the order it is been declared on the component, with required first. If any of it fails, then the remnant of JSF validation and all the bean validation is completely skipped, else all of the bean validation is executed.


2- What do you think about this mixing between bean validation and FacesValidator, is it bad practice, if so what do you suggest?

Hard to answer. It solely depends on the functional requirements and the purpose of the validator and how closely tied the validation is to the model or the view.

If the validation needs to run everytime the model property is set (so, it's closely tied to the model), then bean validation would be preferred. But if validation needs to run only once, e.g. during registration (so, it's closely tied to a specific view), then JSF validation would be preferred.

For example, if you're checking the email syntax pattern, then bean validation would make more sense, e.g.:

@Pattern(regexp = "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)", message = "Email is not in valid format")
private String email;

If you're checking the email existance against the DB, then a JSF validator would make more sense since you don't want it to unnecessarily be executed on every property set. DB calls are not cheap per se.

Again, this is subjective. See what fits the business requirements the best. Maybe you're required to validate it on every property set, for example.

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