Is Java Server Faces a Bona Fide Panacea for the Web UI?, Page 2
JSF is a specification; therefore, any vendor can write their own implementation and compete on performance and features. Sun has the reference implementation (RI) and Apache has the MyFaces version.
Currently, JSF is well supported by the industry and companies such as Oracle, IBM, and the Apache Software Foundation are actively developing for it. A lot of Java IDEs on the market directly recognize core JSF tags. Here are the screen shots from JBuilder and JDeveloper, but MyEclipse, Exadel Studio pro, IBM WSAD, BEA Workshop, Sun Studio Creator, and a lot of other tools support this technology as well.
|JSF components selectors: JDeveloper left, JBuilder right|
Using the JSF
I used Borland JBuilder and WebLogic server to test, but any IDE and any server can be used. On the left is the final project structure.
The project retrieves the weather info based on the user's location. There are only two pages, the input (weather.jsp) and exit.jsp. The user selects the location from the select box and then clicks the button; that submits the form (see screen shots at the end).
To set up the framework, I imported the JSF (and JSTL) libraries into the project, (most IDEs do this automatically).
Next, I added the Faces controller servlet to the standard J2EE web.xml descriptor file and indicated that any request to an extension *.f will be handled by this servlet. Actually, any extension can be used. The JSF will correctly call the JSP, and if I wanted to, I could have called the extension .asp to hide the server technology I use.
<web-app> <display-name>JSFWebModule</display-name> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.f</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>weather.f</welcome-file> </welcome-file-list> </web-app>
I created the managed bean (com.jsf.src.BackingBean) to hold the weather information, and added two empty JSP files. (Some IDEs will create the JSPs, bean classes, and their properties from the faces-config wizard) Next, I visually configured the faces-config.xml file to set up the flow and reference to the backing bean.
Here is the screen shot of JBuilder's Faces Config editor:
The end resulting XML looks like this:
<faces-config> <navigation-rule> <description>Navigation for the weather app.</description> <from-view-id>/weather.jsp</from-view-id> <navigation-case> <from-outcome>success</from-outcome> <to-view-id>/exit.jsp</to-view-id> </navigation-case> </navigation-rule> <managed-bean> <description>backing weather bean.</description> <managed-bean-name>weatherBean</managed-bean-name> <managed-bean-class>com.jsf.src.BackingBean </managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> </faces-config>
Note: I set a navigation rule on "success" to go to exit.jsp. Also, I set the scope of the bean to "session" and its name to be "weatherBean". The scope for JSF managed beans could be application, session, and request.
Then, it was time to put JSF tags into the JSPs.
Page 2 of 3