Thursday, April 30, 2009

Rock Paper Scissors JSF Application

So, we’ve played around a bit with JSF and custom tags and internationalization and stuff, but all of that has been largely display generation. If all we wanted to do was display text to the user, we wouldn’t need a Servlet Engine and a JSF framework. No, the really kewl stuff happens when we start taking data from the user, and responding dynamically to the user depending on what type of data and information they deliver to us on the server side.

This section will look at implementing a Rock-Paper-Scissors type of application using JSF.

First, we need the index.jsp

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<f:view locale="sw"  >
<f:loadBundle basename="com.mcnz.jsf.UIResources" var="i18n"/>
<head>
<title>Rock Paper Scissors: JSF Style</title>
</head>

    <body>

    Please chose between Rock Paper and Scissors:
    <h:form id="rpsgame" >
        <h:inputText id="gesture" ></h:inputText>
        <h:commandButton id="submit" type="submit" value="Play!" action="#{rpsbean.doRockPaperScissorsGame}" ></h:commandButton>
    </h:form>
    </body>

</f:view>
</html>

 

Then we need some pages that we will forward to, depending upon whether the user types in Rock Paper or Scissors:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
<head>
<title>win</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Rational Application Developer">
</head>
<f:view>
    <body>
    <p>I picked rock. You win!</p>

    </body>
</f:view>
</html>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
<head>
<title>lose</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Rational Application Developer">
</head>
<f:view>
    <body>
    I picked Rock...You lose.
    </body>
</f:view>
</html>

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
<head>
<title>tie</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Rational Application Developer">
</head>
<f:view>
    <body>
    <p>I also picked Rock. We tie!</p>

    </body>
</f:view>
</html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
<head>
<title>failure</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Rational Application Developer">
</head>
<f:view>
    <body>
    Hmmm...Something went wrong. Please try again...
    </body>
</f:view>
</html>

Then we need a JavaServerFaces bean class:

package com.mcnz.jsf.bean;

import javax.faces.context.FacesContext;

public class RPSBean {
    public String doRockPaperScissorsGame() {
        String result = "failure";
        java.util.Map requestParameterMap
=
             FacesContext.getCurrentInstance().
getExternalContext()
                 .getRequestParameterMap();
        String clientGesture = (String)requestParameterMap.get("rpsgame:gesture");
        String computerGesture = "rock";
        if (clientGesture.equalsIgnoreCase("scissors")){
            result = "youlose";
        }
        if (clientGesture.equalsIgnoreCase("paper")){
            result = "youwin";
        }
        if (clientGesture.equalsIgnoreCase("rock")){
            result = "tie";
        }
        System.out.println(clientGesture);
        return result;
    }

}

And finally, we need to edit the configuration file. The whole faces-config.xml file looks like this:

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<faces-config>

    <application>
        <locale-config>
            <default-locale>es</default-locale>
            <supported-locale>en</supported-locale>
            <supported-locale>fr</supported-locale>
        </locale-config>
    </application>
    <managed-bean>
        <managed-bean-name>rpsbean</managed-bean-name>
        <managed-bean-class>com.mcnz.jsf.bean.RPSBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>

    <navigation-rule>
        <from-view-id>/index.jsp</from-view-id>
        <navigation-case>
            <from-action>#{rpsbean.doRockPaperScissorsGame}</from-action>
            <from-outcome>youwin</from-outcome>
            <to-view-id>win.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-action>#{rpsbean.doRockPaperScissorsGame}</from-action>
            <from-outcome>youlose</from-outcome>
            <to-view-id>lose.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-action>#{rpsbean.doRockPaperScissorsGame}</from-action>
            <from-outcome>tie</from-outcome>
            <to-view-id>tie.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-action>#{rpsbean.doRockPaperScissorsGame}
</from-action>
            <from-outcome>failure</from-outcome>
            <to-view-id>failure.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>

</faces-config>

How many classes are there in the Java 5 JDK?

Somebody asked this question on the JavaRanch. I did a count of all the files in the “All Classes” link in the JavaDoc and came up with a count of 3280, although that also includes such things as Enum, Interfaces, and I’m wondering if it might also include inner classes.

My JavaRanch Post on the Topic of How Many Classes in the JDK?

Wednesday, April 29, 2009

Is all this JSF stuff really worth it? It makes easy things hard!

 

So far, after quite a bit of writing, we’ve managed to put together a simple JSF view page which uses tags from both the JSF core and JSF HTML tag libraries. Just for review, here’s our Java Server Faces code:

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="
http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>JSF Made Easy</title>
</head>
<f:view>
    <body>
    <p>Hello World! Now check out this link:</p>
    <h:outputLink id="link01" value="
http://www.scja.com">
          <h:outputText id="output01" value="Get Java Certified!" />
    </h:outputLink>
    </body>
</f:view>
</html>

And, here’s what the page looks like when it runs in a web browser like Firefox or Chrome:

jsf intro hello 02

And of course, a web browser simply renders HTML, so here’s the HTML that gets send to the web browser:

<html>
<head>
<title>JSF Made Easy</title>
</head>
<body>
<p>Hello World! Now check out this link:</p>
<a id="link01" href="http://www.scja.com">
<span id="output01">Get Java Certified!</span>
</a>
</body>
</html>

