JSR303 ConstraintValidator,如何显示消息而不显示错误页面
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您混合了两个概念:JSF 验证和 JSR 303 bean 验证。您正在实现 JSR303 bean 验证,但抛出了特定于 JSF 的
ValidatorException
。该方法不应引发异常。该方法应该只返回
true
或false
,具体取决于验证结果。该消息必须在 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
orfalse
depending on the validation outcome. The message has to be definied inValidationMessages.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 aValidatorException
and the message will be displayed in<h:message>
associated with the input component.