A simple Primefaces,Maven,Eclipse,JSF 2.2 application part2

ok doing all things from the first tutorial, we got the chage the stuff in the web.xml (if it is not already like this)

to the following:

<?xml version="1.0" encoding="UTF-8"?>
<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/index.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>

 

and then update the pom.xml already to including primefaces libs. and the hibernate libs in case we wont to use db connection

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.kingsteff.pps</groupId>
  <artifactId>ppsnew2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 <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>
        <hibernate.version>5.2.5.Final</hibernate.version>  
    </properties>
<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>
        <!-- JPA -->
 
    <dependency>
 
      <groupId>org.hibernate</groupId>
 
      <artifactId>hibernate-entitymanager</artifactId>
 
      <version>${hibernate.version}</version>
 
    </dependency>
 
    <!-- For connection pooling -->
 
    <dependency>
 
      <groupId>org.hibernate</groupId>
 
      <artifactId>hibernate-c3p0</artifactId>
 
      <version>${hibernate.version}</version>
 
    </dependency>
 
	<!-- Database -->
 
    <dependency>
 
      <groupId>mysql</groupId>
 
      <artifactId>mysql-connector-java</artifactId>
 
      <version>5.1.31</version>
 
    </dependency>
 </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

 

Now we have Maven., JSF, Hibernate, and Primefaces combined in a project.

so far for the setup.

To test everything we create a small login page like this: and call it index.xhtml. I took the login-sample page from the primefaces website.

put it into the WEB-INF Folder.

index.xhtml can look like this:

 

<!DOCTYPE html>
<html xmlns="http://www.w3c.org/1999/xhtml"
	xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
	<h:form>
		<h:outputLink value="javascript:void(0)" onclick="PF('dlg').show();"
			title="login">
			<p:graphicImage name="/demo/images/login.png" />
		</h:outputLink>
		<h:outputLabel value="OutputLabel testing " />
		<p:growl id="growl" sticky="true" showDetail="true" life="3000" />
 
		<p:dialog header="Login" widgetVar="dlg" resizable="false">
			<h:panelGrid columns="2" cellpadding="5">
				<h:outputLabel for="username" value="Username:" />
				<p:inputText id="username" value="#{LoginBean.username}"
					required="true" label="username" />
 
				<h:outputLabel for="password" value="Password:" />
				<p:password id="password" value="#{LoginBean.password}"
					required="true" label="password" />
 
				<f:facet name="footer">
					<p:commandButton value="Login" update="growl"
						actionListener="#{LoginBean.login}"
						oncomplete="handleLoginRequest(xhr, status, args)" />
				</f:facet>
			</h:panelGrid>
		</p:dialog>
	</h:form>
 
	<script type="text/javascript">
    function handleLoginRequest(xhr, status, args) {
        if(args.validationFailed || !args.loggedIn) {
            PF('dlg').jq.effect("shake", {times:5}, 100);
        }
        else {
            PF('dlg').hide();
            $('#loginLink').fadeOut();
        }
    }
</script>
</h:body>
</html>

So in order to work for our little sample application we need a managed bean for the login page.

The bean is basically an simple Java Object which contains all methods and variables

used bei the jsf page.

You have to create a new java class LoginBean.java

With the following code:

package org.kingsteff.tutorial;
 
 
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
 
import org.primefaces.context.RequestContext;
 
@ManagedBean
 
public class LoginBean {
 
	private String word;
	private String username;
 
	private String password;
 
	public String getUsername() {
		return username;
	}
 
	public void setUsername(String username) {
		this.username = username;
	}
 
	public String getPassword() {
		return password;
	}
 
	public void setPassword(String password) {
		this.password = password;
	}
 
	public void login(ActionEvent event) {
		RequestContext context = RequestContext.getCurrentInstance();
		FacesMessage message = null;
		boolean loggedIn = false;
 
		if (username != null && username.equals("admin") && password != null && password.equals("admin")) {
			loggedIn = true;
			message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", username);
		} else {
			loggedIn = false;
			message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Loggin Error", "Invalid credentials");
		}
 
		FacesContext.getCurrentInstance().addMessage(null, message);
		context.addCallbackParam("loggedIn", loggedIn);
	}
 
}

After saving everything you will recognize, that there are missing dependencies, eclipse is warning you about.

For fixing this its necessary to compile the package and update the eclipse project.

Right Mouse Button on project –> “Run as” –> “Maven Build” –> type goal name: clean package. After that Rightclick on project –> “Update project”.

Possible Errors here is: Maven build fails because of a JRE Installed in Ecplise.

Solution: Window –> Preferences –> JAVA –> Installed JRE. Here you have to provide the current path to an oracle JDK.

Then MAVEN build should work fine.

So if all runs fine. You should have and deployed war file. Which can now be deployed to any applicationserver or JEE container for further testing.

 

thx for reading

have fun 🙂