Monday, April 27, 2009

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

No comments:

Post a Comment

Followers