Tuesday, July 05, 2011

JSF 2.x: Creating a Pure AJAX Application with Apache Tomahawk

Official logo of Greenville, South CarolinaImage via Wikipedia
I was working on creating an application to make calls to a MongoDB database. I was having some difficulties getting the REST based services to work. I decided to make sure that my coding principles were sound so I wrote this application.

The application uses Expression Language (EL), and "pure" JavaScript (no JS frameworks).

Disclaimer: There were no-frameworks harmed in the making of this application.

The application makes an AJAX call to a GeoNames web service called http://api.geonames.org/weatherIcaoJSON?ICAO=KGSP which returns the weather information for Greenville-Spartanburg (Greenville, SC) Airport. There are some other examples which demonstrate some of the things you can do in Expression Language (EL), and how to inject it into JavaScript.

The application I believe demonstrates that you can JSF without managed beans, and make separate AJAX calls to populate the page with data. This is not a normal method of creating a JSF application, but does demonstrate the flexibility of the JSF framework.

The application was done using Apache Maven on NetBeans 7.0 IDE. The source can be downloaded here: jsf2-tomahawk2.zip

  • NetBeans 7.0 (You could use just Maven)
  • Apache Maven 2.2+
  • Internet Connection (Required if dependencies are not on system)



 * Copyright 2011 Blue Lotus Software, LLC.
 * Copyright 2011 John Yeary.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
var weather;
var weatherJSON;

function ajaxWeather() {
    if (!document.getElementById('panelTab1.content').hasChildNodes()) {
    var url = 'http://ws.geonames.org/weatherIcaoJSON?ICAO=KGSP';
    var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else {
        alert('You are using Internet Explorer. Switch to a real browser!');
        if (xhr.readyState == 4 && xhr.status == 200){
            weather = xhr.responseText;
            weatherJSON = eval('(' + weather + ')');
            document.getElementById('panelTab1:unavailable').textContent = '';
            document.getElementById('panelTab1:stationName').textContent = weatherJSON.weatherObservation.stationName;
            document.getElementById('panelTab1:ICAO').textContent = weatherJSON.weatherObservation.ICAO;
            document.getElementById('panelTab1:latitude').textContent = weatherJSON.weatherObservation.lat + "\u02da";
            document.getElementById('panelTab1:longitude').textContent = weatherJSON.weatherObservation.lng + "\u02da";
            document.getElementById('panelTab1:temperature').textContent = weatherJSON.weatherObservation.temperature + "\u02daC";
            document.getElementById('panelTab1:humidity').textContent = weatherJSON.weatherObservation.humidity +"%";
            document.getElementById('panelTab1:windSpeed').textContent = weatherJSON.weatherObservation.windSpeed + " knots";
            document.getElementById('panelTab1:windDirection').textContent = weatherJSON.weatherObservation.windDirection +"\u02da"
        if (xhr.readyState == 4 && xhr.status == 503){
            document.getElementById('panelTab1:unavailable').textContent = 'Weather service is unavailable. Please reload page';
    xhr.open('GET', url, true);

function setText(){
    if (document.getElementById('panelTab6:div1') != null) {
        document.getElementById('panelTab6:div1').textContent = msg;

Enhanced by Zemanta

Sunday, July 03, 2011

Creating a Maven Web Application Archetype in NetBeans 7.0

Slide | The Seven Rules of Great Web Applicati...Image by quasarkitten via Flickr
The  basics for creating a Maven archetype can be found in the Maven - Guide to Creating Archetypes. The guide gives general guidelines for creating an archetype, but no specific details for web applications. This tutorial will show you a simple way of creating a web application archetype and using it. We will use NetBeans 7.0 to create our project, create the archetype, and consume it. This application uses JSF 2.x and GlassFish. However the principles are the same for web applications in general.

The first more practical approach is to create, or use an existing application, and convert it to an archetype.
  1. Create a new Maven web project in NetBeans.
  2. Modify the pom.xml to include the dependencies you require in the project.
  3. (Optional) Modify the pom.xml to include a license
  4. (Optional) Create a web.xml. This is not required for JEE6 web application generally. However, we need one for the Faces Servlet.
  5. (Optional) Add a resource file for externalizing message resources
  6. Create a basic web page, and supporting classes. JSF for example has the following.
    • Index.xhtml
    • IndexBean.java is a Java EE 6 JSF @ManagedBean, and @RequestScoped bean. You could use @Named and @RequstScoped from CDI as well.
  7. Once you are satisfied withe the project, execute mvn archetype:create-from-project. This will create your new archetype in the target/generated-sources/ directory.
  8. Open the newly created project in NetBeans and make any necessary changes.
  9. Next execute mvn install. This will install your new archetype in the local repository.
  10. Create a new project using the new archetype, and run it.
Here is a link to the project: jsf2.zip You can create the archetype from it.

I have created a Youtube video that demonstrates the process.

Enhanced by Zemanta

Friday, July 01, 2011

JSF 2.x and jQuery onload Control

I was asked if there was a way to control Mojarra JSF 2.x components with jQuery. In this case they wanted to be able to control some aspect of the controls from JavaScript on the loading of the document. Here are a couple of quick examples of controlling JSF components using jQuery.
  • Set the background to a light gray
  • Disable form1:inputText1
  • Change the value of form1:inputText2 to Hello World! using a EL assignment to a JavaScript variable.
  • Set form1:inputText3 to be required.



//  Copyright 2011 Blue Lotus Software, LLC.
//  Copyright 2011 John Yeary.
package bean;

import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;

 * @author John Yeary
 * @version 1.0
public class IndexBean {

    private String value = "Hello World";

    public String getValue() {
        return value;

    public void setValue(String value) {
        this.value = value;

Enhanced by Zemanta

Java EE 6: Using an External JPA Library in an EJB

I was looking at the NetBeans Forums for Java Enterprise Edition (EE) Development. I saw a couple of folks who were asking how I did a couple of different projects where I sent them the final result, but not an explanation of how I did it.

I decided to do a YouTube video instead of writing out how I did it. I thought the best way to demonstrate it was in a video. "A picture tells a thousand words."

Popular Posts