sábado, 1 de abril de 2017

JSF 2.1 within embedded Tomcat 8 (2017)

This post relates about configuring the following aspects to get a working JSF 2.1 application:
  • Maven 3
  • JSF 2.1.7
  • Embedded Tomcat 8

pom.xml

<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>eu.altotek</groupId>
 <artifactId>spa-jsf-generator</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>spa-jsf-generator Maven Webapp</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
  <java.version>1.8</java.version>

  <jsf.version>2.1.7</jsf.version>
  <jsp-api.version>2.2.1-b03</jsp-api.version>

  <tomcat.version>8.0.28</tomcat.version>
 </properties>

 <dependencies>
  <!-- JSF -->
  <dependency>
   <groupId>com.sun.faces</groupId>
   <artifactId>jsf-api</artifactId>
   <version>${jsf.version}</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>com.sun.faces</groupId>
   <artifactId>jsf-impl</artifactId>
   <version>${jsf.version}</version>
   <scope>runtime</scope>
  </dependency>

  <!-- Java standards -->
  <dependency>
   <groupId>org.glassfish.web</groupId>
   <artifactId>el-impl</artifactId>
   <version>2.2</version>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>
  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <version>${jsp-api.version}</version>
  </dependency>

  <!-- Tomcat -->
  <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-core</artifactId>
   <version>${tomcat.version}</version>
  </dependency>
  <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-logging-juli</artifactId>
   <version>${tomcat.version}</version>
  </dependency>
  <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <version>${tomcat.version}</version>
  </dependency>
  <dependency>
   <groupId>org.apache.tomcat</groupId>
   <artifactId>tomcat-jasper</artifactId>
   <version>${tomcat.version}</version>
  </dependency>
  <dependency>
   <groupId>org.apache.tomcat</groupId>
   <artifactId>tomcat-jasper-el</artifactId>
   <version>${tomcat.version}</version>
  </dependency>
  <dependency>
   <groupId>org.apache.tomcat</groupId>
   <artifactId>tomcat-jsp-api</artifactId>
   <version>${tomcat.version}</version>
  </dependency>

  <!-- Testing -->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
 <build>
  <finalName>spa-jsf-generator</finalName>
  <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>
 </build>
</project>

web.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">

 <context-param>
  <param-name>javax.faces.PROJECT_STAGE</param-name>
  <param-value>Development</param-value>
 </context-param>

 <welcome-file-list>
  <welcome-file>/jsf/home.jsf</welcome-file>
 </welcome-file-list>

 <listener>
  <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
 </listener>

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

</web-app>

Main.java

package eu.altotek.spa_jsf_generator;

import java.io.File;

import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

public class Main {

 public static void main(final String[] args) throws Exception {

  final String webappPath = new File("src/main/webapp").getAbsolutePath();
  final Tomcat tomcat = new Tomcat();

  final StandardContext ctx = (StandardContext) tomcat.addWebapp("/", webappPath);

  // Declare an alternative location for your "WEB-INF/classes" dir
  // Servlet 3.0 annotation will work
  final String targetClassesPath = new File("target/classes").getAbsolutePath();
  final WebResourceRoot resources = new StandardRoot(ctx);
  resources.addPreResources(new DirResourceSet(//
    resources, "/WEB-INF/classes", //
    targetClassesPath, "/"));
  ctx.setResources(resources);

  tomcat.start();
  tomcat.getServer().await();
 }
}

webapp/jsf/home.xhtml

<!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">
<head>
<title>JSF Tutorial!</title>
</head>

<body>
#{helloWorld.message}
</body>
</html>

HelloWorld.java

package eu.altotek.spa_jsf_generator.jsf;

import javax.faces.bean.ManagedBean;

@ManagedBean
public class HelloWorld {

 public HelloWorld() {
  System.out.println(this + "started!");
 }

 public String getMessage() {
  return "Hello " + this;
 }
}

References & kudos

  • https://www.mkyong.com/jsf2/jsf-2-0-hello-world-example/
  • http://musingsofaprogrammingaddict.blogspot.com.es/2009/12/running-jsf-2-on-embedded-jetty.html
  • https://www.mkyong.com/jsf2/jsf-2-0-tomcat-it-appears-the-jsp-version-of-the-container-is-older-than-2-1/
  • https://www.tutorialspoint.com/jsf/jsf_first_application.htm
  • http://alexgorbatchev.com/SyntaxHighlighter/