SlideShare uma empresa Scribd logo
1 de 25
1



 The author has made every effort in the preparation of this book to ensure the accuracy of the information.
However, information in this book is sold without warranty either expressed or implied. The author will not be
    held liable for any damages caused or alleged to be caused either directly or indirectly by this book.




         JSF, Facelets, Spring-JSF & Maven
 Facelets is a powerful templating language built from ground up for JSF.
 Facelets allow for greater code reuse than JSPs. This tutorial provides an
  example of how to work with JSF, Facelets and Spring-JSF along with
                            Maven-2 in Eclipse.




                                                    by




                                        K. Arulkumaran


                                           & A. Sivayini




               Website: http://www.lulu.com/java-success


             Feedback email: java-interview@hotmail.com
2

                                                 Table Of Contents


Notations ..................................................................................................................... 3
Tutorial 7 – JSF, Facelets, Maven & Eclipse...................................................... 4
Tutorial 8 – JSF, Facelets, Spring, Maven & Eclipse..................................... 19
3
                                     Notations

Command prompt:




Eclipse:




File Explorer or Windows Explorer:




Internet Explorer:
4
Tutorial 7 – JSF, Facelets, Maven & Eclipse


This tutorial will guide you through re-building tutorial-3 for simpleWeb with
facelets. It assumes that you have read tutorials 1-3.

The 3rd party library jar files required are:

                  <!-- JSF/JSTL/Facelets -->
                  <dependency>
                        <groupId>javax.faces</groupId>
                        <artifactId>jsf-api</artifactId>
                        <version>1.2</version>
                  </dependency>
                  <dependency>
                        <groupId>javax.faces</groupId>
                        <artifactId>jsf-impl</artifactId>
                        <version>1.2_04</version>
                  </dependency>
                  <dependency>
                        <groupId>com.sun.facelets</groupId>
                        <artifactId>jsf-facelets</artifactId>
                        <version>1.1.11</version>
                  </dependency>
                  <dependency>
                        <groupId>javax.servlet</groupId>
                        <artifactId>jstl</artifactId>
                        <version>1.1.2</version>
                  </dependency>
                  <dependency>
                        <groupId>javax.el</groupId>         Provided means used for
                        <artifactId>el-api</artifactId>     compiling but no need to
                        <version>1.0</version>              package it. This is because
                        <scope>provided</scope>             the Tomcat server has this
                  </dependency>                             package under its lib folder
                  <dependency>
                        <groupId>com.sun.el</groupId>
                        <artifactId>el-ri</artifactId>
                        <version>1.0</version>
                  </dependency>


Artifact el-ri (Expression Language – reference implementation) can be downloaded from the
following repository and all the other jars except jsf-impl (Sun JSF RI ) should be available from the
maven-2 default repository http://repo1.maven.org/maven2/.

         <repositories>
               <repository>
                     <id>maven-repository.dev.java.net</id>
                     <name>Java Dev Net Repository</name>
                     <url>http://download.java.net/maven/2/</url>
                     <releases>
                           <enabled>true</enabled>
                           <updatePolicy>never</updatePolicy>
                     </releases>
                     <snapshots>
                           <enabled>false</enabled>
                     </snapshots>
               </repository>
         </repositories>
5
Step1: Download JSF 1.2_04 P02 from the site https://javaserverfaces.dev.java.net/download.html
into say your download directory. Create a new folder maven_lib under your c:java folder for the
special library files which you can’t download from any maven repositories. Copy the jsf-impl.jar to
your “c:javamaven_lib” folder and rename it to “jsf-impl-1.2_04.jar”.




Now you need to install this JSF implementation jar into your local maven repository in
c:java.m2repository. To do this run the following command in a command prompt:

mvn install:install-file -Dfile=jsf-impl-1.2_04.jar -DgroupId=javax.faces -DartifactId=jsf-impl -
Dversion=1.2_04 -Dpackaging=jar -DgeneratePom=true




After running the above command, you can check for the presence of the jsf-impl-1.2_04.jar file in
your local maven 2 repository “c:java.m2repositoryjavax”.
6




Now the revised pom.xml file under c:tutorialssimpleWeb should look like:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-
         v4_0_0.xsd">

        <modelVersion>4.0.0</modelVersion>
        <groupId>com.mytutorial</groupId>
        <artifactId>simpleWeb</artifactId>
        <packaging>war</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>simpleWeb Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <dependencies>
                  <dependency>
                          <groupId>junit</groupId>
                          <artifactId>junit</artifactId>
                          <version>3.8.1</version>
                          <scope>test</scope>
                  </dependency>

                 <dependency>
                         <groupId>commons-digester</groupId>
                         <artifactId>commons-digester</artifactId>
                         <version>1.8</version>
                 </dependency>

                 <dependency>
                         <groupId>commons-collections</groupId>
                         <artifactId>commons-collections</artifactId>
                         <version>3.2</version>
                 </dependency>
7
        <!-- JSF/JSTL/Facelets -->
        <dependency>
                <groupId>javax.faces</groupId>
                <artifactId>jsf-api</artifactId>
                <version>1.2</version>
        </dependency>
        <dependency>
                <groupId>javax.faces</groupId>
                <artifactId>jsf-impl</artifactId>
                <version>1.2_04</version>
        </dependency>
        <dependency>
                <groupId>com.sun.facelets</groupId>
                <artifactId>jsf-facelets</artifactId>
                <version>1.1.11</version>
        </dependency>
        <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.1.2</version>
        </dependency>
        <dependency>
                <groupId>javax.el</groupId>
                <artifactId>el-api</artifactId>
                <version>1.0</version>
                <scope>provided</scope>
        </dependency>
        <dependency>
                <groupId>com.sun.el</groupId>
                <artifactId>el-ri</artifactId>
                <version>1.0</version>
        </dependency>

</dependencies>

<build>
  <finalName>simpleWeb</finalName>
  <pluginManagement>
     <plugins>
         <plugin>                                               Java compiler
             <groupId>org.apache.maven.plugins</groupId>        JDK 1.5
             <artifactId>maven-compiler-plugin</artifactId>
             <version>2.0.2</version>
                   <configuration>
                      <source>1.5</source>
                     <target>1.5</target>
                   </configuration>
           </plugin>
           <plugin>                                           Using WTP 2.4 but
             <groupId>org.apache.maven.plugins</groupId>      JSF 1.2 requires 2.5,
             <artifactId>maven-eclipse-plugin</artifactId>    which is not yet
             <version>2.4</version>                           available. So refer
             <configuration>                                  Step: WorkAround
                   <downloadSources>false</downloadSources>   to manually change
                   <wtpversion>1.5</wtpversion>               this value.
               </configuration>
           </plugin>
          </plugins>
         </pluginManagement>
</build>

<repositories>
         <repository>
8
                           <id>maven-repository.dev.java.net</id>
                           <name>Java Dev Net Repository</name>
                           <url>http://download.java.net/maven/2/</url>
                           <releases>
                                    <enabled>true</enabled>
                                    <updatePolicy>never</updatePolicy>
                           </releases>
                           <snapshots>                           By default maven uses
                                    <enabled>false</enabled>     http://repo1.maven.org/maven2/
                           </snapshots>                          and your local repository in
                  </repository>                                  c:java.m2repository. Any other
         </repositories>                                         repositories need to be defined in
                                                                 the pom.xml file. el-i-1.0.jar can
</project>                                                       be found at this repository.

Note: if a particular jar file is not available in any repositories,
it can be downloaded separately and installed in to your local
repository c:java.m2repository using “mvn install:install-file …” as shown above.

Step-2: If you are already inside eclipse, exit out of it and run the following maven command from
c:tutorialssimpleWeb to generate eclipse build path (i.e. classpath).




STEP: WorkAround

The JSF 1.2 requires eclipse web facet 2.5. You need to open the file
“org.eclipse.wst.common.project.facet.core.xml” under C:tutorialssimpleWeb.settings as shown
below from version=2.4 to version=2.5. Every time you use the eclipse:clean command, you will have
to manually fix this up as shown below.




Step-3: Now get back into eclipse and click “F5” for refresh on the “simpleWeb” project. The
required files need to be completed as shown below:
9




PersonBean.java (Model)

package com.mytutorial;

public class PersonBean {
      String personName;

       public String getPersonName() {
             return personName;
       }

       public void setPersonName(String personName) {
             this.personName = personName;
       }
}


PersonBeanController.java (Controller)


package com.mytutorial;

public class PersonControllerBean {

       PersonBean person = new PersonBean(); //later on we will inject
                                             //this using spring

       public String getPersonName() {
             return person.getPersonName();
       }

       public void setPersonName(String personName) {
             person.setPersonName(personName);
       }

       public PersonBean getPerson() {
             return person;
       }
10

        public void setPerson(PersonBean person) {
              this.person = person;
        }

}


messages.properties (same as before in tutorial-3)

inputname_header=JSF Tutorial
prompt=Tell me your name:
greeting_text=Welcome to JSF
button_text=Hello
sign=!


greeting.jspx (page)

Note: The recommended extension for pages by facelets is the “.xhtml”. Since eclipse does not
recognize this and would not provide you code assist when you press ctrl-Space. So the work around is
to use the extension .jspx to solve this problem. These .jspx files can be opened using the webpage
editor provided by eclipse 3.3 WTP. To open the greeting.jspx in a webpage editor right click on it and
select “other” and then “WebPage Editor”
11




The completed “greeting.jspx” should look like:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets" version="2.0">

         <ui:composition>
           <html>
               <head>
                  <title>greeting page</title>
               </head>
               <body>
                   <f:loadBundle basename="com.mytutorial.messages" var="msg" />
                     <h3
                         <h:outputText value="#{msg.greeting_text}" />, <h:outputText
                                    value="#{personBean.personName}" />
                         <h:outputText value="#{msg.sign}" />
                     </h3>                                                   Defined in faces-
                                                                             config.xml
                </body>
             </html>
           </ui:composition>
</jsp:root>
12




inputname.jspx

<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets" version="2.0">

        <ui:composition>
          <html>
              <head>
                <meta http-equiv="Content-Type"
                         content="text/html; charset=ISO-8859-1" />
                 <title>Insert title here</title>
               </head>
               <body>
                <f:view>
                    <f:loadBundle basename="com.mytutorial.messages" var="msg" />
                        <h3>
                          <h:form id="helloForm">
                             <h:outputText value="#{msg.prompt}" />
                             <h:inputText value="#{personBean.personName}" />

                                <h:commandButton action="greeting" value="#{msg.button_text}" />
                                <h:outputText></h:outputText>
                              </h:form>
                             </h3>
                      </f:view>
                     </body>
                   </html>
              </ui:composition>
</jsp:root>
13




faces-config.xml

