《JSF 2.0 Cookbook》 http://www.packtpub.com/article/jsf-20-features
In this recipe we talk about the exception handling mechanism provided by JSF 2.0. You will see how to map exceptions to error pages in the web.xml file, how to use a managed bean for extracting an exception from the request and build a String from the stack trace, and how to customize the exception handling with a user-defined exception handler.
How to do it...
We start our recipe with the simplest solution for handling exceptions. It consists in mapping exception to error pages in the web.xml descriptor . For start we add in web.xml an entry for to define a JSF page as an error page (in our example, we define an error page named error.xhtml, mapped to the java.lang.NumberFormatException exception):
...
<error-page>
<exception-type>java.lang.NumberFormatException</exception-type>
<location>/faces/error.xhtml</location>
</error-page>
...
Now, JSF keeps track of a set of values in the request that provide more details on the error page. Next you can see one of these values edited in error.xhtml:
...
User Error:
#{requestScope['javax.servlet.error.message']}
...
Now, we can test the previous example by throwing a java.lang.NumberFormatException from a bean getter method, as shown next (when the error is thrown, the error.xhtml error page is getting into action):
...
private String number = "345s";
...
public String getNumber() {
try {
Integer intnumber = Integer.valueOf(this.number);
return String.valueOf(intnumber);
} catch (NumberFormatException e) {
throw new java.lang.NumberFormatException(e.getMessage());
}
}
public void setNumber(String number) {
this.number = number;
}
...
Going further, we can write a managed bean for extracting the exception from the request and building a String from the stack trace. You can see the action that does this job for us next:
...
private String error = "";
...
public String getError() {
StringBuilder errorMessage = new StringBuilder();
FacesContext facesContext = FacesContext.getCurrentInstance();
Map<String, Object> map =
facesContext.getExternalContext().getRequestMap();
Throwable throwable = (Throwable)
map.get("javax.servlet.error.exception");
if (throwable != null) {
errorMessage.append(throwable.getMessage()).append("\n");
for (StackTraceElement element : throwable.getStackTrace())
{
errorMessage.append(element).append("\n");
}
}
this.error = errorMessage.toString();
return this.error;
}
...
To get the stack trace we use the following code in the error.xhtml page:
...
System Administrator Error: <br/>
<h:outputText value="#{bean.error}"/>
...
You can go even further and customize the exception handling. Any custom exception handler should be defined in faces-config.xml, as in the following example:
...
<factory>
<exception-handler-factory>
exception.handler.CustomExceptionHandler
</exception-handler-factory>
</factory>
...
In the custom exception handler you should override the handle method to describe the behavior of your application in the case of a particular exception or set of exceptions. The prototype of this method is:
public void handle() throws FacesException {
...//do your job here
super.handle();
}
How it works...
Basically, in all three cases described previously, the idea is the same. The exceptions are caught by the system and they are treated according to our desires. We can provide a simple error page, or we can get much deeper and exploit the exception's stack trace and create large logs with detailed information for users or for administrators.
javax.faces.webapp.FacesServlet
public void service(ServletRequest req, ServletResponse resp)
throws IOException, ServletException {
...
// Acquire the FacesContext instance for this request
FacesContext context = facesContextFactory.getFacesContext
(servletConfig.getServletContext(), request, response, lifecycle);
....
}
com.sun.faces.context.FacesContextFactoryImpl
public FacesContext getFacesContext(Object sc, Object request, Object response, Lifecycle lifecycle)
throws FacesException {
....
FacesContext ctx = new FacesContextImpl(
externalContextFactory.getExternalContext(sc, request, response), lifecycle);
ctx.setExceptionHandler(exceptionHandlerFactory.getExceptionHandler());
return ctx;
}
com.sun.faces.lifecycle.Phase
public void doPhase(FacesContext context,
Lifecycle lifecycle,
ListIterator<PhaseListener> listeners) {
....
try {
handleBeforePhase(context, listeners, event);
if (!shouldSkip(context)) {
execute(context);
}
} catch (Throwable e) {
queueException(context, e);
} finally {
try {
handleAfterPhase(context, listeners, event);
} catch (Throwable e) {
queueException(context, e);
}
....
context.getExceptionHandler().handle();
}
}
JSF 2.0 default impl
com.sun.faces.context.ExceptionHandlerFactoryImpl
public ExceptionHandler getExceptionHandler() {
FacesContext fc = FacesContext.getCurrentInstance();
if (fc.getPartialViewContext().isAjaxRequest()) {
return new AjaxExceptionHandlerImpl(new ExceptionHandlerImpl(Boolean.TRUE));
}
ApplicationAssociate associate = getAssociate(fc);
return new ExceptionHandlerImpl(((associate != null) ? associate.isErrorPagePresent() : Boolean.TRUE));
}
相关推荐
**JSF 2.0(JavaServer Faces 2.0)**是Java平台上的一种服务器端的用户界面框架,用于构建富互联网应用程序(Rich Internet Applications,RIA)。它遵循MVC(Model-View-Controller)设计模式,提供了一种声明式的...
本例中的"jsf2.0版本helloworld"就是一个基础的入门示例,旨在展示如何在MyEclipse环境中配置和运行一个简单的JSF应用。 **JSF 2.0的关键特性** 1. **Faces Flow**: JSF 2.0引入了Faces Flow,这是一种新的导航模型...
**JSF 2.0 源代码详解** JavaServer Faces (JSF) 是一个用于构建Web用户界面的Java框架,由Sun Microsystems(现已被Oracle收购)开发并维护。JSF 2.0是该框架的一个重要版本,它带来了许多改进和新特性,提升了...
JSF 2.0是其一个重要版本,带来了许多改进和新特性,使得开发更加高效且易于维护。这个压缩包包含了JSF 2.0的API jar包、实现库(即impl jar包)、源码以及doc帮助文档,为深入理解和学习JSF提供了全面的资源。 API...
在本篇中,我们将深入探讨JSF 2.0中的TextBox组件,它是用户界面中用于输入文本的基础元素。JSF(JavaServer Faces)是Oracle公司提供的一个用于构建Web应用程序的MVC框架,而JSF 2.0版本带来了许多改进和新特性,使...
文档《JSF2.0系列简介.doc》可能详细介绍了JSF 2.0的基础知识和实践指导,而压缩包中提供的源代码可能是为了演示如何在实际项目中使用JSF 2.0。这些示例可能涵盖了以下内容: - **创建基本的JSF页面**:展示如何...
JSF2.0标签手册DQSV
**JavaServer Faces 2.0 (JSF 2.0) 和 Spring 框架的整合教程** 在当今的企业级Web开发中,JSF 2.0 和 Spring 的结合使用非常常见,因为它们各自提供了独特的优势。JSF 是一个用于构建用户界面的组件模型框架,而 ...
JSF 2.0是一个重要的版本更新,引入了许多改进和新特性,旨在提升开发效率和用户体验。Mojarra是JSF规范的主要实现之一,版本2.0.2-FCS(Final Candidate Release)是该版本的一个稳定版本。 1. **组件库增强**: ...
在JSF 2.0中,Ajax功能的引入极大地增强了用户界面的交互性和实时性。这一部分我们将深入探讨如何实现自定义的Ajax更新方式,以更灵活地控制UI组件的异步更新。在这个主题中,我们将关注以下几个关键知识点: 1. **...
**JSF 2.0(JavaServer Faces 2.0)是Java EE 6平台上的一个关键组件,用于构建Web用户界面。它提供了一个模型-视图-控制器(MVC)框架,使得开发人员能够更高效地创建动态、数据驱动的Web应用程序。** **一、JSF...
在"Jsf2.0 Reference"中,我们能够深入理解JSF的核心概念和技术,这包括但不限于以下几个方面: 1. **JSF生命周期**:JSF组件有其独特的生命周期,包括六种阶段:恢复视图、应用请求值、处理验证、更新模型值、调用...
### JSF 2.0 开发资料:详细解析与实用指南 #### 一、JSF 2.0 概览 JavaServer Faces (JSF) 2.0 是 Java 平台上的一种用于构建用户界面的标准组件框架。它作为 Java EE 的一部分,在 JSF 2.0 版本中进行了大量的...
### JSF 2.0 开发入门 #### 一、简介 JSF(JavaServer Faces)2.0 是 Java 平台的一个强大的组件基础 Web 应用框架,它旨在简化 Web 应用程序的开发过程,并提高开发效率。JSF 2.0 作为 JSF 的一个重要版本,在原有...
**JSF 2.0 开发简介** JavaServer Faces (JSF) 2.0 是一个重要的里程碑,它显著改善了JSF 1.0 的功能,并吸取了来自开源社区的创新成果。JSF 2.0 的核心目标是简化Web应用程序的开发,通过引入新特性,如注解配置、...
### JSF 2.0与Ajax交互实现 #### 核心知识点概述 1. **JSF 2.0**: JavaServer Faces (JSF) 是一个用于构建基于组件的用户界面的标准Java框架。JSF 2.0是JSF的一个重大更新版本,提供了更简洁的API、增强的功能以及...
**JSF 2.0(JavaServer Faces 2.0)教程详解** JavaServer Faces (JSF) 是Java EE平台中的一个用户界面组件框架,它主要用于构建动态、数据驱动的Web应用程序。JSF 2.0是该框架的一个重要版本,引入了许多改进和新...