简体   繁体   English

在JSF转换/验证机制之外的action方法中创建FacesMessage?

[英]Creating FacesMessage in action method outside JSF conversion/validation mechanism?

I'm currently learning about jsf 2.0 from core jsf 2.0 book + glassfish + cdi. 我目前正在从核心jsf 2.0 book + glassfish + cdi学习jsf 2.0。

I would like to ask a question about handling validations that are not defined in the jsf pages or managed/named beans with bean-validation-framework. 我想问一个关于使用bean-validation-framework处理未在jsf页面或托管/命名bean中定义的验证的问题。 I got these tiers in my head : 我脑子里有这些层次:

  • 1) ui tier / jsf pages 1)ui tier / jsf页面
  • 1.5) jsf managed / named beans (i use 1.5, because i think it's still tightly coupled with the jsf tier, like the backing beans) 1.5)jsf托管/命名bean(我使用1.5,因为我认为它仍然与jsf层紧密耦合,如支持bean)
  • 2) business logic tier (which are clean from jsf stuffs / imports, doing only pure business logic stuffs) 2)业务逻辑层(从jsf stuffs / imports中清除,只做纯业务逻辑的东西)
  • 3) persistence tier 3)持久层

I imagine tier 1.5(jsf bean) initializing and calling tier 2(business logic objects), supplying arguments when calling business methods, fetching result, populating the result into jsf bean properties, so that the ui could render correctly. 我想象层1.5(jsf bean)初始化和调用第2层(业务逻辑对象),在调用业务方法时提供参数,获取结果,将结果填充到jsf bean属性中,以便ui可以正确呈现。

What im curious is the fact that the tier 2(business logic objects) could do validations on the supplied arguments, or validating data, etc, and could throw exceptions or error objects. 令人好奇的是,第2层(业务逻辑对象)可以对提供的参数进行验证,或者验证数据等,并且可能抛出异常或错误对象。

I think i could handle the exceptions and get the error objects in the tier 1.5(jsf managed beans), but how am i supposed to display the error in the rendered pages ? 我想我可以处理异常并获取层1.5(jsf托管bean)中的错误对象,但是我应该如何在呈现的页面中显示错误? I cant seem to find it from the book im reading, but i'm hoping there's a way to create a global error message and somehow could inject it into somewhere so that it gets rendered by the tag ? 我似乎无法从我正在阅读的书中找到它,但我希望有一种方法来创建一个全局错误消息,并以某种方式可以将它注入某个地方,以便它由标记呈现?

Thank you ! 谢谢 !

You can use FacesContext#addMessage() to add a FacesMessage to the context programmatically. 您可以使用FacesContext#addMessage()以编程方式将FacesMessage添加到上下文中。

FacesContext facesContext = FacesContext.getCurrentInstance();
FacesMessage facesMessage = new FacesMessage("This is a message");
facesContext.addMessage(null, facesMessage);

When you set the client ID argument with null , it will become a global message. 将客户端ID参数设置为null ,它将成为全局消息。 You can display and filter them using <h:messages /> 您可以使用<h:messages />显示和过滤它们

<h:messages globalOnly="true" />

The globalOnly="true" will display only messages with a null client ID. globalOnly="true"将仅显示具有null客户端ID的消息。

You can however also specify a specific client ID. 但是,您也可以指定特定的客户端ID。

facesContext.addMessage("formid:inputid", facesMessage);

This one will then end up in 然后这个将结束

<h:form id="formid">
    <h:inputText id="inputid" />
    <h:message for="inputid" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM