在受管理的bean中捕获异常并在jsf2中显示屏幕上显示消息 [英] catch exception in managed bean and show message on screen in jsf2
问题描述
.xhtml $ c $
我该怎么做?
解决方案您可以使用 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屋!