Monday, January 20, 2014

RichFaces 4.3.x Tip of the Day: Complex RichFaces Data Tables

Introduction

I have been working on JSF tables for the various projects I have been involved with over the years. Starting in 2012, I began looking at RichFaces <rich:dataTable /> for some projects at my day job. The research into how to handle a number of complex situations has been enlightening to say the least.

The table is the most complex component in HTML. It is seemingly boundless in its extensibility. You can have multi-column headers that span multiple rows, you can multi-row cells, or multi-column cells. Tables can be displayed left-to-right, or right-to-left, top-to-bottom and vice-versa. As a result, when developing components for JSF, or any component framework, decisions must be made on how to generate them.

A couple of the component frameworks like PrimeFaces, and RichFaces allow developers to create more complex tables with more ease. However there are limitations with each of these frameworks. We trade flexibility for consistency, and this is fine in most cases.

The demonstration code in this post is about getting some of the flexibility back, or taking advantage of the flexibility that comes with a framework like RichFaces. We will gain the flexibility back, but it is a function of complexity. The examples will show you techniques for doing the "same thing" in multiple ways. For example, sorting can be done on the server, client, or a combination of both.

The question is where we put the complex bits. The answer to that question depends on you as a developer. You need to examine the problem domain, and understand the limits to the techniques presented.

Solutions

Please let me confess something. I like building HTML objects programmatically. There I said it. In this case I am trading the ease of development for flexibility. The solutions below will demonstrate the different techniques for accomplishing the same functionality. Please examine the code carefully before discounting it. I spent a lot of time playing with it to make it look simple.

The code for this project was developed using NetBeans and Apache Maven. The code was tested on GlassFish 3.1.2.2 and 4.0. It should work on other application servers, but I have not tested it on other servers. This project assumes you are using NetBeans which includes a sample database that these examples require. If you are not using NetBeans, you will need to create your own database with sample data to display some of the tables.

The code can be downloaded from Bitbucket at the link below, or in the references section at the end of the post.

richfaces-tables-poc

Dynamic Data Table with Sorting

Dynamic Table with Sorting
This example uses the binding attribute of the <rich:dataTable /> to bind our table to a CDI @ManagedBean. The bean is responsible for generating the table programmatically, and returning it back to the page. The data is sortable by column.
As you can see the page is very simple. In fact, most of the page is plumbing and navigation. The <rich:dataTable /> is the smallest part of the page. The code to generate the table is much more complex.
As you can see we have traded simplicity in the page for complexity in the @ManagedBean. If you are satisfied with this technique, lets take a look at another one.

Dynamic Data Table with Sorting Revisited

Dynamic Table
This table uses the same dynamic binding as the example above on the JSF page, but uses helper utilities to create JSF components dynamically from a library that I have written. It is a separate project that you can download (Please see references). This reduces the chances for errors creating common components, but it is still a lot of code. To check our sorting, I have made a "random" data generator for the table data for the code to sort.
The more simplified code in the @ManagedBean is shown below.
The code above was written before I added more functionality to my jsf-utils project. The new methods would shorten this considerably, but it would still be fairly complex.

Dynamic Table using JSP/JSTL Tags with JSF

JSF/JSTL Dynamic Table
Let me start this example with a warning. If you are using JSP/JSTL tags in your JSF pages, you may encounter very bad behavior. This technique should only be used as a last resort. I will not labor a point. If you don't understand why this is a bad idea, take a look at this post for links: JSF 2.x Tip of the Day: Great Blog Posts Explaining JSTL vs. JSF.
In this example, I will generate the rows and columns using <c:forEach />. This transfers a lot of the complexity to the page and away from the @ManagedBean. Since we are using <c:forEach />, our mechanism for sorting has to change. I used Query jquery.tablesorter.js to allow sorting of the headers.
As you can see we have much simpler code in the page bean. It looks like what you would expect for a normal JSF data table.

Complex Data Table Design

Complex Table Design
This table has a lot of really cool features, but the code is complex in the page, and the page bean is relatively simple.

Conclusion

RichFaces supports complex table designs, and produces nice results. The amount of work required to create dynamic data tables depends on the technique chosen, and limitations on the data being presented. There is no "one good way" to create data tables. Suffice to say that the easiest path should be chosen.

References

SourceServlet: Displaying the Source Code and Pages from a Project on your Blog

I had been working on a way to create nice examples that include a way to display the page and source code for it. I created a servlet that will display the code as plain text that I would like to share with all of you.

SourceServlet.java


Sunday, January 19, 2014

JSF 2.x: Nested Templates, Decorators, Fragments, and Includes

Ugly for Illustrative Purpose
I was looking at some JSF pages that I had been playing with to see about using decorators, fragments, etc. I thought someone might find it instructional, so I thought I would publish it before deleting the project. It demonstrates using a combination of methods to composite a JSF page into a cohesive unit.

DecoratedPage.xhtml



decoratedTable.xhtml



unorderedListTemplate.xhtml



listElements.xhtml


JSF 2.x Tip of the Day: Custom JSF AJAX Error Handling on Client

Introduction

One of the holes in JSF, in my professional judgement, is the lack of really good exception handling on the client for AJAX exceptions. A number of client frameworks like PrimeFaces, OmniFaces, and RichFaces attempt to cleanup for this shortcoming, it is still a deficiency.

The capabilities are present to make AJAX exception handling more robust. The "chemistry" is present in the framework, but it is not really standardized.

In this short example, I am demonstrating how to use the jsf.ajax.addOnError functionality to make client exception handling better. We will display at a minimum, an alert to let them know something bad has happened to their request.

Additionally, I will demonstrate how to use XPath to get additional information from the response.

Solution