Now, one thing you might be thinking right now is ‘that’s a whole heck of a lot of work to send some pretty basic HTML to a web browser!’ I mean, if all we wanted to do was display the web page pictured above, we could have simply written the HTML by hand in a text editor, saved it with an html extension, and published it to a web server like Apache. It would have taken all of fifteen minutes, if even that. So, is all of this stuff with Front Controllers, FacesServlets, JSF custom tags and faces-config.xml files worth it? Right now, it probably seems like it doesn’t.

Of course, we need to bear in mind that right now, one of our main goals is to just get things working. We need to get the JSF framework, and a page that uses JSF custom tags, working. If we can’t write, deploy and test a basic JSF application, we’ll never succeed at the more complicated stuff. But still, one can’t help but wonder if the complexity is really worth it.

Well, let me assure you that the complexity is indeed worth it. Remember, the goal of JSF isn’t just to pump out HTML to a web browser, but instead, the goal is to simplify many of the most common and tedious programming tasks associated with Java based, enterprise web development.

For example, Java Server Faces provides a variety of functions that facilitate state management, provide input validation, implement the standard ‘memento pattern’ and a variety of other important concepts that all good web applications should embrace. One important, yet often difficult to implement function is that of internationalization, or i18n for short.

For example, our application currently hard codes in phrases like “Hello World” and “Get Java Certified.” That’s unacceptable, especially if our application is targeting the US population that primarily speaks Spanish. Yes, all good web based applications should be internationalized, and we should never actually hard-code any language specific text strings in our GUI layer.

Internationalizing your JSF applications is easy. There’s a few steps involved, don’t get me wrong, but with a modicum of time and effort, you’ll find that multi-language support in your JSF view pages is a cinch.

The first thing you need to do as you prepare to internationalize your JSF applications is to decide which languages you are going to support, and if someone views your application with an unsupported language, to which language setting will your application default. This is all then configured in an application entry in the faces-config.xml file, using the XML elements locale-config, default-locale and supported locale. Here’s how our faces-config.xml file would if we chose to support Spanish, French and English by default:

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<faces-config>

    <application>
        <locale-config>
            <default-locale>es</default-locale>
            <supported-locale>en</supported-locale>
            <supported-locale>fr</supported-locale>
        </locale-config>
    </application>

</faces-config>

Notice that the entry in the default-locale and supported-locale elements are simply the universally accepted internationalization language codes that all web browsers and application servers understand.

The second thing you need to do in order to internationalize your applications is take a look at all of the places where text strings appear in your application. I’ve bolded all of the hard coded text strings in our current JSP:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="
http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="
http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>JSF Made Easy</title>
</head>
<f:view >
    <body>
    <p>Hello World! Now check out this link:</p>
    <h:outputLink value=”
http://www.scja.com>
          <h:outputText id="output01" value="Get Java Certified!" />
    </h:outputLink>
    </body>
</f:view>
</html>

Once identified, you need to give a unique identifier to each of these Strings that will act as a key in a properties file or resource bundle. The keys I’ll create will all start with the word ‘index’, as this is the index.jsp page, followed by a ‘dot’ delimiter, and then a description of the text. Next to that goes an equals sign and a language specific translation. Here’s how my English translation, key-value pairs, will look:

###### UIResources_en.properties  English Translations ######
index.title = JSF Made Easy

index.helloworld = Hello World.Check out the link below!

index.link = http://www.google.com
index.linkdescription = It's google dot calm

This then gets saved to a text file named UIResources_en.properties, which I’m placing alongside all of my Java source code, in a subfolder structure of com\mcnz\jsf. This will end up giving the UIResources file a Java based path of com.mcnz.jsf.UIResources_en.properties. Notice the ‘_en’ appended to the name of the file. This indicates that this file should be used for English language users. I’ll also add a UIResources_es.properties file in the same com\mcnz\jsf folder for all of my American viewers, which will look like this:

###### UIResources_es.properties  Spanish Translations ######
index.title = El JSF Made Easy

index.helloworld = Hola mundo! Compruebe hacia fuera este acoplamiento.

index.link = http://www.google.es
index.linkdescription = Google espagnol

And seeing that I’m a Canadian, I’d lose my citizenship if I didn’t include a French, UIResources_fr,  translation file as well:

###### UIResources_fr.properties  Spanish Translations ######
index.title = Le JSF Made Easy

index.helloworld = Bonjour monde! Verifiez ce lien.

index.link = http://www.google.fr
index.linkdescription = Google fran├žais

You will notice that each UIResources file ends with an internationalization code such as _fr or _es. It’s always a good idea to include a default translation file with no extension, just for good measure. Here’s my default UIResources file, with a couple of English words added just to allow us to identify it if the Java Server Faces frameworks calls upon it in our application.

###### UIResources.properties  Default Text ######
index.title = Default JSF Made Easy Title

index.helloworld = Default Hello World. Check out the link below!

index.link = http://www.google.com
index.linkdescription = It's google dot calm

So, once your internationalized resource bundles are defined, you need to head over to your JSP page and add a little JSF core tag called loadbundle. In this tag, you provide the package based path to your resource file, along with a variable name that will be used to reference the internationalized resource files later on in the JSP.

