Introduction
To show formatted text Hilite is used
Our goal is to configure an Eclipse Maven JSF2 2.2 and CDI with Tomcat.
Weld reports a great problem for servlet containers :
- NO SESSION SCOPED BEANS can be used,
- NO @EJB INJECTION can be used.
- NO @PersistentContext INJECTION can be used.
- NO TRANSACTIONAL EVENTS can be used.
So the previous example where a @SessionScoped bean was defined SHOULD NOT WORK (OR MAYBE IT CAN WORK)
As mentioned in BalusC Blog these dependencies will be referenced :
Our pom.xml file may be this one
Defining Maven dependencies
As mentioned in BalusC Blog these dependencies will be referenced :
- weld-servlet-shaded.jar (requiered)
-
validation-api.jar (optional for bean validation)
hibernate-validator.jar (optional for bean validation)
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | <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.ximodante.jsf</groupId> <artifactId>JSFv02</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>JSFv02</name> <description>JSF 2.2 & CDI</description> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <failOnMissingWebXml>false</failOnMissingWebXml> </properties> <dependencies> <!-- Servlet 3.1 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- JSF 2.2 API --> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.2.14</version> </dependency> <!-- JSF 2.2 Implementation --> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.2.14</version> </dependency> <!-- Primefaces --> <dependency> <groupId>org.primefaces</groupId> <artifactId>primefaces</artifactId> <version>6.1</version> </dependency> <!-- Primefaces Themes --> <dependency> <groupId>org.primefaces.extensions</groupId> <artifactId>all-themes</artifactId> <version>1.0.8</version> <type>pom</type> </dependency> <!-- Weld CDI for Tomcat (does not fulfill all capabilities !!!) --> <dependency> <groupId>org.jboss.weld.servlet</groupId> <artifactId>weld-servlet-shaded</artifactId> <version>3.0.0.Final</version> </dependency> <!-- Validation API Optional --> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.0.CR3</version> </dependency> <!-- Hibernate Bean Validator Optional --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.4.1.Final</version> </dependency> </dependencies> </project> |
Creating additional files
These files are our candidates for being created in our webapp folder:
- META-INF/context.xml (not necessary for Mojarra version > 2.2.11 ( using now 2.2.14)
- WEB-INF/beans.xml (empty file)
NOTE: I have encountered an error on Maven reporting that failed to read artifact descriptor for hibernate-validator dependency. There are different solutions to this issue is in stackoverflow and this other one stackoverflow.
As mentioned above we SHOULD NOT use CDI @SessionScoped Beans with Tomcat, (we could use @ViewScoped instead .... but for now I will only change the @SessionScoped reference of this dependence to Weld javax.enterprise.context.SessionScoped)
The new annotation replacement for @ManagedBean is @Named according to Weld
But our class must implement Serializable interface in order to avoid this error
Managed bean KeyboardBean which declares a passivating scope SessionScoped must be passivation
capable [JSR-346 §6.6.5]
Our class KeyboardBean will be now:
Let's execute as last post Run As - Run on Server, select tomcat and our URL is:
http://localhost:8080/JSFv01/index.jsf
And everything should go as last post
Updating existing files
As mentioned above we SHOULD NOT use CDI @SessionScoped Beans with Tomcat, (we could use @ViewScoped instead .... but for now I will only change the @SessionScoped reference of this dependence to Weld javax.enterprise.context.SessionScoped)
The new annotation replacement for @ManagedBean is @Named according to Weld
But our class must implement Serializable interface in order to avoid this error
Managed bean KeyboardBean which declares a passivating scope SessionScoped must be passivation
capable [JSR-346 §6.6.5]
Our class KeyboardBean will be now:
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 | package org.ximodante.jsf; //import javax.faces.bean.ManagedBean; //import javax.faces.bean.SessionScoped; import javax.inject.Named; import javax.enterprise.context.SessionScoped; import java.io.Serializable; //@ManagedBean @Named @SessionScoped public class KeyboardBean implements Serializable { private static final long serialVersionUID = 1L; private String value="12345"; public String getValue() { System.out.println("KeyboardBean::reading value: " + value); return value; } public void setValue(String value) { System.out.println("KeyboardBean::setting value: " + value); this.value = value; } } |
Let's execute as last post Run As - Run on Server, select tomcat and our URL is:
And everything should go as last post