Thursday, January 24, 2013

JSF 2.x Tip of the Day: AJAX Redirection from @WebFilter (Filter)

I was working on an application where I needed to have it redirect to a login page when certain conditions exist in the application, e.g. session timeout, etc. A ViewExpiredException custom exception handler (available in JSF 2.0) can handle this case, but I had a need for another type of "Session" object to be monitored to determine if I should redirect based on its status. The other object was stored in the HttpSession object as an attribute so I decided to handle it with a Filter (@WebFilter).

The first thing is to determine if the request is a partial/ajax request. If it is a normal post, we can handle it with a HttpResponse.sendRedirect(String location) mechanism. If it is AJAX, we need to handle it in a completely different manner. Once I determined that the request was AJAX, I needed to be able to pass the appropriate response back to the JSF page in a format that it could understand. A great tip came from Jim Driscoll's blog: Redirecting from a JSF 2.0 Ajax Request which gave me the general syntax for what I needed to send back.

Note: This is being intercepted in a Filter so I don't have access to the FacesContext. Here is a partial code snippet of how to send the redirect. You would need to set the variable TARGET to go to the desired location.

Monday, January 14, 2013

GlassFish 3 Tip of the Day: Using JDK 7 with JSP Code

A question came up on the NetBeans J2EE Mailing List about using JDK 7 with GlassFish 3.1.2. Specifically, they were getting the error:
org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP
PWC6197: An error occurred at line: 4 in the jsp file: /index.jspPWC6199: Generated servlet error:strings in switch are not supported in -source 1.5  (use -source 7 or higher to enable strings in switch)

The fix is quite simple. You must include a glassfish-web.xml file in your project, and set a couple of properties. compilerSourceVM and compilerTargetVM.

Please see the example below for a complete configuration.
The project will now compile and use JDK7.

Friday, January 11, 2013

JSF 2.x Tip of the Day: RichFaces Table Sorting (3-position switch design)

A technique I like to call the three position switch is what I use for sorting on my RichFaces tables. This consists of default (unsorted), ascending, and descending. It is implemented very easily by setting the <rich:column/> sortOrder attribute to point to our current sort, and using an <a4j:commandLink> in the header facet to control the sort as shown in the example below. This simple technique makes sorting simple and sexy.


This additional code will let the user know which sorting direction we are currently using.

JSF 2.x Tip of the Day: Programmatically Creating a <h:outputText/>

In this tip of the day we create one of the most basic components in our JSF arsenal: <h:outputText>.
This example uses another library called jsf-utils to handle the heavy lifting.

JSF 2.x Tip of the Day: RichFaces Programmatically Creating an <a4j:commandLink/>

This example of how to create an <a4j:commandLink/> uses another library called jsf-utils to handle the heavy lifting.

The method below will create the <a4j:commandLink/> component, any additional configuration will need to be performed programmatically, or using the tag attributes.

You would bind it to a backing bean to a UICommandLink component;

Thursday, January 10, 2013

JSF 2.x Tip of the Day: Determining the Base URL

This question has come up a couple of times in the last few days, so I thought I would write a couple of convenience methods to determine the Base URL.

I added them to my jsf-utils project for those who want to use my utilities.

Wednesday, January 09, 2013

JSF 2.x Tip of the Day: Displaying Content Conditionally by FacesMessage.Severity

Many times we do, or do not want to display content based on whether there are FacesMessage queued to be displayed. The FacesMessage.Severity can be used to help us determine if messages should be displayed. Please see the table below.

FacesMessage Values
Ordinal Severity

The examples below demonstrate a couple of techniques to determine if a component (in this case a <ui:fragment/> containing a <ui:include/>) should be displayed based on whether FacesMessage.Severity are present and if they are not equal to an informational message.

This is equivalent to the listing above.
This displays if the messages are WARN or less.

Tuesday, January 08, 2013

Internet Explorer 9 (IE9) Table White Space Issues

I was tasked with fixing a problem in our product where one of our data tables (HTML) was rendering on IE9 with spaces randomly scattered throughout the table this resulted in the data being in the wrong columns, or headers appearing in the wrong place. I originally thought I might there might be "holes", or null values in my data which was resulting in the error. I was wrong.

It is actually a bug in IE9.

The issue shows up, most often, when using AJAX when there is partial page rendering. It seems according to forum remarks to be focused on white space between table tag elements like line breaks, spaces, or carriage returns. So if you use HTML tidy, you will screw up your output. Nice one Microsoft!

Fortunately, there are "fixes" out there to help you get along. Here is the fix which I slightly modified from an answer on stackoverflow. A shout out goes to Blago for his recursive function listed below.

You can implement it this way.
Here are some references on the issue if you are interested.

Monday, January 07, 2013

RichFaces 4.2.x: Unique <rich:dataTable/> Design