<?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>

        <navigation-rule>
                <from-view-id>/pages/inputname.jspx</from-view-id>
                <navigation-case>
                         <from-outcome>greeting</from-outcome>
                         <to-view-id>/pages/greeting.jspx</to-view-id>
                </navigation-case>
        </navigation-rule>
                                                         Used in JSF pages

        <managed-bean>
               <managed-bean-name>personBean</managed-bean-name>
               <managed-bean-class>
                       com.mytutorial.PersonControllerBean
               </managed-bean-class>
               <managed-bean-scope>request</managed-bean-scope>
        </managed-bean>


        <application>
                 <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
        </application>

</faces-config>
14




web.xml

<!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>
       <context-param>
               <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
               <param-value>server</param-value>
       </context-param>

          <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.DEFAULT_SUFFIX</param-name>
                  <param-value>.jspx</param-value>
          </context-param>

          <!-- Special Debug Output for Development -->
          <context-param>
                   <param-name>facelets.DEVELOPMENT</param-name>
                   <param-value>true</param-value>
          </context-param>

          <!-- Optional JSF-RI Parameters to Help Debug -->
          <context-param>
                   <param-name>com.sun.faces.validateXml</param-name>
                   <param-value>true</param-value>
          </context-param>
          <context-param>
                   <param-name>com.sun.faces.verifyObjects</param-name>
                   <param-value>true</param-value>
15
        </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>*.jsf</url-pattern>
        </servlet-mapping>
</web-app>




Finally the “index.jspx”

<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core" version="2.0">

        <ui:composition>
              <html>
              <body>

                 <f:view>
                       <a href="pages/inputname.jsf">Click Me</a>
                       <br />
                 </f:view>
                 </body>
16
            </html>
      </ui:composition>
</jsp:root>




You can now try to build it and deploy it to tomcat as discussed in tutorial-3. Try deploying it from
both inside eclipse & outside. You can check your deployed war file from inside eclipse under
following folder (check if it is properly packaged):




Important!!
17
It is important to note that, if you are deploying to Tomcat inside eclipse, remember to exclude the jar
files which are already available under Tomcat’s lib directory. So you need to remove the el-api-
1.0.jar file from getting packaged. You could do this inside eclipse by right clicking on simpleWeb
and then selecting “properties”. Under J2EE module dependencies make sure that el-api-1.0.jar is
unticked. Only the ticked files make it to the WEB-INlib folder. If you build the war file outside
eclipse then maven pom.xml file will take care of this, since its scope is declared as “provided”.




The URL to use after you deploy/publish and start the Tomcat server:

http://localhost:8080/simpleWeb/index.jsf
18




Please feel free to email any errors to java-interview@hotmail.com. Also stay tuned at
    http://www.lulu.com/java-success for more tutorials and Java/J2EE interview
                                        resources.
19
Tutorial 8 – JSF, Facelets, Spring, Maven & Eclipse


This is the continuation of tutorial-7 for simpleWeb with Spring. It assumes
that you have read tutorials 1-3 and tutorial 7. You need to make use of
Spring with JSF:


Add spring.jar to the pom.xml file under c:tutorialssimpleWeb

<dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring</artifactId>
       <version>2.0.6</version>
</dependency>




After adding, save it and exit out of eclipse and run the following mvn (Maven) command
from the command line.


mvn eclipse:eclipse
20




STEP: WorkAround

The JSF 1.2 requires eclipse web facet 2.5. You need to open the file
“org.eclipse.wst.common.project.facet.core.xml” under C:tutorialssimpleWeb.settings as show
below from version=2.4 to version=2.5. Every time you use the eclipse:clean command, you will have
to manually fix this up as shown below.




You can now open eclipse and refresh (i.e. F5) simpleWeb project. After this if you check
your eclipse build path, it should look like below with spring-2.0.6.jar.
21




Also check your project facet to make sure that dynamic web module is 2.5. If not repeat step marked
“Work Around” above after exiting eclipse.



To use Spring make the following changes to the existing artifacts and also add the
“applicationContext.xml” file under WEB-INF.



PersonControllerBean.java

Note the comments in bold. Spring will inject this.

package com.mytutorial;

public class PersonControllerBean {

         PersonBean person; //removed instantiation.
                            //Spring will inject this using the setter.

         public String getPersonName() {
               return person.getPersonName();
         }

         public void setPersonName(String personName) {
               person.setPersonName(personName);
         }

         public PersonBean getPerson() {
               return person;
         }

         public void setPerson(PersonBean person) {
               this.person = person;
         }

}


Let’s add the applicationContext.xml file
22
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

        <bean id="person" class="com.mytutorial.PersonBean" />

        <bean id="personBean" class="com.mytutorial.PersonControllerBean" scope="session">
                <property name="person" ref="person" />
        </bean>

</beans>




Now make the required changes to the descriptor files web.xml & faces-config.xml.

faces.-config.xml
Remove the <managed-bean> declaration and add the <variable-resolver> under <application>.

<?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>

        <navigation-rule>
                <from-view-id>/pages/inputname.jspx</from-view-id>
                <navigation-case>
                         <from-outcome>greeting</from-outcome>
                         <to-view-id>/pages/greeting.jspx</to-view-id>
                </navigation-case>
        </navigation-rule>

        <!-- To be removed, Spring will take care of this
        <managed-bean>
23
               <managed-bean-name>personBean</managed-bean-name>
               <managed-bean-class>
                       com.mytutorial.PersonControllerBean
               </managed-bean-class>
               <managed-bean-scope>request</managed-bean-scope>
        </managed-bean>

        -->
                                                         Added

        <application>
           <variable-resolver>
                org.springframework.web.jsf.DelegatingVariableResolver
            </variable-resolver>
            <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
        </application>

</faces-config>




Finally add listeners and context-param to the web.xml

