在受管理的bean中捕获异常并在jsf2中显示屏幕上显示消息 [英] catch exception in managed bean and show message on screen in jsf2

查看:132
本文介绍了在受管理的bean中捕获异常并在jsf2中显示屏幕上显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSF 2,当我使用一些Web服务时我收到一些异常,我想抓住这个异常,并在我的 .xhtml

我该怎么做?

解决方案

您可以使用 try-catch 来捕获并处理异常。您可以使用 FacesContext#addMessage() 将面部消息添加到面部上下文中。您可以使用 < h:messages> 来显示面孔消息。



所以,在bean中这样的东西:

  try {
data = yourWebService.retrieve();
} catch(YourWebServiceException e){
String message =无法从webservice检索数据:+ e.getMessage());
FacesContext.getCurrentInstance()。addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO,message,null));
e.printStackTrace(); //或使用记录器。
}

这在JSF页面中:

 < h:messages globalOnly =true/> 

globalOnly =true将强制< h:messages> 仅显示具有 null 客户端ID的消息)


I'm using JSF 2 and I'm getting some exception while I'm using some web service and I want to catch that exception and display the message on my .xhtml page.

How can I do that?

解决方案

You can use the try-catch block to catch and handle the exception. You can use FacesContext#addMessage() to add a faces message to the faces context. You can use <h:messages> to display faces messages.

So, something like this in the bean:

try {
    data = yourWebService.retrieve();
} catch (YourWebServiceException e) {
    String message = "Failed to retrieve data from webservice: " + e.getMessage());
    FacesContext.getCurrentInstance().addMessage(null, 
        new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
    e.printStackTrace(); // Or use a logger.
}

And this in the JSF page:

<h:messages globalOnly="true" />

(the globalOnly="true" will force the <h:messages> to only show messages with a null client ID)

这篇关于在受管理的bean中捕获异常并在jsf2中显示屏幕上显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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