People say that the world of IT is running so fast that one must learn harder in order to stay in the same place. So, in the name of “staying in the same place”, I’d like to spend some time learning a -not so- new J2EE framework called JSF (Java Server Faces).
JSF is one of MVC frameworks available in the market whose technical specification defined in JSR (Java Specificatioin Request).
- JSF version 1.0 and 1.1 (bug-fixes for version 1.0) are defined in JSR 127.[1]
- JSF version 1.2 is defined in JSR 252.[2]
There are some JSF implementations out there, but the one that is used in this writing is the implementation written by Apache Foundation named MyFaces.
Installation
First, you would need MyFaces library downloaded from Apache Foundation’s site. And don’t forget to download libraries needed by MyFaces:
- commons-beanutils, the jar file can be downloaded here.
- commons-collections, the jar file can be downloaded here.
- commons-digester, the jar file can be downloaded here.
- commons-el, the jar file can be downloaded here.
- commons-lang, the jar file can be downloaded here.
- commons-logging, the jar file can be downloaded here.
To install those libraries, you only need to extract the jar files to the “WEB-INF/lib” folder of your web application.
web.xml
Next, you must create the web.xml file, the configuration file for a J2EE application located in “WEB-INF” folder. To make it simpler, you can use following snippet of web.xml. Example below was taken from MyFaces’ site with minor updates.
<!DOCTYPE web-app PUBLIC
“-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
“http://java.sun.com/dtd/web-app_2_3.dtd”>
<web-app>
<display-name>MyFaces Demo</display-name>
<description>MyFaces Demo</description><context-param>
<param-name>javax.faces.application.CONFIG_FILES</param-name>
<param-value>
</param-value>
<description>
Comma separated list of URIs of additional faces config files.
(e.g. /WEB-INF/my-config.xml) Attention: /WEB-INF/faces.config.xml is added automatically.
See JSF 1.0 PRD2, 10.3.2
</description>
</context-param><context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param><context-param>
<param-name>myfaces_allow_javascript</param-name>
<param-value>true</param-value>
<description>
This parameter tells MyFaces if javascript code should be allowed in the
rendered HTML output.
If javascript is allowed, command_link anchors will have javascript code
that submits the corresponding form.
If javascript is not allowed, the state saving info and nested parameters
will be added as url parameters.
Default: “true”
</description>
</context-param><context-param>
<param-name>myfaces_pretty_html</param-name>
<param-value>true</param-value>
<description>
If true, rendered HTML code will be formatted, so that it is “human readable”.
i.e. additional line separators and whitespace will be written, that do not
influence the HTML code.
Default: “true”
</description>
</context-param><!– Faces Servlet –>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet><!– Faces Servlet Mapping –>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>
faces-config.xml
The next file would be faces-config.xml. This is the configuration file known by MyFaces. If you ever tasted the flavour of Struts, this file service is nearly the same as struts-config.xml. For our web application, you may use following snippet.
<?xml version=’1.0′ encoding=’UTF-8′?>
<!DOCTYPE faces-config PUBLIC
“-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN”
“http://java.sun.com/dtd/web-facesconfig_1_1.dtd” >
<faces-config/>
hello.jsp
This is our final step before deploying our application to the server, our “Hello World!” page.
<%@ taglib uri=”http://java.sun.com/jsf/core” prefix=”f” %>
<%@ taglib uri=”http://java.sun.com/jsf/html” prefix=”h” %>
<f:view>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<strong>Hello World!</strong>
</body>
</html>
</f:view>
Result
All required steps have been taken, and the time for harvesting has been before our eyes. Of course, we still need to deploy them though :p
This is the screenshot of our application.

I don’t cover much detail on MyFaces or JSF, but I you’d like to continue much more deeper, feel free to go to :
Footnotes
[1] http://www.roseindia.net/jsf/jsf-versions.shtml
[2] Ibid.
С этой записи у тебя начинается опыт блогинга) чтож, можно пожелать тольк удачи!
Comment by Vanowasp — April 21, 2009 @ 3:35 pm