web.xml:

<!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>
          <context-param>
                  <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
                  <param-value>server</param-value>
          </context-param>
          <context-param>
                  <param-name>javax.faces.CONFIG_FILES</param-name>
24
                <param-value>/WEB-INF/faces-config.xml</param-value>
        </context-param>
        <context-param>
                <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
                <param-value>.jspx</param-value>
        </context-param>                                             Added
        <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>

        <!-- Special Debug Output for Development -->
        <context-param>
                 <param-name>facelets.DEVELOPMENT</param-name>
                 <param-value>true</param-value>
        </context-param>
        <!-- Optional JSF-RI Parameters to Help Debug -->
        <context-param>
                 <param-name>com.sun.faces.validateXml</param-name>
                 <param-value>true</param-value>
        </context-param>
        <context-param>
                 <param-name>com.sun.faces.verifyObjects</param-name>
                 <param-value>true</param-value>
        </context-param>

        <listener>
              <listener-class>
                     org.springframework.web.context.ContextLoaderListener
              </listener-class>
        </listener>
                                                                          Added
        <listener>
              <listener-class>
                    org.springframework.web.context.request.RequestContextListener
              </listener-class>
          </listener>

        <!-- 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>*.jsf</url-pattern>
        </servlet-mapping>
</web-app>
25




The URL to use after you deploy/publish and start the Tomcat server:

http://localhost:8080/simpleWeb/index.jsf


Run the application as before and you should see the same output.
This time with Spring’s dependency injection.




Please feel free to email any errors to java-interview@hotmail.com. Also stay tuned at
    http://www.lulu.com/java-success for more tutorials and Java/J2EE interview
                                        resources.

Mais conteúdo relacionado

Mais procurados

Enterprise Build And Test In The Cloud
Enterprise Build And Test In The CloudEnterprise Build And Test In The Cloud
Enterprise Build And Test In The Cloud
Carlos Sanchez
 
JavaDo#09 Spring boot入門ハンズオン
JavaDo#09 Spring boot入門ハンズオンJavaDo#09 Spring boot入門ハンズオン
JavaDo#09 Spring boot入門ハンズオン
haruki ueno
 
Enterprise Build And Test In The Cloud
Enterprise Build And Test In The CloudEnterprise Build And Test In The Cloud
Enterprise Build And Test In The Cloud
Carlos Sanchez
 

Mais procurados (20)

Ajax, JSF, Facelets, Eclipse & Maven tutorials
Ajax, JSF, Facelets, Eclipse & Maven tutorialsAjax, JSF, Facelets, Eclipse & Maven tutorials
Ajax, JSF, Facelets, Eclipse & Maven tutorials
 
Java EE 6 & GlassFish = Less Code + More Power at CEJUG
Java EE 6 & GlassFish = Less Code + More Power at CEJUGJava EE 6 & GlassFish = Less Code + More Power at CEJUG
Java EE 6 & GlassFish = Less Code + More Power at CEJUG
 
Releasing Projects Using Maven
Releasing Projects Using MavenReleasing Projects Using Maven
Releasing Projects Using Maven
 
Java EE 6 = Less Code + More Power
Java EE 6 = Less Code + More PowerJava EE 6 = Less Code + More Power
Java EE 6 = Less Code + More Power
 
Enterprise Maven Repository BOF
Enterprise Maven Repository BOFEnterprise Maven Repository BOF
Enterprise Maven Repository BOF
 
Enterprise Build And Test In The Cloud
Enterprise Build And Test In The CloudEnterprise Build And Test In The Cloud
Enterprise Build And Test In The Cloud
 
Google app engine by example
Google app engine by exampleGoogle app engine by example
Google app engine by example
 
Hibernate, Spring, Eclipse, HSQL Database & Maven tutorial
Hibernate, Spring, Eclipse, HSQL Database & Maven tutorialHibernate, Spring, Eclipse, HSQL Database & Maven tutorial
Hibernate, Spring, Eclipse, HSQL Database & Maven tutorial
 
Java EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for futureJava EE 6 and GlassFish v3: Paving the path for future
Java EE 6 and GlassFish v3: Paving the path for future
 
JavaDo#09 Spring boot入門ハンズオン
JavaDo#09 Spring boot入門ハンズオンJavaDo#09 Spring boot入門ハンズオン
JavaDo#09 Spring boot入門ハンズオン
 
Java EE 6 & GlassFish v3: Paving path for the future
Java EE 6 & GlassFish v3: Paving path for the futureJava EE 6 & GlassFish v3: Paving path for the future
Java EE 6 & GlassFish v3: Paving path for the future
 
Spring boot for buidling microservices
Spring boot for buidling microservicesSpring boot for buidling microservices
Spring boot for buidling microservices
 
Algotitmo Moinho
Algotitmo MoinhoAlgotitmo Moinho
Algotitmo Moinho
 
Maven
MavenMaven
Maven
 
Maven
MavenMaven
Maven
 
Simple module Development in Joomla! 2.5
Simple module Development in Joomla! 2.5Simple module Development in Joomla! 2.5
Simple module Development in Joomla! 2.5
 
Maven, Eclipse And OSGi Working Together
Maven, Eclipse And OSGi Working TogetherMaven, Eclipse And OSGi Working Together
Maven, Eclipse And OSGi Working Together
 
Enterprise Build And Test In The Cloud
Enterprise Build And Test In The CloudEnterprise Build And Test In The Cloud
Enterprise Build And Test In The Cloud
 
