在 JSF 转换/验证机制之外的操作方法中创建 FacesMessage? [英] Creating FacesMessage in action method outside JSF conversion/validation mechanism?

查看:16
本文介绍了在 JSF 转换/验证机制之外的操作方法中创建 FacesMessage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从核心 jsf 2.0 书籍 + glassfish + cdi 中学习 jsf 2.0.

我想问一个问题,关于处理未在 jsf 页面中定义的验证或使用 bean-validation-framework 的托管/命名 bean.我脑子里有这些层次:

  • 1) ui 层/jsf 页面
  • 1.5) jsf 托管/命名 bean(我使用 1.5,因为我认为它仍然与 jsf 层紧密耦合,就像支持 bean)
  • 2) 业务逻辑层(从 jsf 的东西/导入中清除,只做纯业务逻辑的东西)
  • 3) 持久层

我想象第 1.5 层(jsf bean)初始化和调用第 2 层(业务逻辑对象),在调用业务方法时提供参数,获取结果,将结果填充到 jsf bean 属性中,以便 ui 可以正确呈现.

令我好奇的是,第 2 层(业务逻辑对象)可以对提供的参数进行验证,或验证数据等,并可能抛出异常或错误对象.

我想我可以处理异常并在 1.5 层(jsf 托管 bean)中获取错误对象,但是我应该如何在呈现的页面中显示错误?我似乎无法从我正在阅读的书中找到它,但我希望有一种方法可以创建全局错误消息,并且可以以某种方式将其注入某个地方,以便它由标签呈现?

谢谢!

解决方案

您可以使用 FacesContext#addMessage() 添加FacesMessage 以编程方式发送到上下文.

FacesContext facesContext = FacesContext.getCurrentInstance();FacesMessage facesMessage = new FacesMessage("这是一条消息");facesContext.addMessage(null, facesMessage);

当您使用 null 设置客户端 ID 参数时,它将成为全局消息.您可以使用 <h:messages/>

显示和过滤它们

globalOnly="true" 将仅显示带有 null 客户端 ID 的消息.

不过,您也可以指定特定的客户端 ID.

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

然后这个会在

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

I'm currently learning about jsf 2.0 from core jsf 2.0 book + glassfish + cdi.

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. I got these tiers in my head :

  • 1) ui tier / jsf pages
  • 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)
  • 2) business logic tier (which are clean from jsf stuffs / imports, doing only pure business logic stuffs)
  • 3) persistence tier

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.

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.

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 ? 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 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. You can display and filter them using <h:messages />

<h:messages globalOnly="true" />

The globalOnly="true" will display only messages with a null client ID.

You can however also specify a specific client 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" />

这篇关于在 JSF 转换/验证机制之外的操作方法中创建 FacesMessage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