Downloading MyFaces and creating web application
In this section we will learn how to create web application for our login and registration example program. Alternatively you can also download the integrated web application with source code and library files.
Downloading MyFaces
MyFaces can be configured using libraries and configuration files that comes with the example applications. The latest version of MyFaces can be downloaded from http://myfaces.apache.org/download.html. We have downloaded tomahawk-examples-1.1.6-bin.zip from http://www.apache.org/dyn/closer.cgi/myfaces/binaries/tomahawk-examples-1.1.6-bin.zip for this example tutorial. The downloaded file will be a zip file named tomahawk-examples-1.1.6-bin.zip. Extract the zip file and you will get 4 war files. Copy myfaces-example-simple-1.1.6.war file in the webapps directory of Tomcat which will automatically be expanded in the directory of same name. Now we will use the libraries and configuration files from the exploded application to create our web application.
Creating web application
Web application follows the start directory structure as defined in the JEE (J2EE) specification. Here we are creating the application in the exploded format, you can also create archive (war, ear) and then deploy on the application server.
Following image shows the directory structure of our web application.
Now follow the following steps to create the application:
- Create a directory with the name of your application (HibernateMyFaces) under webapps directory of tomcat.
- Create "WEB-INF" directory inside it.
- Create other directories as shown in the above image.
- Copy libraries files from "myfaces-example-simple-1.1.6\WEB-INF\lib" directory to "lib" directory.
- Create web.xml file under WEB-INF directory
and add the following content.
<?xml version="1.0"?> <!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> <!-- Spring context Configuration Begins--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/log4j.properties</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-hibernate.xml</param-value> </context-param> <servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!--End Spring configuration --> <context-param> <param-name>javax.faces.CONFIG_FILES</param-name> <param-value> /WEB-INF/faces-config.xml </param-value> </context-param> <context-param> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <!-- Extensions Filter --> <filter> <filter-name>extensionsFilter</filter-name> <filter-class>org.apache.myfaces.component.html.util.ExtensionsFilter </filter-class> <init-param> <param-name>uploadMaxFileSize</param-name> <param-value>100m</param-value> <description>Set the size limit for uploaded files. Format: 10 - 10 bytes 10k - 10 KB 10m - 10 MB 1g - 1 GB </description> </init-param> <init-param> <param-name>uploadThresholdSize</param-name> <param-value>100k</param-value> <description>Set the threshold size - files below this limit are stored in memory, files above this limit are stored on disk. Format: 10 - 10 bytes 10k - 10 KB 10m - 10 MB 1g - 1 GB </description> </init-param> </filter> <filter-mapping> <filter-name>extensionsFilter</filter-name> <url-pattern>*.jsf</url-pattern> </filter-mapping> <filter-mapping> <filter-name>extensionsFilter</filter-name> <url-pattern>/faces/*</url-pattern> </filter-mapping> <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>*.jsf</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>Now create faces-config.xml and add the following code.
<?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>en</default-locale> </locale-config> <message-bundle>net.roseindia.web.ui.messages </message-bundle> </application> <managed-bean> <managed-bean-name>Bean</managed-bean-name> <managed-bean-class>net.roseindia.web.ui.Bean </managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> <managed-bean> <managed-bean-name>CheckValidUser</managed-bean-name> <managed-bean-class>net.roseindia.web.ui.CheckValidUser </managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> <navigation-rule> <from-view-id>/pages/login.jsp</from-view-id> <navigation-case> <from-outcome>reg</from-outcome> <to-view-id>/pages/registration.jsp</to-view-id> </navigation-case> <navigation-case> <from-outcome>success</from-outcome> <to-view-id>/pages/successLogin.jsp</to-view-id> </navigation-case> <navigation-case> <from-outcome>failure</from-outcome> <to-view-id>/pages/login.jsp</to-view-id> </navigation-case> </navigation-rule> <navigation-rule> <from-view-id>/pages/registration.jsp</from-view-id> <navigation-case> <from-outcome>success</from-outcome> <to-view-id>/pages/welcome.jsp</to-view-id> </navigation-case> <navigation-case> <from-outcome>failure</from-outcome> <to-view-id>/pages/registration.jsp</to-view-id> </navigation-case> </navigation-rule> </faces-config>
Please Note: In the next sections we will
add the configuration
into web.xml and faces-config.xml files, so the content will change. We
advice you to download the full code from our site and then take the files
from there.
<project name="MyFaces, Hibernate and Spring Integration" basedir="../" default="all"> <!-- Project settings --> <property name="project.title" value="RoseIndia MyFaces, Hibernate and Spring Integration Tutorial"/> <property name="project.jar.file" value="roseindia.jar"/> <path id="class.path"> <fileset dir="lib"> <include name="**/*.jar"/> </fileset> <fileset dir="libext"> <include name="**/*.jar"/> </fileset> </path> <!-- Classpath for Project --> <path id="compile.classpath"> <pathelement path ="lib/myfaces-api-1.1.5.jar"/> <pathelement path ="lib/myfaces-impl-1.1.5.jar"/> <pathelement path ="lib/jstl-1.1.0.jar"/> <pathelement path ="lib/tomahawk-1.1.6.jar"/> <pathelement path ="libext/servlet-api.jar"/> <pathelement path ="classes"/> <pathelement path ="${classpath}"/> </path> <!-- Check timestamp on files --> <target name="prepare"> <tstamp/> </target> <!-- Copy any resource or configuration files --> <target name="resources"> <copy todir="src/classes" includeEmptyDirs="no"> <fileset dir="src/java"> <patternset> <include name="**/*.conf"/> <include name="**/*.properties"/> <include name="**/*.xml"/> </patternset> </fileset> </copy> </target> <!-- Normal build of application --> <target name="compile" depends="prepare,resources"> <javac srcdir="src" destdir="src/classes" debug="true" debuglevel="lines,vars,source"> <classpath refid="class.path"/> </javac> <jar jarfile="lib/${project.jar.file}" basedir="src/classes"/> </target> <!-- Remove classes directory for clean build --> <target name="clean" description="Prepare for clean build"> <delete dir="classes"/> <mkdir dir="classes"/> </target> <!-- Build Javadoc documentation --> <target name="javadoc" description="Generate JavaDoc API docs"> <delete dir="./doc/api"/> <mkdir dir="./doc/api"/> <javadoc sourcepath="./src/java" destdir="./doc/api" classpath="${servlet.jar}:${jdbc20ext.jar}" packagenames="*" author="true" private="true" version="true" windowtitle="${project.title} API Documentation" doctitle="<h1>${project.title} Documentation (Version ${project.version})</h1>" bottom="Copyright © 2002"> <classpath refid="compile.classpath"/> </javadoc> </target> <!-- Build entire project --> <target name="project" depends="clean,prepare,compile"/> <!-- Build project and create distribution--> <target name="all" depends="project"/> </project>
We will use ant tool to build the application, so make sure ant tool is installed on your development machine.