Spring boot入門ハンズオン第二回
Spring boot入門ハンズオン第二回Spring boot入門ハンズオン第二回
Spring boot入門ハンズオン第二回
 
Install Samples
Install SamplesInstall Samples
Install Samples
 

Destaque

Attack_Simulation_and_Threat_Modeling
Attack_Simulation_and_Threat_ModelingAttack_Simulation_and_Threat_Modeling
Attack_Simulation_and_Threat_Modeling
Oluseyi Akindeinde
 
2011 New Product Showcase
2011 New Product Showcase2011 New Product Showcase
2011 New Product Showcase
bmmitt
 

Destaque (20)

Introduction to Ext JS 4
Introduction to Ext JS 4Introduction to Ext JS 4
Introduction to Ext JS 4
 
Medication Reconciliation in Electronic Health Information Exchange
Medication Reconciliation in Electronic Health Information ExchangeMedication Reconciliation in Electronic Health Information Exchange
Medication Reconciliation in Electronic Health Information Exchange
 
Proceedings online v2
Proceedings online v2Proceedings online v2
Proceedings online v2
 
JSF2 and JSP
JSF2 and JSPJSF2 and JSP
JSF2 and JSP
 
document
documentdocument
document
 
Attack_Simulation_and_Threat_Modeling
Attack_Simulation_and_Threat_ModelingAttack_Simulation_and_Threat_Modeling
Attack_Simulation_and_Threat_Modeling
 
Search engines
Search enginesSearch engines
Search engines
 
Xakep
XakepXakep
Xakep
 
Google
GoogleGoogle
Google
 
互联网搜索技巧
互联网搜索技巧互联网搜索技巧
互联网搜索技巧
 
Tomislav Capan - Muzika Hr (IT Showoff)
Tomislav Capan - Muzika Hr (IT Showoff)Tomislav Capan - Muzika Hr (IT Showoff)
Tomislav Capan - Muzika Hr (IT Showoff)
 
IoF South West Conference
IoF South West ConferenceIoF South West Conference
IoF South West Conference
 
Adobe Digital Publishing Suite by dualpixel
Adobe Digital Publishing Suite by dualpixelAdobe Digital Publishing Suite by dualpixel
Adobe Digital Publishing Suite by dualpixel
 
2011 New Product Showcase
2011 New Product Showcase2011 New Product Showcase
2011 New Product Showcase
 
Web Application Hacking 2004
Web Application Hacking 2004Web Application Hacking 2004
Web Application Hacking 2004
 
Ticer2005
Ticer2005Ticer2005
Ticer2005
 
Training for Foster Parents
Training for Foster ParentsTraining for Foster Parents
Training for Foster Parents
 
Shanghai big tradeshow calendar 2014 collection by MARKYE@LIERJIA.CN
Shanghai big tradeshow calendar 2014 collection by MARKYE@LIERJIA.CN Shanghai big tradeshow calendar 2014 collection by MARKYE@LIERJIA.CN
Shanghai big tradeshow calendar 2014 collection by MARKYE@LIERJIA.CN
 
Unemployment
UnemploymentUnemployment
Unemployment
 
Catalog
CatalogCatalog
Catalog
 

Semelhante a JSF, Facelets, Spring-JSF & Maven

Soft shake 2013 - make use of sonar on your mobile developments
Soft shake 2013 - make use of sonar on your mobile developmentsSoft shake 2013 - make use of sonar on your mobile developments
Soft shake 2013 - make use of sonar on your mobile developments
rfelden
 
Note - Apache Maven Intro
Note - Apache Maven IntroNote - Apache Maven Intro
Note - Apache Maven Intro
boyw165
 

Semelhante a JSF, Facelets, Spring-JSF & Maven (20)

Pom configuration java xml
Pom configuration java xmlPom configuration java xml
Pom configuration java xml
 
Spring Web Service, Spring JMS, Eclipse & Maven tutorials
Spring Web Service, Spring JMS, Eclipse & Maven tutorialsSpring Web Service, Spring JMS, Eclipse & Maven tutorials
Spring Web Service, Spring JMS, Eclipse & Maven tutorials
 
Apache Maven basics
Apache Maven basicsApache Maven basics
Apache Maven basics
 
Make use of Sonar for your mobile developments - It's easy and useful!
Make use of Sonar for your mobile developments - It's easy and useful!Make use of Sonar for your mobile developments - It's easy and useful!
Make use of Sonar for your mobile developments - It's easy and useful!
 
Soft shake 2013 - make use of sonar on your mobile developments
Soft shake 2013 - make use of sonar on your mobile developmentsSoft shake 2013 - make use of sonar on your mobile developments
Soft shake 2013 - make use of sonar on your mobile developments
 
Hands On with Maven
Hands On with MavenHands On with Maven
Hands On with Maven
 
Introduction To Maven2
Introduction To Maven2Introduction To Maven2
Introduction To Maven2
 
Using Maven2
Using Maven2Using Maven2
Using Maven2
 
Maven in Mule
Maven in MuleMaven in Mule
Maven in Mule
 
Ant, Maven and Jenkins
Ant, Maven and JenkinsAnt, Maven and Jenkins
Ant, Maven and Jenkins
 
Gradle: From Extreme to Mainstream
Gradle: From Extreme to MainstreamGradle: From Extreme to Mainstream
Gradle: From Extreme to Mainstream
 
An Introduction to Maven and Flex
An Introduction to Maven and FlexAn Introduction to Maven and Flex
An Introduction to Maven and Flex
 
Pom
PomPom
Pom
 
