Está en la página 1de 3

2011/3/7 ZK - ZK Developer's Reference/UI Patter…

Log in / create account

PRODUCT
DEMO
download
WHY ZK
COMMUNITY
DOCUMENTATION
SUPPORT

Google Wiki
Search
Documentation
Overview
ZK
ZK Studio
ZK Spring
ZK Spreadsheet
ZK Calendar
ZK JSP
讚 7 人說這讚。成
為你朋友中第一

ZK Developer's Reference/UI Patterns/Error Handling


From Documentation

Search this book:


TOC UI Patterns Session Timeout Management Actions and Effects
Search

Here we describe how to handle errors. An error is caused by an exception that is not caught by the
application. An exception might be thrown in two situations: when loading a ZUML document, and when serving an AU request (aka, an Ajax request).

Error Handling When Loading ZUML Documents


If an un-caught exception is thrown when loading a ZUML document, it is handled directly by the Web server. In other words, the handling is no different
from other servlets.

By default, the Web server displays an error page showing the error message and stack trace. For example,

You can customize the error handling by specifying the error page in WEB-INF/web.xml as follows[1] .

1 <!‐‐ WEB‐INF/web.xml ‐‐>


2 <error‐page>
3 <exception‐type>java.lang.Throwable</exception‐type>
4 <location>/WEB‐INF/sys/error.zul</location>
5 </error‐page>

Then, when an error occurs in loading a page, the Web server forwards the error page you specified, /error/error.zul. Upon forwarding, the Web server
passes a set of request attributes to the error page to describe what happens. These attributes are as follows.

Request Attribute Type


javax.servlet.error.status_code java.lang.Integer
javax.servlet.error.exception_type java.lang.Class
javax.servlet.error.message java.lang.String
javax.servlet.error.exception java.lang.Throwable
javax.servlet.error.request_uri java.lang.String
javax.servlet.error.servlet_name java.lang.String

Then, in the error page, you can display your custom information by use of these attributes. For example,

books.zkoss.org/wiki/…/Error_Handling 1/3
2011/3/7 ZK - ZK Developer's Reference/UI Patter…
1 <window title="Error ${requestScope['javax.servlet.error.status_code']}">
2 Cause: ${requestScope['javax.servlet.error.message']}
3 </window>

Tips:

The error page can be any kind of servlets. In addition to ZUML, you can use JSP or whatever servlet you preferred.

1. ↑ Please refer to Java Servlet Specification


(http://www.oracle.com/technology/sample_code/tech/java/codesnippet/servlets/HandlingServletExceptions/HandlingServletExceptions.html)
for more details.

Error Handling When Serving AU Requests


If an uncaught exception is thrown when serving an AU request (aka., an Ajax request; such as caused by an event listener), it is handled by the ZK
Update Engine. By default, it simply shows up an error message to indicate the error.

For example, suppose we have the following code:

1 <button label="Cause Error" onClick='throw new NullPointerException("Unknown Value")'/>

Then, if you click the button, the following error message will be shown.

You can customize the error handling by specifying the error page in WEB-INF/zk.xml as described in ZK Configuration Reference. For example,

1 <!‐‐ zk.xml ‐‐>


2 <error‐page>
3 <exception‐type>java.lang.Throwable</exception‐type>
4 <location>/WEB‐INF/sys/error.zul</location>
5 </error‐page>

Then, when an error occurs in an event listener, the ZK Update Engine creates a dialog by use of the error page you specified, /error/error.zul.

Like error handling in loading a ZUML page, you can specify multiple <error-page> elements. Each of them is associated with a different exception type
(the value of <exception-type> element). When an error occurs, ZK will search the error pages one-by-one until the exception type matches.

In addition, ZK passes a set of request attributes to the error page to describe what happens. These attribute are as follows.

Request Attribute Type


javax.servlet.error.exception_type java.lang.Class
javax.servlet.error.message java.lang.String
javax.servlet.error.exception java.lang.Throwable

For example, you can specify the following content as the error page.

1 <window title="Error ${requestScope['javax.servlet.error.status_code']}"


2 width="400px" border="normal" mode="modal" closable="true">
3 <vbox>
4 KillerApp encounters an error: ${requestScope['javax.servlet.error.message']}
5 <hbox style="margin‐left:auto; margin‐right:auto">
6 <button label="Continue" onClick="spaceOwner.detach()"/>
7 <button label="Reload" onClick="Executions.sendRedirect(null)"/>
8 </hbox>
9 </vbox>
10
11 <!‐‐ optional: record the error for improving the app ‐‐>
12 <zscript>
13 org.zkoss.util.logging.Log.lookup("Fatal").error(
14 requestScope.get("javax.servlet.error.exception"));
15 </zscript>
16 </window>

Then, when the button is clicked, the following will be shown.

books.zkoss.org/wiki/…/Error_Handling 2/3
2011/3/7 ZK - ZK Developer's Reference/UI Patter…
Tips:

The error page is created at the same desktop that causes the error, so you can retrieve the relevant information from the desktop.

Version History
Last Update : 2010/12/7

Version Date Content

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License. (http://www.gnu.org/copyleft/fdl.html)

Retrieved from "http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/UI_Patterns/Error_Handling"


Categories: Documentation | ZK Developer's Reference

Page |
Discussion |
View source |
History

What links here |


Related changes |
Special pages
| Recent changes
| Help

This page was last modified on 7 December 2010, at 03:47.

Disclaimers |
Modified by ZK

books.zkoss.org/wiki/…/Error_Handling 3/3

También podría gustarte