<f:loadBundle basename="com.mcnz.jsf.UIResources" var="i18n"/>

Again, notice how the var attribute makes ‘i18n’ a variable reference for this particular resource bundle, as it is possible to have multiple resource bundles used on a page.

The loadbundle tag is best placed immediately after the view tag, like so:

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<f:view>
<f:loadBundle basename="com.mcnz.jsf.UIResources" var="i18n"/>

<head>
<title>JSF Made Easy</title>
</head>

    <body>
    <p>Hello World! Now check out this link:</p>
    <h:outputLink value=”
http://www.scja.com>
          <h:outputText id="output01" value="Get Java Certified!" />
    </h:outputLink>
    </body>


</f:view>
</html>

The next step is to remove any hard coded display string with a JSF outputText tag. Furthermore, the value of the outputText tag must point to a key defined in the UIResources file. So, we will replace the “JSF MADE EASY” text in the <title> tags with the following JSF custom tag:

<title>
<h:outputText value="#{i18n['index.title']}"/>
</title>

Now, I’ll be the first to admit that the syntax of the value attribute is a little scary. I mean, what’s with the hash sign and the chicken-lip brackets and the square brackets and all? Well, I’ll try to explain it, but sometimes you just have to accept the syntax as being the required syntax. What’s the old saying? “It is what it is.”

value="#{i18n['index.title']}"

For the value attribute, we must instruct the JSF framework to process the value programatically, and not just print out the text as it appears in the tag. After all, I don’t think any user wants to see all of the textual junk inside the quotes there displaying on their page. So, the hash sign indicates that the attribute should be processed by the JSF framework. Essentially, the has sign, #, begins what we call a “JSF expression” language statement, which in this case, will ask the JSF framework to look for the locally defined variable named i18n (which is the name of our resource bundle as defined in the loadBundle tag), and ask an internationalized version of the property file that i18n variable represents, for a text string that represents a translation for the key ‘index.title’. So, when an English user hits this tag, JSF will spit out the value “JSF Made Easy”, and when a Spanish user hits this tag, JSF will spit out the string “El JSF Made Easy.” (Please excuse me for my poor Spanish translations.)

Of course, we need to replace all of the language-dependent text with a reference to the corresponding key in the associated, translated, resource bundle. When we’ve fully internationalized our index.jsp, here’s how the page source will look:

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<f:view>
<head>
<title><h:outputText value="#{i18n['index.title']}"/></title>
</head>

<f:loadBundle basename="com.mcnz.jsf.UIResources" var="i18n"/>
    <body>
    <h:outputText value="#{i18n['index.helloworld']}"/>
    <h:outputLink id ="link01" value="#{i18n['index.link']}">
          <h:outputText value="#{i18n['index.linkdescription']}"/>
    </h:outputLink>
    </body>
</f:view>
</html>

When I’m browsing the internet, the language setting on my computer is Canadian-English, so when I test my application, I end up getting text pulled from the UIResources_en message bundle. If the language setting of my browser was changed to Spanish or French, I would get the corresponding Spanish or French translations.

jsf intro hello 03

By default, JSF will look at the language setting configured by the web browser, and subsequently sent to the server throught the corresponding Http request header. However, we can override this default behavior by adding a locale attribute to the h:view JSF tag. Here’s how we could edit the h:view tag to force the web page to display in spanish:

<f:view locale="es">

jsf intro hello 04

And of course, setting our page’s locale to French, fr, would trigger a French rendering of the page:

jsf intro hello 05

And of course, setting the language code to swahili, well, it just diverts back to the English translation, as English was configured as the default language in the faces-config.xml file:

 

Now, one thing that I should point out is that not only have all of the text strings been internationalized, but so has the associated link!

When we talk about internationalization, we usually limit the conversation to text strings, but there are many different parts of a web page that might require internationalization. You’ll notice that with the outputLink tag, we used the same JSF expression to look up an appropriately internationalized URL as we did for the outputText tag. So now, French users will be sent to the google.fr page, and Spanish users will be sent to the google.es webpage.

And of course, this same convention can be used in other JSF tags as well. For example, images displayed on a webpage might be different for different languages, so we can have images internationalized the same way when we use the graphicImage tag provided by JSF. And of course, this is really the idea behind using a framework like JSF – sure, some very easy tasks, such as just spitting out plain old HTML might be a little more work with JSF, but complex tasks, like internationalization and localization, are greatly simplified and standardized when using Java Server Faces. This is why we love JSF, because it helps to simplify and standardize how we approach some of the most difficult, challenging, and dare I say it – tedious – aspects of modern Java development.

 

By the way, in JSF 1.2, you don’t need to add a loadBundle tag to every JSP page. You can simply set up the i18n resource bundle and its name once in the faces-config.xml file, and unless instructed otherwise, JSF pages will look for translations in these named files.

<application> <resource-bundle> <base-name>bundles.messages</base-name> <var>bundle</var> </resource-bundle> </application>

Setting up a Resource Bundle in the faces-config.xml file?

 

I just posted this question on JavaRanch. I’m reposting it here, with the answer, so I don’t forget it.