Apache Maven supports all Java (JokerConf 2018)
Apache Maven supports all Java (JokerConf 2018)Apache Maven supports all Java (JokerConf 2018)
Apache Maven supports all Java (JokerConf 2018)
 
AOP sec3.pptx
AOP sec3.pptxAOP sec3.pptx
AOP sec3.pptx
 
Apache Maven for AT/QC
Apache Maven for AT/QCApache Maven for AT/QC
Apache Maven for AT/QC
 
Note - Apache Maven Intro
Note - Apache Maven IntroNote - Apache Maven Intro
Note - Apache Maven Intro
 
Spring Boot and JHipster
Spring Boot and JHipsterSpring Boot and JHipster
Spring Boot and JHipster
 
Hibernate 5 – merge() Example
Hibernate 5 – merge() ExampleHibernate 5 – merge() Example
Hibernate 5 – merge() Example
 
Sel study notes
Sel study notesSel study notes
Sel study notes
 

Mais de Raghavan Mohan (12)

Accelerate with BIRT and Actuate11
Accelerate with BIRT and Actuate11Accelerate with BIRT and Actuate11
Accelerate with BIRT and Actuate11
 
Who is BIRT
Who is BIRTWho is BIRT
Who is BIRT
 
Introduction to BIRT
Introduction to BIRTIntroduction to BIRT
Introduction to BIRT
 
Sachin Tendulkar Resume
Sachin Tendulkar ResumeSachin Tendulkar Resume
Sachin Tendulkar Resume
 
Manmohan Singh Resume
Manmohan Singh ResumeManmohan Singh Resume
Manmohan Singh Resume
 
Senator Barrack Obama Resume
Senator Barrack Obama ResumeSenator Barrack Obama Resume
Senator Barrack Obama Resume
 
Java/J2EE CV Guide
Java/J2EE CV GuideJava/J2EE CV Guide
Java/J2EE CV Guide
 
Java/J2EE Companion
Java/J2EE CompanionJava/J2EE Companion
Java/J2EE Companion
 
Fast Track to Ajax.
Fast Track to Ajax.Fast Track to Ajax.
Fast Track to Ajax.
 
23617968 digit-fast-track-jan-2009-php
23617968 digit-fast-track-jan-2009-php23617968 digit-fast-track-jan-2009-php
23617968 digit-fast-track-jan-2009-php
 
Quality - Douglas Crockford
Quality - Douglas CrockfordQuality - Douglas Crockford
Quality - Douglas Crockford
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

