JSF 从常规 bean 方法中抛出 FacesMessage
在 JSF 的自定义转换器和验证器中,您可以这样说:
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Invalid length!",
"Length = 8");
throw new ConverterException(message);
ti 将消息发送到由转换器或验证器确定的某个 h:message 组件。有没有一种方法可以在常规 bean 方法中执行类似的操作(如果是这样,如何确定 h:message 组件将呈现消息)或者更好的方法是在自定义验证器中完成所有验证工作并留下业务不检查 bean 方法的逻辑。
In JSF's custom convertors and validators you can say:
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Invalid length!",
"Length = 8");
throw new ConverterException(message);
and ti sends the message to some h:message component determined from the convertor or validator. Is there a way to do something like that in regular bean method(and if so, how to determine the h:message component witch will render the message) or the better way is to do all the validation work in custom validators and to left business logic with no checks for the bean methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,有时你不能只依靠验证器,而是需要在 bean 中进行一些验证。 (大多数时候,当您一起验证多个输入的数据时,您需要这种验证)
对于这种情况,如果页面上有
h:messages
控件,则会显示错误:如果您想按组件显示验证消息而不是每页使用常规消息,请参阅这个答案。
Ps:请注意,如果您使用
h:messages
,它还会显示其他组件验证消息。如果您不想要此设置globalOnly="true"
Of course that sometimes you cannot only count on Validators but need to do some validation in bean. (most of the times you need this kind of validation when you are validating data for multiple inputs together)
For this case, if you have an
h:messages
control on the page, this will show the error there:If you want to display validation messages per components and not using a general one per page, see this answer.
Ps: Pay attention that if you use
h:messages
it will also display other components validation messages also. If you do not want this setglobalOnly="true"