I was doing some internationalization, and all of my resources talk about adding an f:loadbundle tag to every JSP. That seems rather cumbersome. Isn’t there some way to point to the appropriate resource bundle once in the faces-config.xml file or web.xml file, and then not have to re-reference that message bundle or resource bundle on every single JSF view page? I’m positive that I saw that somewhere!

….

Well, apparently, in JSF 1.1 and earlier you do. In JSF 1.2 and on, you can use this little element in your faces-config.xml file.

<application>
    <resource-bundle>
        <base-name>bundles.messages</base-name>
        <var>bundle</var>
    </resource-bundle>
</application>

Tuesday, April 28, 2009

Stored Procedures with Hibernate and JPA Annotations

Here's a nice article on stored procedures with Hibernate. This might answer some questions:


Stored Procedures with Hibernate

<f:view> WSViewRoot incompatible with UIOutput ClassCastException

 

This is the type of exception messages you get when you try and deploy a JSF page that uses JSF custom tags, but does not have an f:view tag surrounding the tags.

All JSF tags must, at some point in their hierarchy, be surrounded by <f:view> … </f:view> tags.

[4/28/09 17:12:13:562 PDT] 0000001f ServletWrappe E   SRVE0068E: Uncaught exception thrown in one of the service methods of the servlet: /index.jsp. Exception thrown : javax.servlet.ServletException: com.ibm.ws.jsf.application.WSViewRoot incompatible with javax.faces.component.UIOutput
    at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:658)
    at com.ibm._jsp._index._jspService(_index.java:78)

---- Begin backtrace for Nested Throwables
java.lang.ClassCastException: com.ibm.ws.jsf.application.WSViewRoot incompatible with javax.faces.component.UIOutput at com.sun.faces.taglib.html_basic.OutputLinkTag.setProperties(OutputLinkTag.java:203)
ServletWrappe E   SRVE0068E: Uncaught exception thrown in one of the service methods of the servlet: Faces Servlet. Exception thrown : javax.servlet.ServletException: com.ibm.ws.jsf.application.WSViewRoot incompatible with javax.faces.component.UIOutput at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:658)

java.lang.ClassCastException: com.ibm.ws.jsf.application.WSViewRoot incompatible with javax.faces.component.UIOutput at com.sun.faces.taglib.html_basic.OutputLinkTag.setProperties(OutputLinkTag.java:203) at javax.faces.webapp.UIComponentTag.findComponent(UIComponentTag.java:716)

E   [Servlet Error]-[Faces Servlet]: java.lang.ClassCastException: com.ibm.ws.jsf.application.WSViewRoot incompatible with javax.faces.component.UIOutput at com.sun.faces.taglib.html_basic.OutputLinkTag.setProperties(OutputLinkTag.java:203)

Improving on the Worlds Simplest JSF Program

Okay, the worlds simplest Java Server Faces program which I just created was pretty simple, but it was also pretty nascent with regards to using JSF in the view layer. Sure, the little application proved that indeed the JSF framework was working properly, handling throw the FacesServlet all requests that come into the server with a .faces mapping at the end of them. And we even got the FacesServlet to invoke our simple JSP view page when it was requested, but that view page was nothing but simple, static HTML. If we really want to boast about having a real, JSF-generated view, then we need to at least throw a couple of JSF custom tags on our JSP page.