JSF, Facelets, Spring-JSF & Maven

  • 1. 1 The author has made every effort in the preparation of this book to ensure the accuracy of the information. However, information in this book is sold without warranty either expressed or implied. The author will not be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. JSF, Facelets, Spring-JSF & Maven Facelets is a powerful templating language built from ground up for JSF. Facelets allow for greater code reuse than JSPs. This tutorial provides an example of how to work with JSF, Facelets and Spring-JSF along with Maven-2 in Eclipse. by K. Arulkumaran & A. Sivayini Website: http://www.lulu.com/java-success Feedback email: java-interview@hotmail.com
  • 2. 2 Table Of Contents Notations ..................................................................................................................... 3 Tutorial 7 – JSF, Facelets, Maven & Eclipse...................................................... 4 Tutorial 8 – JSF, Facelets, Spring, Maven & Eclipse..................................... 19
  • 3. 3 Notations Command prompt: Eclipse: File Explorer or Windows Explorer: Internet Explorer:
  • 4. 4 Tutorial 7 – JSF, Facelets, Maven & Eclipse This tutorial will guide you through re-building tutorial-3 for simpleWeb with facelets. It assumes that you have read tutorials 1-3. The 3rd party library jar files required are: <!-- JSF/JSTL/Facelets --> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-impl</artifactId> <version>1.2_04</version> </dependency> <dependency> <groupId>com.sun.facelets</groupId> <artifactId>jsf-facelets</artifactId> <version>1.1.11</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.el</groupId> Provided means used for <artifactId>el-api</artifactId> compiling but no need to <version>1.0</version> package it. This is because <scope>provided</scope> the Tomcat server has this </dependency> package under its lib folder <dependency> <groupId>com.sun.el</groupId> <artifactId>el-ri</artifactId> <version>1.0</version> </dependency> Artifact el-ri (Expression Language – reference implementation) can be downloaded from the following repository and all the other jars except jsf-impl (Sun JSF RI ) should be available from the maven-2 default repository http://repo1.maven.org/maven2/. <repositories> <repository> <id>maven-repository.dev.java.net</id> <name>Java Dev Net Repository</name> <url>http://download.java.net/maven/2/</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
  • 5. 5 Step1: Download JSF 1.2_04 P02 from the site https://javaserverfaces.dev.java.net/download.html into say your download directory. Create a new folder maven_lib under your c:java folder for the special library files which you can’t download from any maven repositories. Copy the jsf-impl.jar to your “c:javamaven_lib” folder and rename it to “jsf-impl-1.2_04.jar”. Now you need to install this JSF implementation jar into your local maven repository in c:java.m2repository. To do this run the following command in a command prompt: mvn install:install-file -Dfile=jsf-impl-1.2_04.jar -DgroupId=javax.faces -DartifactId=jsf-impl - Dversion=1.2_04 -Dpackaging=jar -DgeneratePom=true After running the above command, you can check for the presence of the jsf-impl-1.2_04.jar file in your local maven 2 repository “c:java.m2repositoryjavax”.
  • 6. 6 Now the revised pom.xml file under c:tutorialssimpleWeb should look like: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven- v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mytutorial</groupId> <artifactId>simpleWeb</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>simpleWeb Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-digester</groupId> <artifactId>commons-digester</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2</version> </dependency>
  • 7. 7 <!-- JSF/JSTL/Facelets --> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-impl</artifactId> <version>1.2_04</version> </dependency> <dependency> <groupId>com.sun.facelets</groupId> <artifactId>jsf-facelets</artifactId> <version>1.1.11</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.el</groupId> <artifactId>el-api</artifactId> <version>1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.sun.el</groupId> <artifactId>el-ri</artifactId> <version>1.0</version> </dependency> </dependencies> <build> <finalName>simpleWeb</finalName> <pluginManagement> <plugins> <plugin> Java compiler <groupId>org.apache.maven.plugins</groupId> JDK 1.5 <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> Using WTP 2.4 but <groupId>org.apache.maven.plugins</groupId> JSF 1.2 requires 2.5, <artifactId>maven-eclipse-plugin</artifactId> which is not yet <version>2.4</version> available. So refer <configuration> Step: WorkAround <downloadSources>false</downloadSources> to manually change <wtpversion>1.5</wtpversion> this value. </configuration> </plugin> </plugins> </pluginManagement> </build> <repositories> <repository>
  • 8. 8 <id>maven-repository.dev.java.net</id> <name>Java Dev Net Repository</name> <url>http://download.java.net/maven/2/</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> </releases> <snapshots> By default maven uses <enabled>false</enabled> http://repo1.maven.org/maven2/ </snapshots> and your local repository in </repository> c:java.m2repository. Any other </repositories> repositories need to be defined in the pom.xml file. el-i-1.0.jar can </project> be found at this repository. Note: if a particular jar file is not available in any repositories, it can be downloaded separately and installed in to your local repository c:java.m2repository using “mvn install:install-file …” as shown above. Step-2: If you are already inside eclipse, exit out of it and run the following maven command from c:tutorialssimpleWeb to generate eclipse build path (i.e. classpath). STEP: WorkAround The JSF 1.2 requires eclipse web facet 2.5. You need to open the file “org.eclipse.wst.common.project.facet.core.xml” under C:tutorialssimpleWeb.settings as shown below from version=2.4 to version=2.5. Every time you use the eclipse:clean command, you will have to manually fix this up as shown below. Step-3: Now get back into eclipse and click “F5” for refresh on the “simpleWeb” project. The required files need to be completed as shown below:
  • 9. 9 PersonBean.java (Model) package com.mytutorial; public class PersonBean { String personName; public String getPersonName() { return personName; } public void setPersonName(String personName) { this.personName = personName; } } PersonBeanController.java (Controller) package com.mytutorial; public class PersonControllerBean { PersonBean person = new PersonBean(); //later on we will inject //this using spring public String getPersonName() { return person.getPersonName(); } public void setPersonName(String personName) { person.setPersonName(personName); } public PersonBean getPerson() { return person; }
  • 10. 10 public void setPerson(PersonBean person) { this.person = person; } } messages.properties (same as before in tutorial-3) inputname_header=JSF Tutorial prompt=Tell me your name: greeting_text=Welcome to JSF button_text=Hello sign=! greeting.jspx (page) Note: The recommended extension for pages by facelets is the “.xhtml”. Since eclipse does not recognize this and would not provide you code assist when you press ctrl-Space. So the work around is to use the extension .jspx to solve this problem. These .jspx files can be opened using the webpage editor provided by eclipse 3.3 WTP. To open the greeting.jspx in a webpage editor right click on it and select “other” and then “WebPage Editor”
  • 11. 11 The completed “greeting.jspx” should look like: <?xml version="1.0" encoding="ISO-8859-1" ?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" version="2.0"> <ui:composition> <html> <head> <title>greeting page</title> </head> <body> <f:loadBundle basename="com.mytutorial.messages" var="msg" /> <h3 <h:outputText value="#{msg.greeting_text}" />, <h:outputText value="#{personBean.personName}" /> <h:outputText value="#{msg.sign}" /> </h3> Defined in faces- config.xml </body> </html> </ui:composition> </jsp:root>
  • 12. 12 inputname.jspx <?xml version="1.0" encoding="ISO-8859-1" ?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" version="2.0"> <ui:composition> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Insert title here</title> </head> <body> <f:view> <f:loadBundle basename="com.mytutorial.messages" var="msg" /> <h3> <h:form id="helloForm"> <h:outputText value="#{msg.prompt}" /> <h:inputText value="#{personBean.personName}" /> <h:commandButton action="greeting" value="#{msg.button_text}" /> <h:outputText></h:outputText> </h:form> </h3> </f:view> </body> </html> </ui:composition> </jsp:root>
  • 13. 13 faces-config.xml <?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> <navigation-rule> <from-view-id>/pages/inputname.jspx</from-view-id> <navigation-case> <from-outcome>greeting</from-outcome> <to-view-id>/pages/greeting.jspx</to-view-id> </navigation-case> </navigation-rule> Used in JSF pages <managed-bean> <managed-bean-name>personBean</managed-bean-name> <managed-bean-class> com.mytutorial.PersonControllerBean </managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> <application> <view-handler>com.sun.facelets.FaceletViewHandler</view-handler> </application> </faces-config>
  • 14. 14 web.xml <!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> <context-param> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>server</param-value> </context-param> <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.DEFAULT_SUFFIX</param-name> <param-value>.jspx</param-value> </context-param> <!-- Special Debug Output for Development --> <context-param> <param-name>facelets.DEVELOPMENT</param-name> <param-value>true</param-value> </context-param> <!-- Optional JSF-RI Parameters to Help Debug --> <context-param> <param-name>com.sun.faces.validateXml</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>com.sun.faces.verifyObjects</param-name> <param-value>true</param-value>
  • 15. 15 </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>*.jsf</url-pattern> </servlet-mapping> </web-app> Finally the “index.jspx” <?xml version="1.0" encoding="ISO-8859-1" ?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" version="2.0"> <ui:composition> <html> <body> <f:view> <a href="pages/inputname.jsf">Click Me</a> <br /> </f:view> </body>
  • 16. 16 </html> </ui:composition> </jsp:root> You can now try to build it and deploy it to tomcat as discussed in tutorial-3. Try deploying it from both inside eclipse & outside. You can check your deployed war file from inside eclipse under following folder (check if it is properly packaged): Important!!
  • 17. 17 It is important to note that, if you are deploying to Tomcat inside eclipse, remember to exclude the jar files which are already available under Tomcat’s lib directory. So you need to remove the el-api- 1.0.jar file from getting packaged. You could do this inside eclipse by right clicking on simpleWeb and then selecting “properties”. Under J2EE module dependencies make sure that el-api-1.0.jar is unticked. Only the ticked files make it to the WEB-INlib folder. If you build the war file outside eclipse then maven pom.xml file will take care of this, since its scope is declared as “provided”. The URL to use after you deploy/publish and start the Tomcat server: http://localhost:8080/simpleWeb/index.jsf
  • 18. 18 Please feel free to email any errors to java-interview@hotmail.com. Also stay tuned at http://www.lulu.com/java-success for more tutorials and Java/J2EE interview resources.
  • 19. 19 Tutorial 8 – JSF, Facelets, Spring, Maven & Eclipse This is the continuation of tutorial-7 for simpleWeb with Spring. It assumes that you have read tutorials 1-3 and tutorial 7. You need to make use of Spring with JSF: Add spring.jar to the pom.xml file under c:tutorialssimpleWeb <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.0.6</version> </dependency> After adding, save it and exit out of eclipse and run the following mvn (Maven) command from the command line. mvn eclipse:eclipse
  • 20. 20 STEP: WorkAround The JSF 1.2 requires eclipse web facet 2.5. You need to open the file “org.eclipse.wst.common.project.facet.core.xml” under C:tutorialssimpleWeb.settings as show below from version=2.4 to version=2.5. Every time you use the eclipse:clean command, you will have to manually fix this up as shown below. You can now open eclipse and refresh (i.e. F5) simpleWeb project. After this if you check your eclipse build path, it should look like below with spring-2.0.6.jar.
  • 21. 21 Also check your project facet to make sure that dynamic web module is 2.5. If not repeat step marked “Work Around” above after exiting eclipse. To use Spring make the following changes to the existing artifacts and also add the “applicationContext.xml” file under WEB-INF. PersonControllerBean.java Note the comments in bold. Spring will inject this. package com.mytutorial; public class PersonControllerBean { PersonBean person; //removed instantiation. //Spring will inject this using the setter. public String getPersonName() { return person.getPersonName(); } public void setPersonName(String personName) { person.setPersonName(personName); } public PersonBean getPerson() { return person; } public void setPerson(PersonBean person) { this.person = person; } } Let’s add the applicationContext.xml file
  • 22. 22 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <bean id="person" class="com.mytutorial.PersonBean" /> <bean id="personBean" class="com.mytutorial.PersonControllerBean" scope="session"> <property name="person" ref="person" /> </bean> </beans> Now make the required changes to the descriptor files web.xml & faces-config.xml. faces.-config.xml Remove the <managed-bean> declaration and add the <variable-resolver> under <application>. <?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> <navigation-rule> <from-view-id>/pages/inputname.jspx</from-view-id> <navigation-case> <from-outcome>greeting</from-outcome> <to-view-id>/pages/greeting.jspx</to-view-id> </navigation-case> </navigation-rule> <!-- To be removed, Spring will take care of this <managed-bean>
  • 23. 23 <managed-bean-name>personBean</managed-bean-name> <managed-bean-class> com.mytutorial.PersonControllerBean </managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> --> Added <application> <variable-resolver> org.springframework.web.jsf.DelegatingVariableResolver </variable-resolver> <view-handler>com.sun.facelets.FaceletViewHandler</view-handler> </application> </faces-config> Finally add listeners and context-param to the web.xml web.xml: <!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> <context-param> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>server</param-value> </context-param> <context-param> <param-name>javax.faces.CONFIG_FILES</param-name>
  • 24. 24 <param-value>/WEB-INF/faces-config.xml</param-value> </context-param> <context-param> <param-name>javax.faces.DEFAULT_SUFFIX</param-name> <param-value>.jspx</param-value> </context-param> Added <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- Special Debug Output for Development --> <context-param> <param-name>facelets.DEVELOPMENT</param-name> <param-value>true</param-value> </context-param> <!-- Optional JSF-RI Parameters to Help Debug --> <context-param> <param-name>com.sun.faces.validateXml</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>com.sun.faces.verifyObjects</param-name> <param-value>true</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> Added <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> <!-- 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>*.jsf</url-pattern> </servlet-mapping> </web-app>
  • 25. 25 The URL to use after you deploy/publish and start the Tomcat server: http://localhost:8080/simpleWeb/index.jsf Run the application as before and you should see the same output. This time with Spring’s dependency injection. Please feel free to email any errors to java-interview@hotmail.com. Also stay tuned at http://www.lulu.com/java-success for more tutorials and Java/J2EE interview resources.