📜 ⬆️ ⬇️

Configuring Tomcat to work with JSF2 and other Java EE 6 components

This article describes step by step how to configure Tomcat (currently version 6.0.26) to work with different components of Java EE 6.You can skip reading this article and immediately get customized Tomcat and sample applications .

Tomcat

Why Tomcat and not Glassfish V3 ? Tomcat use is important for me. It is compact, fast-running, and easily merged into a cluster running Apache Httpd. V3 does not know how to work in a cluster yet .

Installation

  1. Install JDK 6 .
  2. Set the JAVA_HOME environment variable
  3. Download the apache-tomcat-6.0.26.zip archive and unpack it into the current folder. This will create the apache-tomcat-6.0.26 folder.
For start:
> bin/startup.(bat|sh) - <br/>> bin/catalina.(bat|sh) run - .
To increase memory and debugging capabilities, you can specify the JAVA_OPTS environment variable before running catalina.bat:
set JAVA_OPTS=-Xmx512m -XX:MaxPermSize=256m -Xdebug -Xrunjdwp:transport=dt_socket,address=1025,server=y,suspend=n<br/>export JAVA_OPTS=-Xmx512m -XX:MaxPermSize=256m -Xdebug -Xrunjdwp:transport=dt_socket,address=1025,server=y,suspend=n

Unified EL 2.2

The expression language is needed both in JSP when using tag libraries, and in facelets. The main improvements in the new version (see the list of innovations ) are the ability to call methods, incl. with parameters.
${application.getRealPath( '/data' )}

Installation

  1. Remove lib / el-api.jar from Tomcat.
  2. Copy the implementation of el-api-2.2.jar and el-impl-2.2.jar to the lib folder in Tomcat. These are jars from Glassfish V3.
  3. Add the following lines to your application in WEB-INF / web.xml:
    < context-param > <br/> < param-name > com.sun.faces.expressionFactory </ param-name > <br/> < param-value > com.sun.el.ExpressionFactoryImpl </ param-value > <br/> </ context-param >
Unfortunately, after updating the Jasper expression language (JSP compiler implementation in Tomcat) does not understand the new expressions and they cannot be used in JSP. If you want Tomcat to use the new expression language everywhere (and not your org.apache.el implementation), you need to recompile Tomcat. Details here (in English. Language). Also point 3 will not be needed.

Mojarra

The first implementation of JSF 2. Stable and fast running. In addition to the JSF implementation, we also need to install JSTL 1.2.

Installation

  1. Download mojarra-2.0.2-FCS-binary.zip
  2. Copy jsf-api.jar and jsf-impl.jar from the archive from the mojarra-2.0.2-FCS / lib folder to the WEB-INF / lib folder of your application.
  3. Copy jstl 1.2 to your application's WEB-INF / lib folder.
  4. Add the following lines to your application in WEB-INF / web.xml:
    < servlet > <br/> < servlet-name > FacesServlet </ servlet-name > <br/> < servlet-class > javax.faces.webapp.FacesServlet </ servlet-class > <br/> < load-on-startup > 1 </ load-on-startup > <br/> </ servlet > <br/> < servlet-mapping > <br/> < servlet-name > FacesServlet </ servlet-name > <br/> < url-pattern > *.xhtml </ url-pattern > <br/> </ servlet-mapping >
  5. Create a WEB-INF / faces-config.xml file in your application:
    < faces-config xmlns ="http://java.sun.com/xml/ns/javaee" <br/> xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" <br/> xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" <br/> version ="2.0" > <br/> </ faces-config >

Weld

Weld implements the JSR 299 Contexts and Dependency Injection specification. In short, Weld allows you to set field values ​​automatically using annotations.