The solution is to add the following code to the <head />: of the JSF page, or to an external JavaScript file that is included in the head. In my case, I am using an external JS file called jsf.ajax.handler.js that is loaded using JSF <h:outputScript />.

Here are the contents of the file.

References

JSF 2.2.x Tip of the Day: Custom JSF Exception Page including the default Facelets Exception Page

If you want to create a custom exception page in JSF and include the default exception page, you simply need to include one line of code.

This is an example of a custom exception page using the code above.

The resulting page will look like the image below. Please note that we are including the default JSF exception page.

Saturday, January 18, 2014

Java EE Tip of the Day: @WebFilter (Servlet Filter) Redirection

There is a common case to check a condition in a filter, and if the condition is not satisfied to redirect back to the context root of the application. This may be the case in a login filter, or a filter that performs some session management.

Here is a simple code example of how to perform the redirection:

HOWTO: Adding SyntaxHighlighter to Blogger

If you want to use SyntaxHighlighter in your blog, you can simply add the code below to the <head /> element in the template for your Blogger blog.

JSF 2.x Tip of the Day: Redirecting to the context path with parameters

This is just a quick code example that demonstrates how to do a redirect to the Servlet context path with parameters.

The code above will correctly format parameters to be passed as part of the redirection.

Here is an example using bogus parameters.



Here is the JSF Form I used to generate the output below.

Friday, January 17, 2014

GlassFish 4 Tip of the Day: Removing Expired GTE CyberTrust Solution Certificate

Problem

Have you seen this ugly warning message in your logs, or NetBeans IDE?

Do you want to get rid of it?

Solution

The technique to remove it is quite simple if you are using a basic GlassFish installation. This is a combination of posts from the resources listed in the references below.
  1. Go to the ${GLASSFISH_INSTALLATION_DIRECTORY}/glassfish/domains/domain1/config/ directory. You will need to repeat this if you have multiple domains.
  2. Execute the following command:
  3. Restart server, and the expired certificate warning will disappear.

References

Thursday, January 16, 2014

How do you cancel a scheduled Thread in an ExecutorService?

Executors in the Middle Ages
Humm... I thought...

I was asked this question a while ago, and I came up with a quick solution since it was just a general question. I went back and cleaned up my example which uses Callable and Future to demonstrate the concurrency libraries in Java.

In the example, I use a ExecutorService with a fixed thread pool size of two (2), to make the code slower to demonstrate the point. You can manipulate the value to see the effect on the code execution.

The code is to demonstrate you can do it,  there may be other cleaner ways of doing this. If you have an example, or idea on how to do it cleaner, please comment. :-)

Solution

The code for the project was developed using NetBeans and can be downloaded from Bitbucket at the link below. It is a NetBeans (Apache Ant) based project and should work with Ant without NetBeans.

CallOrderCancel

CallOrderCancel.java


Tuesday, January 14, 2014

Unit Testing Example with Code Coverage using Cobertura

Cobertura Coverage in NetBeans 7.4
I gave a presentation on unit testing a few months ago at Greenville Java Users Group (GreenJUG) about unit testing using JUnit and Cobertura. I finally have gotten around to publishing the code with a little cleanup.

NetBeans will detect the addition of Cobertura in the POM file and automatically add menu items for checking code coverage. This is a very slick feature in NetBeans.

The Cobertura coverage shows 90% in the project because it does not handle the generics correctly at this time.

The Apache Maven based project can be downloaded from Bitbucket below:

unit-testing-demo

Here is an example snippet of the code.

ListDataStoreImplTest.java


Friday, January 10, 2014

JSF 2.2.x Expression Language (EL) Delimiting { } in nested EL

Introduction

I was working on possible updates for our application to use JavaServer Faces 2.2 with Manfred Riem from Oracle. I mentioned that I was had tried to update to 2.2.3, but encountered an exception from EL that indicated that the statements were unbalanced. I didn't have time at that point to figure out what the issue was. It was annoying that the switch from 2.1 to 2.2 would cause such an issue, but I thought I would figure it out later. Later arrived...

There is an issue JAVASERVERFACES-2977 : Components get rendered twice we encountered that occurred when we tried to upgrade to 2.1.26. It was fixed in 2.2.5, but needs to be backported to 2.1.x branch. You can get around the issue by using 2.1.21, but Manfred suggested using JSF 2.2. I decided to give it another try, but I am also aware that I am trying to run it on an EE 6 container not EE 7. This is not an issue in the sense that the technology works, but it is not on a support matrix.

Issue

Fast forward, the EL issue appeared again. Manfred and I looked at the issue and it has the following code signature:

"#{abc:method('ABC$XYZ(current{QUANTITY})',bean.property)}"
or this in a more shortened form.
"#{'{}'}"

Manfred and I tried a number of solutions, one of which seemed to work. However, after further examination it failed to work too.
The troubling part is that existing code running on JSF 2.0, and 2.1 on Weblogic worked fine. I began to look a little deeper. I really want our application to run JSF 2.2 on the latest build. I created a simple project to work out how to delimit the EL which was causing a problem. The project can be downloaded below, or in the references section.

Code: jsf-el-delimiting
When I deployed the project to my GlassFish 4.0 server, it worked! I was really surprised. I went back to my modified GlassFish 3.1.2.2 instance which had JSF 2.2.4 running on it. It failed. So I realized that there was an issue introduced between 2.2.0 and 2.2.4. I later checked with 2.2.5 and found the same issue. I began the process of tracking down where the issue began. It looks like it started on 2.2.2.
JSF Version Passed
2.2.0 OK
2.2.1 OK
2.2.2 FAIL
2.2.3 FAIL
2.2.4 FAIL
2.2.5 FAIL

SOLUTION

The interim solution until the JIRA issue listed in the references is resolved, you can use JSF 2.2.0, or 2.2.1 in your project.

References

Popular Posts