The entire JSF API from Sun provides two, count them: two, custom tag libraries that we can use in our JSP pages. Java developers can define as many of their own custom tag libraries as they want, but as far as the standard API goes, Sun only provides you with two custom tag libraries. They are:

  1. The JSF Core Tags (http://java.sun.com/jsf/core)
  2. The JSF HTML Tags (http://java.sun.com/jsf/html)

There are 25 separate tags defined in the JSF HTML tag library, all of which pretty much have to do with generating HTML for a web based client. If you have an familiarity with web page development, you could probably guess what many of the JSF HTML tags, such as inputText, selectOneListbox and graphicImage, are designed to do. Just to appease your curiosity, here’s an alphabetical listing of the twenty-five tags that round out the JSF HTML tag library.

  1. column
  2. commandButton
  3. commandLink
  4. dataTable
  5. form
  6. graphicImage
  7. inputHidden
  8. inputSecret
  9. inputText
  10. inputTextarea
  11. message
  12. messages
  13. outputFormat
  14. outputLabel
  15. outputLink
  16. outputText
  17. panelGrid
  18. panelGroup
  19. selectBooleanCheckbox
  20. selectManyCheckbox
  21. selectManyListbox
  22. selectManyMenu
  23. selectOneListbox
  24. selectOneMenu
  25. selectOneRadio

So, as I said, there are two custom tag libraries associated with JSF. The HTML library contains tags for creating HTML markup. The other library, the JSF core tags, contain all of the tags that are not in the HTML tag library. :P

Okay, that’s a bit of a flippant definition of the core tag library, but it is remarkably accurate. The HTML library tags all generate some type of markup when they are used. The core library tags are not limited to just HTML markup, and provide functionality that allows for the creation of JSF view pages that can be viewed outside of a standard web browser. Looking at the following list of eighteen JSF Core tags, you will again see many that are probably self evident in their purpose, including tags such as convertDateTime, convertNumber,  validateLength, and validateLongRange. Here’s the complete, alphabetical listing of all of the JSF core tags:

  1. actionListener
  2. attribute
  3. convertDateTime
  4. converter
  5. convertNumber
  6. facet
  7. loadBundle
  8. param
  9. selectItem
  10. selectItems
  11. subview
  12. validateDoubleRange
  13. validateLength
  14. validateLongRange
  15. validator
  16. valueChangeListener
  17. verbatim
  18. view

Anyways, I don’t want to try and include every single custom tag in my introductory JSF application. All I’d like to do is use one or two simple tags from each of the two JSF tag libraries in my JSP.

Let’s take a look at our nascent little helloworld.jsp as it currently stands:

<html>
<head><title>Simple JSF Page</title></head>
    <body>
    <p>Hello Cruel World!</p>
    </body>
</html>

First of all, if we hope to use two custom tag libraries, we must first add in two taglib declarations at the top of the page, before the <html> tag. The standard syntax for the core and html custom taglib directives are as follows:

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="
http://java.sun.com/jsf/html" prefix="h"%>

By convention, the core tags are always given the prefix “f” while the html tags are given the “h” prefix.

The Core JSF tag I was interested in using was the <f:view> tag. Actually, I’m forced to use it, because every JSF tag on a view page must, at some point in time, be contained within an <f:view> tag. So, with the opening and closing f:view tags added, our JSP page will look like this:

 

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>JSF Made Easy</title>
</head>
<f:view>
    <body>
    <p>Hello World! </p>
    </body>
</f:view>
</html>

Of course, this change becomes more semantic than pedantic, as it doesn’t really change how our page displays in the web browser. The <f:view> tag really only sets our page up for the potential use of sexy and salacious JSF HTML tags, which can help us render hyper-text markup which can be displayed in a web browser. So, within our core view tag, we need some JSF HTML tags. The tags I was interested in playing with here are the outputLink and the outputText tags.

As you probably imagined, the outputLink tag simply generates a clickable, HTML link in the web browser. Of course, the outputLink really only creates the HTML anchor tag, without any text associated with the link, which basically means it creates a link that displays nothing for the client to click on. To have some clickable text, we must nest a handsome outputText tag within the outputLink tag. It’s all pretty simple. Here’s how it looks, as we emphasize that the taglib directive stated that the letter ‘h’ is associated with all JSF HTML custom tags.

    <h:outputLink id="link01" value="http://www.scja.com">
          <h:outputText id="output01" value="Get Java Certified!" />
    </h:outputLink>

So, this nested little JSF tag combination will create a clickable link that says “Get Java Certified”, and when clicked, will send the user to my amazing website, www.scja.com.

 

After adding a little “Now check out this link” preamble in the prose after ‘Hello World’, my JSP page is completed. Here’s how the complete JSP page looks:

<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="
http://java.sun.com/jsf/html" prefix="h"%>
<html>
<head>
<title>JSF Made Easy</title>
</head>
<f:view>
    <body>
    <p>Hello World! Now check out this link:</p>
    <h:outputLink id="link01" value="http://www.scja.com">
          <h:outputText id="output01" value="Get Java Certified!" />
    </h:outputLink>

    </body>
</f:view>
</html>

 

jsf intro hello 02

Here’s the HTML that actually gets generated by the Java Server Faces custom tags:

<html>
<head>
<title>JSF Made Easy</title>
</head>

<body>
<p>Hello World! Now check out this link:</p>

<a id="link01" href="
http://www.scja.com">
<span id="output01">Get Java Certified!</span>
</a>

</body>

</html>

Simple Interactive JSF Page

Okay, I’ve just created a very simple JSF application. Well, pretty much the simplest JSF application you could possibly imagine. But really, all it does is display “Hello World.” I’m not going to win any programming awards with a simple ‘Hello World.’

I was thinking that I might explore a bit more interactivity by trying to create a JSF application that recreates the universal experience of playing that old playground game of Rock Paper Scissors (RPS). I’m thinking the the client submits either “rock”, “paper” or “scissors” to the server, the server then randomly makes its own choice, and the result is sent back to the client. It’s an easy concept to grasp, and it would make a quirky, yet simple, scenario for playing around with Java Server Faces and JSF tags.

Monday, April 27, 2009

JSF WAR Deployment with IRAD7 to WebSphere 6.1

I just deployed a little war file from IRAD 7, which was developed with a WebSphere 6.1 target, and I noticed that the exported war file did not include any JSF libraries in the lib directory. Is this because the JSF 1.0 libraries come, by default, on the WebSphere 6.1 runtime? Is that standard for all version 2.4 Servlet engines?

Just wondering?

The Simplest JSF Program You Can Write

 

So, I was just wondering how simply can you make a JSF application. I mean, every time a RAD tool creates a JSF project, all sorts of entries get populated in the web.xml and faces-config.xml file. But how many of those entries can you do without?

So, I decided to use IBM’s Rational Application Developer and strip out all of the superfluous stuff from the various xml and configuration files, and just have the FacesServlet invoke a very innocuous JSP file sitting in the root of the web application.

Here’s the simple deployment descriptor for the web module, the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="
http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>JSFWeb</display-name>
    <servlet id="JSF_Front_Controller_Servlet">
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

And here’s the faces-config.xml file. I really didn’t think it could get this small, but apparently, you really just need it there. If you’re not using any JSF components or managed beans, you don’t need to have anything inside of it!

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
  "
http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<faces-config>

</faces-config>

And here’s the handsome JSP page. There’s actually probably more here than I really need, with the doctype and everything, and even the custom tag – it’s not totally needed, but at least it definitively asserts that indeed, we have a JSF page, and not just a JSP.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="
http://java.sun.com/jsf/core" prefix="f"%>
<html>
<head>
<title>Simple JSF Page</title>

</head>
<f:view>
    <body>
    <p>Hello World!</p>
    </body>
</f:view>
</html>

Actually, I tested the whole thing with just this for the html/jsp page, and it all worked just fine. Not the best form, granted, but it works! Sometimes, simple is best when you’re just starting out.

<html>
<head><title>Simple JSF Page</title></head>
    <body>
    Hello World!
    </body>
</html>

jsf hello world example irad websphere java server faces

Struggling with CVSNT and Netbeans / Rational IRAD 7

 

Man, I’ve been just really struggling with IRAD7 and CVSNT.

First off, I downloaded a bunch of garbage from CVSNT.org, and it was just a bunch of junk that complicated things, rather than making things easier. I got a tortoise and a merge tool, but just the basics of CVSNT were hidden in some util folder that didn’t really look like it was front and center. Instead, something called WinCVS or something was the tool that seemed to be doing the source code management. Apparently, the people at CVSNT have been bought out, and have decided to try and make money on their tool, and part of that money-making scheme involves getting people who think they are downloading CVSNT to download a bunch of trial software instead.

Anyways, I eventually found the download for the CVSNT server, and I installed that. I think file, named cvsnt-server-2.5.04.3236.msi, was about 4 megs in size. If you download anything bigger than that, you’ve probably got a bunch of bloat ware.

Here’s the URL from which I downloaded CVSNT. Going directly here will likely end up in a redirect that gets you to click on some button that accepts the license agreement or something, but you can give it a try.

http://march-hare.com/archive/cvsnt-server-2.5.04.3236.msi

I managed to get NetBeans and CVSNT to play together quite nicely using the local url, :local:/jsf02. (This local CVSNT URL required an environment variable to be set, although different tutorials said either CVS_EXE or CVSEXE were the Windows system variable names, so I set both up and configured them to point to the cvs.exe file:

C:\Program Files\CVSNT\cvs.exe

However, IRAD7 didn’t allow me to type in the local URL. This mean using the remote URL, which also meant I had to go through CVS authentication, which is different from simply authenticating against the local user registry in Windows. What I was hoping would be very easy and straight forward ended up requiring the manual addition of users to the local windows user registry (CVS has separate user accounts, but they must link back to a user defined in XP or the Unix/LDAP user registry), and then some command line statements such as:

C:\Program Files\CVSNT>set cvsroot=:sspi:hp-000:/test

C:\Program Files\CVSNT>cvs password -a cvsuser
Adding user cvsuser@hp-000
New Password:
Verify Password:

Anyways, I finally got it to work in IRAD7. The really big help was actually this article below,

http://web.telia.com/~u86216177/InstallCVSNT25.html#AddUsers

Kudos to author Bo Berglund for his ‘Google Ad Free’ tutorial on working with CVSNT and setting up users and repositories and stuff. It helped me get everything working.



CVSNT 2.5.03 Installation on Windows 2003 Author: Bo Berglund

What is with the Netbeans Icon?

netbeans vs ATHF wisdom cubeI’m just looking at the Netbeans IDE icon on my desktop, and just wondering how much time the marketing and branding department took to come up with that one?

I mean, what is it supposed to be, the ‘Wisdom Cube’ from Aqua Teen Hunger Force or something? Actually, that would be totally kewl if that was the reference, but I doubt it is.

Just another Test

A Test Post

Friday, April 24, 2009

iastore.sys is corrupted HP Pavilion XP Install over Vista

32-bit Floppy Configuration Utility for Intel® Matrix Storage Manager

This is the file that I used. It wrote a whole program to a floppy drive, that I then used during the XP install. I used a USB based floppy drive. I was surprised that the XP install program could recognize it, but it did.

Actually, I think I needed the 3-series chipset driver found here:

http://downloadcenter.intel.com/Detail_Desc.aspx?agr=Y&ProductID=2842&DwnldID=17413&strOSs=44&OSFullName=Windows*%20XP%20Professional&lang=eng

This creates the correct folder structure in the floppy, with the correct TxtSetup.OEM file configuration and everything.

Here what the txtsetup.oem file looked like:

Sample TxtSetup.OEM for installing SATA Drivers for an XP installation on an HP Desktop computer:

; ****************************************************************************
; ****************************************************************************
; ** Filename: TxtSetup.OEM
; ****************************************************************************
; ****************************************************************************

[Disks]
disk1 = "Intel Matrix Storage Manager driver", iaStor.sys, \

[Defaults]
scsi = iaStor_ICH8M

;----------- Component Section -----------

[scsi]
iaStor_ICH8M = "Intel(R) 82801HEM SATA RAID Controller (Mobile ICH8M-E)"
iaAHCI_ICH8M = "Intel(R) 82801HEM/HBM SATA AHCI Controller (Mobile ICH8M-E/M)"
iaStor_ICH8R = "Intel(R) 82801HR/HH/HO SATA RAID Controller (Desktop ICH8R)"
iaAHCI_ICH8R = "Intel(R) 82801HR/HH/HO SATA AHCI Controller (Desktop ICH8R)"
iaStor_ESB2 = "Intel(R) 631xESB/632xESB SATA RAID Controller (Server/Workstation ESB2)"
iaAHCI_ESB2 = "Intel(R) 631xESB/632xESB SATA AHCI Controller (Server/Workstation ESB2)"
iaStor_ICH7MDH = "Intel(R) 82801GHM SATA RAID Controller (Mobile ICH7MDH)"
iaStor_ICH7DH = "Intel(R) 82801GR/GH SATA RAID Controller (Desktop ICH7R/DH)"
iaAHCI_ICH7R = "Intel(R) 82801GR/GH SATA AHCI Controller (Desktop ICH7R/DH)"
iaAHCI_ICH7M = "Intel(R) 82801GBM SATA AHCI Controller (Mobile ICH7M/DH)"
iaStor_ICH6R = "Intel(R) 82801FR SATA RAID Controller (Desktop ICH6R)"
iaAHCI_ICH6R = "Intel(R) 82801FR SATA AHCI Controller (Desktop ICH6R)"
iaAHCI_ICH6M = "Intel(R) 82801FBM SATA AHCI Controller (Mobile ICH6M)"

;-------------------------------------------

[Files.scsi.iaStor_ICH8M]
driver = disk1, iaStor.sys, iaStor
inf = disk1, iaStor.inf
catalog = disk1, iaStor.cat

[Files.scsi.iaAHCI_ICH8M]
driver = disk1, iaStor.sys, iaStor
inf = disk1, iaAHCI.inf
catalog = disk1, iaAHCI.cat

[Files.scsi.iaStor_ICH8R]
driver = disk1, iaStor.sys, iaStor
inf = disk1, iaStor.inf
catalog = disk1, iaStor.cat

[Files.scsi.iaAHCI_ICH8R]
driver = disk1, iaStor.sys, iaStor
inf = disk1, iaAHCI.inf
catalog = disk1, iaAHCI.cat

[Files.scsi.iaStor_ESB2]
driver = disk1, iaStor.sys, iaStor
inf = disk1, iaStor.inf
catalog = disk1, iaStor.cat

[Files.scsi.iaAHCI_ESB2]
driver = disk1, iaStor.sys, iaStor
inf = disk1, iaAHCI.inf
catalog = disk1, iaAHCI.cat

[Files.scsi.iaStor_ICH7DH]
driver = disk1, iaStor.sys, iaStor
inf = disk1, iaStor.inf
catalog = disk1, iaStor.cat

[Files.scsi.iaAHCI_ICH7R]
driver = disk1, iaStor.sys, iaStor
inf = disk1, iaAHCI.inf
catalog = disk1, iaAHCI.cat

[Files.scsi.iaStor_ICH7MDH]
driver = disk1, iaStor.sys, iaStor
inf = disk1, iaStor.inf
catalog = disk1, iaStor.cat

[Files.scsi.iaAHCI_ICH7M]
driver = disk1, iaStor.sys, iaStor
inf = disk1, iaAHCI.inf
catalog = disk1, iaAHCI.cat

[Files.scsi.iaStor_ICH6R]
driver = disk1, iaStor.sys, iaStor
inf = disk1, iaStor.inf
catalog = disk1, iaStor.cat

[Files.scsi.iaAHCI_ICH6R]
driver = disk1, iaStor.sys, iaStor
inf = disk1, iaAHCI.inf
catalog = disk1, iaAHCI.cat

[Files.scsi.iaAHCI_ICH6M]
driver = disk1, iaStor.sys, iaStor
inf = disk1, iaAHCI.inf
catalog = disk1, iaAHCI.cat


;-------------------------------------------

[Config.iaStor]
value = "", tag, REG_DWORD, 1b
value = "", ErrorControl, REG_DWORD, 1
value = "", Group, REG_SZ, "SCSI miniport"
value = "", Start, REG_DWORD, 0
value = "", Type, REG_DWORD, 1

;-------------------------------------------------------

[HardwareIds.scsi.iaStor_ICH8M]
id = "PCI\VEN_8086&DEV_282A&CC_0104","iaStor"

[HardwareIds.scsi.iaAHCI_ICH8M]
id = "PCI\VEN_8086&DEV_2829&CC_0106","iaStor"

[HardwareIds.scsi.iaStor_ICH8R]
id = "PCI\VEN_8086&DEV_2822&CC_0104","iaStor"

[HardwareIds.scsi.iaAHCI_ICH8R]
id = "PCI\VEN_8086&DEV_2821&CC_0106","iaStor"

[HardwareIds.scsi.iaStor_ESB2]
id = "PCI\VEN_8086&DEV_2682&CC_0104","iaStor"

[HardwareIds.scsi.iaAHCI_ESB2]
id = "PCI\VEN_8086&DEV_2681&CC_0106","iaStor"

[HardwareIds.scsi.iaStor_ICH7DH]
id = "PCI\VEN_8086&DEV_27C3&CC_0104","iaStor"

[HardwareIds.scsi.iaStor_ICH7MDH]
id = "PCI\VEN_8086&DEV_27C6&CC_0104","iaStor"

[HardwareIds.scsi.iaAHCI_ICH7R]
id = "PCI\VEN_8086&DEV_27C1&CC_0106","iaStor"

[HardwareIds.scsi.iaAHCI_ICH7M]
id = "PCI\VEN_8086&DEV_27C5&CC_0106","iaStor"

[HardwareIds.scsi.iaStor_ICH6R]
id = "PCI\VEN_8086&DEV_2652&CC_0104","iaStor"

[HardwareIds.scsi.iaAHCI_ICH6R]
id = "PCI\VEN_8086&DEV_2652&CC_0106","iaStor"

[HardwareIds.scsi.iaAHCI_ICH6M]
id = "PCI\VEN_8086&DEV_2653&CC_0106","iaStor"

The the way, the iastor.sys file was 277K in size.

Wednesday, April 22, 2009

Creating/Restoring Image with Norton 14 Wrong Drive Letter Problem Sovled!

So, I've got Norton 14 installed and I'm trying to image my disc. The problem is, when I copy my hard drive to say, my E or F drive, when I reboot into that hard drive, the image doesn't rename itself to C:\. So, it starts looking for files in c:\program files, but the disc itself retains the letter name of E or F, so no programs run, not even computer manager, which can be used to rename drive letters.

I ended up just downloading the Norton recovery boot disc. I then imaged my C:\ drive and stored the image locally. I rebooted with the Norton Recovery Boot Disc, which I had to download as an iso file and burn myself, and then chose to restore my computer from a backup. I chose the backup on the C: drive, and chose the second hard drive as the destination drive. I did delete the partitions on the second drive, but I'm not sure if that's needed.

During the re-image, I chose to make the reimaged drive an active hard drive so that the OS would be recognized. I DID NOT select the two check boxes underneath, which would restore the original disc signature and secondly, restore the MBR, Master Boot Record. I'm wondering it it was the original disc signature, that said that my second hard drive was not the C:\ drive, that was messing everything up.

In the end, when I rebooted, and disconneted the original hard drive, the second, new hard drive booted properly, and all is well. :)

Just as a point of reference, I'm using Windows Vista.

Sunday, April 19, 2009

When IRADs WebSphere Test Server Fails to Respond

My test server keeps failing to response; when I right-click on it in the servers tab, I can't even get a context menu. Well, I just want to stop and restart it, so to stop it, I just went right to the bin directory and issued the stopServer command. Even though it's the test server for rational application developer 7.5, the stopServer and startServer commands in the bin directory still work fine.

Here's the command:

C:\_irad\core\runtimes\base_v7\bin>stopServer -server1

**********************
C:\_irad\core\runtimes\base_v7\bin>stopServer server1
ADMU0116I: Tool information is being logged in file
C:\_irad\profiles\was7profile\logs\server1\stopServer.log
ADMU0128I: Starting tool with the was7profile profile
ADMU3100I: Reading configuration for server: server1
ADMU3201I: Server stop request issued. Waiting for stop status.
ADMU4000I: Server server1 stop completed.


C:\_irad\core\runtimes\base_v7\bin>startServer server1
ADMU0116I: Tool information is being logged in file
C:\_irad\profiles\was7profile\logs\server1\startServer.log
ADMU0128I: Starting tool with the was7profile profile
ADMU3100I: Reading configuration for server: server1

**********************

What is the URL for the WebSphere 7 Admin Console in IRAD 7.5

http://localhost:9060/ibm/console/

I hate the fact that from within IRAD, you can't see the URL for the admin console. Every time you try to do something with the console, the annoying console window starts spitting out garbage, and the window restores itself to a non-maximized state.

Well, here's the URL for the admin console so you can open it up on Google Chrome or Firefox:

http://localhost:9060/ibm/console/

Heh...I just noticed in the Title that IBM has renamed the WebSphere Administrative Console to the "Integrated Solutions Console." That certainly makes everything work better. :P

Value '2.0' is not facet-valid with respect to enumeration '[1.2]'.

Darn, it looks like I've got my design-time, Rational Application Developer 7.5 environment loving my JSF2.0 application, but at runtime, when I deploy to the WebSphere 7.0 server, the server is expecting JSF 1.2. Now to get around this annoying little discrepancy. Any ideas?

I believe the second exception is just being triggered by the first. Alternatively, it could just be an indication that the required libraries simply aren't available on the server. Maybe I should dig into the administrative console for WebSphere 7?


Exception caught while initializing context: {0}
com.sun.faces.config.ConfigurationException: CONFIGURATION FAILED! Unable to parse document WEB-INF/faces-config.xml': cvc-enumeration-valid: Value '2.0' is not facet-valid with respect to enumeration '[1.2]'. It must be a value from the enumeration.

com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0100E: Uncaught init() exception created by servlet Faces Servlet in application IfNavigationEar: java.lang.IllegalStateException: Application was not properly initialized at startup, could not find Factory: javax.faces.context.FacesContextFactory
at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:270)

Followers