JSF, Facelets, Spring-JSF & Maven

1,815 views

Published on

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.

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,815
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

JSF, Facelets, Spring-JSF & Maven

  1. 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. 2 Table Of Contents Notations ..................................................................................................................... 3 Tutorial 7 – JSF, Facelets, Maven & Eclipse...................................................... 4 Tutorial 8 – JSF, Facelets, Spring, Maven & Eclipse..................................... 19
  3. 3. 3 Notations Command prompt: Eclipse: File Explorer or Windows Explorer: Internet Explorer:
  4. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.

×