In my current job, I have opportunities to do Proof-of-Concept for various new JSF ideas for our products. I often do a lot of these general experiments on my own time after hours at home. That is not unusual I would imagine for most developers. A couple of months ago I was tasked with trying to generate a table dynamically using RichFaces 4. In RichFaces 3, you could provide dynamic columns and have it generate the table columns and data dynamically. This functionality is currently not available in RichFaces 4.2.

I came up with a strange amalgamation of bed-mates which worked. It is an interesting approach to getting around various problems encountered developing tables.

An additional requirement was the ability to sort the data displayed in the table. Since our application at work does BI analytics, doing server side sorting is much slower and expensive than doing it client side. This problem was partially solved with JQuery and a plugin called TableSorter. I think it will work on most basic tables, and this one... well is no different.

Are you ready for some magic?

Data Table Design #3

Now that you have a chance to look at the XHTML file, I am sure you noticed some strange things like a mix of <c:forEach/> wrapping a number of <rich:column/> components inside a <rich:dataTable/>. This is the normal configuration that you might see. Also, we have added a <rich:jQuery/< component along with our JavaScript. Yes, it works. The data is pulled from JPA and displayed. It also elegantly sorts on all of the columns. The only strange code is the EL code in the <c:forEach/> for the items attribute. Since the <c:forEach/> is expecting an array, or a Collection (List) to iterate over, we must provide it a customer in that format.

I thought about deleting the design, but it actually works and may help someone solve a problem so I am publishing it.

I will publish all of the other designs once they are cleaned up in project for download.

Sunday, January 06, 2013

JSF 2.1 Tip of the Day: Programmatically Creating a <h:commandButton/>

I have been doing a lot of work on JSF lately including making custom components. It seems like the whole process of creating custom components looks like some arcane arts. It almost seems like black magic in some cases.

I thought I would share some of the things I have discovered over time in an attempt to help others avoid some of the pitfalls I have come across.
This JSF Tip of the Day is an amalgamation of a couple of tips: JSF 2.x Tip of the Day: Programmatically Creating EL ValueExpression and JSF 2.x Tip of the Day: Programmatically Creating EL MethodExpression which are combined with this tip.

In this tip we will create a <h:commandButton/> programmatically. The code from the previous tips noted, are used to create the component.

The component is bound using the following:

Saturday, January 05, 2013

Using GlassFish 3.1.1 Embedded with JUnit 4.x and HtmlUnit 2.x


I was looking for an example of how to use GlassFish Embedded Maven plugin to integrate with HtmlUnit(JUnit) for doing testing. I was surprised that I really didn't find anything that seemed very complete. There were a number of examples that showed how to use the Embedded EJB Container in tests, but nothing that really showed how to use the Maven plugin. While searching I came across a question on stackoverflow asking how about Unit testing a Java Servlet. I decided to kill two birds with one stone; (1) write up a good example for Glassfish, and (2) help someone else with a complete solution

The testing here is implemented as part of the integration-test lifecycle of Maven. This does not replace standard JUnit testing of your code, but rather provides testing of your web application as you would expect in deployment. Consider this a valuable supplement to your testing strategy.

The complete project is located here: image-servlet.


  1. First make sure that you configure the Maven plugin. Complete details can be found in Oracle GlassFish Server Embedded Server Guide
  2. Next, configure the surefire plugin to skip its normal test cycle.
  3. Add a dependencies for JUnit and HtmlUnit to the pom.xml
  4. Write your tests. This is one example.
  5. Execute mvn:install.


You should see the GlassFish server start, deploy your application, execute the unit tests, undeploy the application, and shutdown gracefully.

There is a complete project on Bitbucket which demonstrates the entire process from start to finish doing a complete set of integration tests using HtmlUnit.

The project was developed using NetBeans 7.2.1, and GlassFish 3.1.2.

The complete project is located here: image-servlet.

Java Tip of the Day: Converting an InputStream to a BufferedImage

I was looking for a standard way using the JDK to convert an InputStream into a BufferedImage as part of a JUnit test. The solution was quite simple using ImageIO.


Wednesday, January 02, 2013

<ui:repeat/>, Templates, and @ConversationScope

Fishy Dates
I was going through stackoverflow when I came across: ui:repeat does not work in JSF 2.0? I know that their had been some issues which I detailed in JSF Tip of the Day: <ui:repeat /> Usage Example so I took a look. I resolved the basic issue and printed out a pretty page with some data. The solution is  on  stackoverflow so I won't repeat myself too much.

The modified version which includes a conversation portion and some other bells and whistles is available for download.

The code for the solution is available stackoverflow-uirepeat-example

The project was developed using NetBeans 7.2.1 on GlassFish 3.1.2 on JSF 2.1 (Mojarra) and Weld 1.0.