Change web context using Maven

<packaging>war</packaging>
<build>
<plugins>
  <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
    <configuration>
        <packagingExcludes>WEB-INF/web.xml</packagingExcludes>
        <warName>exp-elec-web</warName>
    </configuration>
</plugin>
</plugins>
</build>

Maven config for JSF2/RichFaces

Here is my Maven configuration for a JSF2/Richfaces project:

<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>net.classnotfound</groupId>
    <artifactId>jsf</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>jsf</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.apache.myfaces.version>2.2.0</org.apache.myfaces.version>
        <org.richfaces.version>4.3.5.Final</org.richfaces.version>
    </properties>

    <dependencies>

        <!-- web container dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>7.0.50</version>
            <scope>provided</scope>
        </dependency>
        <!-- JSF dependencies -->
        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-bundle</artifactId>
            <version>${org.apache.myfaces.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-digester</groupId>
            <artifactId>commons-digester</artifactId>
            <version>2.1</version>
        </dependency>
        <!-- end JSF dependencies -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- RichFaces dependencies -->
        <dependency>
            <groupId>org.richfaces.core</groupId>
            <artifactId>richfaces-core-api</artifactId>
            <version>${org.richfaces.version}</version>
        </dependency>
        <dependency>
            <groupId>org.richfaces.core</groupId>
            <artifactId>richfaces-core-impl</artifactId>
            <version>${org.richfaces.version}</version>
        </dependency>
        <dependency>
            <groupId>org.richfaces.ui.common</groupId>
            <artifactId>richfaces-ui-common-api</artifactId>
            <version>${org.richfaces.version}</version>
        </dependency>
        <dependency>
            <groupId>org.richfaces.ui.core</groupId>
            <artifactId>richfaces-ui-core-api</artifactId>
            <version>${org.richfaces.version}</version>
        </dependency>
        <dependency>
            <groupId>org.richfaces.ui</groupId>
            <artifactId>richfaces-components-api</artifactId>
            <version>${org.richfaces.version}</version>
        </dependency>
        <dependency>
            <groupId>org.richfaces.ui</groupId>
            <artifactId>richfaces-components-ui</artifactId>
            <version>${org.richfaces.version}</version>
        </dependency>
        <dependency>
            <groupId>org.w3c.css</groupId>
            <artifactId>sac</artifactId>
            <version>1.3</version>
        </dependency>
        <!-- End RichFaces dependencies -->
    </dependencies>
</project>

Starting of Spring context in web application

Using Spring in a web application, we need to load the context when the application starts. Fortunately, it can be done in a simple way, by adding the right listener in the application web.xml.

Using Maven, you need to add the Spring-web dependency in your pom.xml:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>4.0.0.RELEASE</version>
  <scope>compile</scope>
</dependency>

And in the web.xml:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Check Generic type

Using JMS which is not implemented with Generics, I decided to add some “genericity”.
The problem is that, because of type erasure, I can’t use the simple instanceof operator, the solution I used is to pass the class of the Generic when I use it:

import java.io.Serializable;

public class Handler {

    public Handler() {
        super();
    }
    
    private Class clazz;

    public Handler(Class clazz) {
        super();
        this.clazz = clazz;
    }

    public T extract(Object o) {
        if (!(o.getClass().isAssignableFrom(clazz)))
            throw new RuntimeException("explicite message");
        return (T) o;
    }
    
    public static void main(String[] args) {
        Handler handler = new Handler(String.class);
        
        System.out.println(handler.extract(new Integer("11")));//This throw an Exception
        
        System.out.println(handler.extract(new Integer("11")));//This is ok
    }
}

Now it works, but I will try to find a more elegant solution to avoid the clazz parameter.