In this post, we will setup PrimeFaces 5.3 with Maven and Tomcat. It is assumed that the installations described in this post are already in place.
Maven Archetype
Use Maven webapp Archetype to create application structure
1 2 |
cd C:\tvi\Software\workspace_mars mvn archetype:generate -DgroupId=com.techvalueinsight -DartifactId=PrimeFacesWebAppLab -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false |
Create Folders
Create folders src/main/java, src/test/java and src/test/resources.
Update pom.xml
1 2 3 4 5 6 7 8 9 |
<properties> <java.version>1.8</java.version> <junit.version>4.12</junit.version> <servlet.version>3.1.0</servlet.version> <mojarra.version>2.2.12</mojarra.version> <primefaces.version>5.3</primefaces.version> <maven.compiler.plugin.version>3.3</maven.compiler.plugin.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <!-- Mojarra JSF --> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>${mojarra.version}</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>${mojarra.version}</version> </dependency> <!-- PrimeFaces --> <dependency> <groupId>org.primefaces</groupId> <artifactId>primefaces</artifactId> <version>${primefaces.version}</version> </dependency> </dependencies> |
There are optional dependencies for specific features/components, which can be added as needed (E.g. commons-fileupload and commons-io for FileUpload feature). Similarly, other dependencies like Hibernate/JPA, Spring etc. can be added as the application development progresses, as needed.
Add plugin specifying java version (as desired) inside <build>:
1 2 3 4 5 6 7 8 9 10 11 |
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.plugin.version}</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> |
Update web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>PrimeFaces App</display-name> <welcome-file-list> <welcome-file>views/helloworld.xhtml</welcome-file> </welcome-file-list> <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> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> </web-app> |
Import Maven Project into Eclipse
1 2 |
cd C:\tvi\Software\workspace_mars\PrimeFacesWebAppLab mvn eclipse:clean eclipse:eclipse |
Open Eclipse with C:\tvi\Software\workspace_mars as workspace directory. File –> Import –> Existing Maven Projects. Root Directory C:\tvi\Software\workspace_mars\PrimeFacesWebAppLab. Select PrimeFacesWebAppLab’s pom.xml
Create test xhtml file and Managed bean
In Eclipse project explorer, go to src\main\webapp. Right-click –> New –> xhtml page. Give file name as helloworld.xhtml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"> <h:head> <title>Hello World</title> </h:head> <h:body> <h:form> <h:outputText value="Your Name: " /> <p:inputText value="#{helloWorld.name}" /> <p:commandButton value="Submit" update="sayHelloId" /> <br /><br /> <h:outputText id="sayHelloId" value="#{helloWorld.sayHello()}" /> </h:form> </h:body> </html> |
In Project explorer, go to src\main\java. Right-click –> New –> Other –> Class. Give package name as com.techvalueinsight.jsf.primefaces and class name as HelloWorld.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.techvalueinsight.jsf.primefaces; import javax.faces.bean.ManagedBean; @ManagedBean public class HelloWorld { private String name; public String sayHello() { if (name != null && !name.trim().equals("")) { return "Hello, " + name + "!"; } else { return null; } } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
Deploy on Tomcat from Eclipse
Start Tomcat from the Servers tab
Right-click on the server –> Add And Remove –> Select PrimeFacesWebAppLab
Access the application from browser
Give a name and click Submit: