ZK Exception Handling and Error Popup

Once I had some basic functionality in my mini accounting system that I’m writing (for research purposes), one of my goals was to have an adequate error display for the user, that was the same every time.  Suffice it to say, I was completely thrilled to find out that ZK could do exactly what I wanted to do, and I didn’t have to write an once of code.  All I did was write some ZUL, which wrapped a JSP inside of a ZK window.  I share how I did that in this post.


The first thing to do is to configure ZK to handle your unhandled exceptions, and target a ZUL page for rendering.  This is done through the zk.xml file.


ZK error.zul

The second thing to do is create an error.zul page, which I pulled mostly from the ZK Developers Guide.  I added the bit where I include my error.jsp file.

<?xml version="1.0" encoding="UTF-8"?>
<zk xmlns="http://www.zkoss.org/2005/zul"
    xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">
  <window mode="modal" sizable="true"
          title="Error ${requesScope['javax.servlet.error.status_code']}">
        <include src="/jsp/error.jsp"/>
      <hbox style="margin-left:auto; margin-right:auto; text-align: center;">
        <button label="Okay" onClick="spaceOwner.detach()"/>
        <button label="Reload Page" onClick="Executions.sendRedirect(null)"/>

JSP error.jsp

I basically pulled this error page from the standard error page that I normally use, and tailored it a bit.

<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.io.StringWriter" %>
<%@ page import="com.example.util.Util" %>
<%@ page isErrorPage="true" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="ce" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://jakarta.apache.org/taglibs/string-1.1" prefix="st" %>
<!-- BEGIN error.jsp -->
<div class="error">
  <p class="message"><%=exception.getMessage()%></p>
  <p>An error has occured. If you believe this error is a system problem, and you
  continue to get this error, please contact a special person via
    <a href="mailto:'me@example.com' ">email</a>
  <ce:if test="<%=Util.getBooleanItem(\"/debug\")%>">
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
    <ce:set var="exceptionStack"><%=sw.toString()%>
    <div class="debug">
      Message : <%=exception.getMessage()%>
      Exception : <%=exception.getClass().getName()%>
      Stack : ${fn:escapeXml(exceptionStack)}
<!-- END error.jsp -->

Sample Error Page

And, we have the result;  it looks quite nice.  If debug were enabled, it would have my stack trace displayed in a wrap-able fixed width font ; providing much easier diagnostics during development.

ZK Sample Error Page

3 thoughts on “ZK Exception Handling and Error Popup

  1. Senthil

    Great. I need some more information to be shown in the error popup for the development people. I mean , can we include one more button say “Details”, when we click on details, can we show the stack of messages


Leave a Reply

Your email address will not be published. Required fields are marked *