Installation

  1. Download weld-1.0.1-Final.zip
  2. Create a WEB-INF / beans.xml file with the following contents:
    <? xml version ="1.0" encoding ="UTF-8" ? > <br/> < beans />
    This is necessary for Weld to scan the web application for Java-Beans that will participate in CDI.
  3. Copy weld-1.0.1-Final / artifacts / weld / weld-tomcat-support.jar from the archive to the lib folder in Tomcat.
  4. Create the file META-INF / context.xml in your application with the following contents:
    <? xml version ="1.0" encoding ="UTF-8" ? > <br/> < Context > <br/> <!-- Declare Weld Bean Manager --> <br/> < Resource name ="BeanManager" <br/> auth ="Container" <br/> type ="javax.enterprise.inject.spi.BeanManager" <br/> factory ="org.jboss.weld.resources.ManagerObjectFactory" /> <br/> <!-- For Weld to support Servlet Injection --> <br/> < Listener className ="org.jboss.weld.environment.tomcat.WeldLifecycleListener" /> <br/> </ Context >
  5. Copy weld-1.0.1-Final / artifacts / weld / weld-servlet.jar from the archive into the WEB-INF / lib folder of your application.
  6. Add the following lines to WEB-INF / web.xml:
    <!-- Weld listener --> <br/> < listener > <br/> < listener-class > org.jboss.weld.environment.servlet.Listener </ listener-class > <br/> </ listener > <br/> <!-- Reference to Weld Bean Manager --> <br/> < resource-env-ref > <br/> < description > Object factory for the CDI Bean Manager </ description > <br/> < resource-env-ref-name > BeanManager </ resource-env-ref-name > <br/> < resource-env-ref-type > javax.enterprise.inject.spi.BeanManager </ resource-env-ref-type > <br/> </ resource-env-ref >

Hibernate validator

JSR 303 Bean Validation provides a simple way to apply validation rules for both JSF and JPA. These rules are defined via field annotations in Java Beans:
@NotNull@Size(min = 1)<br/> private String username;

Installation

  1. Download hibernate-validator-4.0.2.GA-dist.zip
  2. Copy hibernate-validator-4.0.2.GA/hibernate-validator-4.0.2.GA.jar from the archive into the WEB-INF / lib folder of your application.
  3. Copy the additional libraries slf4j-api-1.5.6.jar, validation-api-1.0.0.GA.jar into the WEB-INF / lib folder of your application from the folder in the hibernate-validator-4.0.2.GA/lib archive.

Eclipselink

JPA allows you to save to the Java Beans database without using SQL. Required calls and if necessary a database schema is created automatically. It uses a simple interface EntityManager . Unfortunately, because Tomcat is not a full-fledged Java EE server; some JPA features (transaction management, automatic EntityManager) are not available. Also, all objects that will be saved in the database should be described in the configuration file.

Installation

  1. This installation assumes you have a MySQL database on your local computer. The password for the database user root is "" (empty password).
  2. Download the driver for the database and put the JAR in the lib folder in Tomcat.
  3. Download eclipselink-2.0.1.v20100213-r6600.zip
  4. Copy eclipselink / jlib / jpa / javax.persistence_2.0.0.v201002051058.jar from the archive into the folder WEB-INF / lib of your application.
  5. Copy eclipselink / jlib / eclipselink.jar from the archive into the WEB-INF / lib folder of your application.
  6. Create a WEB-INF / classes / META-INF / persistence.xml file in your application with the following contents:
    <? xml version ="1.0" encoding ="UTF-8" ? > <br> < persistence version ="1.0" <br> xmlns ="http://java.sun.com/xml/ns/persistence" <br> xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" <br> xsi:schemaLocation ="http://java.sun.com/xml/ns/persistence java.sun.com/xml/ns/persistence/persistence_1_0.xsd" > <br> < persistence-unit name ="sample" transaction-type ="RESOURCE_LOCAL" > <br> < provider > org.eclipse.persistence.jpa.PersistenceProvider </ provider > <br> <!-- < class > your.class.Here </ class > --><br> < exclude-unlisted-classes > false </ exclude-unlisted-classes > <br> < properties > <br> < property name ="javax.persistence.jdbc.driver" value ="com.mysql.jdbc.Driver" /> <br> < property name ="javax.persistence.jdbc.url" value ="jdbc:mysql://localhost:3306/test" /> <br> < property name ="javax.persistence.jdbc.user" value ="root" /> <br> < property name ="javax.persistence.jdbc.password" value ="" /> <br> < property name ="eclipselink.logging.logger" value ="JavaLogger" /> <br> < property name ="eclipselink.logging.level" value ="FINEST" /> <br> <!-- Can specify: create-tables, drop-and-create-tables --> <br> < property name ="eclipselink.ddl-generation" value ="create-tables" /> <br> < property name ="eclipselink.ddl-generation.output-mode" value ="database" /> <br> < property name ="eclipselink.target-database" value ="MYSQL" /> <br> < property name ="eclipselink.jdbc.native-sql" value ="true" /> <br> </ properties > <br> </ persistence-unit > <br> </ persistence >

Download


Links

* Source code was highlighted with Source Code Highlighter .

')

Source: https://habr.com/ru/post/87973/


All Articles