-1

I'm gonna start an application with these frameworks, and I don't know which versions to choose (with or without maven).

fvrghl
  • 3,642
  • 5
  • 28
  • 36
krachleur
  • 356
  • 3
  • 14
  • Honestly, if you have no specific restrictions, try using the latest version of each one. And I do recommend using Maven, you'll have a lot less problems pulling in all the dependencies that way. – Shivan Dragon Jan 21 '13 at 15:21
  • This isn't a good question for this site. IMHO I would use JSF 2.1, RichFaces 4.2, Spring 3.2 and Hibernate 4.1.9 (latest releases until now), but since I'm a fan of Java EE 6, I would recommend using EB 3.1 and JPA 2 with Hibernate implementation instead of Spring 3.2 and Hibernate, and use JBoss AS 7 instead of tomcat because JBoss is builded on top of Tomcat and is very lightweight. – Luiggi Mendoza Jan 21 '13 at 16:10
  • Related: [Java EE 6 vs. Spring 3 stack](http://stackoverflow.com/q/2499323/1065197) – Luiggi Mendoza Jan 21 '13 at 16:16
  • I've read this question as "how to start an application using Spring, JSF and Hibernate", as I know myself it can be very overwhelming to bootstrap an application with those. Though "choosing versions" may not be very answerable, bootstraping an app certainly is, and I personally believe it's a very important one. I'm not a fan of Spring, nor really of any of the technologies I cite in my answer, but I believe they make a good start anyway. – Elias Dorneles Jan 21 '13 at 17:26

2 Answers2

1

Which versions to choose may not be a relevant question, since there are many, and choosing what is appropriate is really unique in each case, since you have to take into consideration which technologies you already know how to use, which you can learn along the way and, of course, what are the requirements of the current project.

But how to bootstrap a project is a good question indeed, in the sense that there are too many options that is easily to become overwhelmed, and there aren't much good tutorials that put you on a good-enough starting path, and you have to start somewhere so, that's what I'm going to try to answer, telling you the steps to create a Maven based project integrating Spring, JSF and JPA (using Hibernate implementation), targetting Tomcat 7.

Later, you can tweak the version numbers in the pom.xml file, to try out other versions of Spring, Mojarra, Hibernate, etc.

  • Install Maven, if you haven't already
  • Create a simple Maven webapp project, using the archetype:generate Maven target (this may take a while), running this in the command line:

mvn archetype:generate -B -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=PUT_YOUR_BASE_PACKAGE_NAME_HERE -DartifactId=APP_NAME -Dpackage=war

  • Edit the pom.xml, adding the following dependencies:

    <!-- To configure a datasource pool -->
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
        <scope>runtime</scope>
    </dependency>
    
    <!-- Mojarra -->
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.faces</artifactId>
        <version>2.0.10</version>
        <scope>compile</scope>
    </dependency>
    
    <!-- Richfaces -->
    <dependency>
        <groupId>org.richfaces.core</groupId>
        <artifactId>richfaces-core-api</artifactId>
        <version>4.2.2.Final</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.richfaces.core</groupId>
        <artifactId>richfaces-core-impl</artifactId>
        <version>4.2.2.Final</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.richfaces.ui</groupId>
        <artifactId>richfaces-components-api</artifactId>
        <version>4.2.2.Final</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.richfaces.ui</groupId>
        <artifactId>richfaces-components-ui</artifactId>
        <version>4.2.2.Final</version>
        <scope>runtime</scope>
    </dependency>
    
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-asm</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.1.1.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    
    <!-- Hibernate & JPA -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.10.Final</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-envers</artifactId>
        <version>3.6.10.Final</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.Final</version>
        <scope>compile</scope>
    </dependency>
    
    <!-- Logging config, app uses java.util.logging and wrappers for log4j and commons-logging (jcl) -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jdk14</artifactId>
        <version>1.6.6</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>1.6.6</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.6.6</version>
    </dependency>
    <!-- In-memory DB, for beginning development -->
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.2.8</version>
        <scope>runtime</scope>
    </dependency>
    
  • Create a src/main/resources/META-INF/persistence.xml for JPA configuration:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
        <persistence-unit name="APP_NAME">
                <!-- Add here your Entity classes, like: -->
                <!-- <class>PUT_YOUR_BASE_PACKAGE_NAME_HERE.BookEntity</class> -->
    </persistence-unit>
    </persistence>
    
  • Create a file src/main/resources/appContext.xml for the Spring beans configuration, with the following content (sets up a JPA EMF with transaction management, and an in-memory db):

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    <context:component-scan base-package="PUT_YOUR_BASE_PACKAGE_NAME_HERE" />
    
    <!-- sets up transaction management for service beans -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="APP_NAME" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
        <property name="url" value="jdbc:hsqldb:mem:tempdb" />
        <property name="username" value="sa" />
    </bean>
    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true" />
        <property name="generateDdl" value="true" />
        <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
    </bean>
    </beans>
    
  • Edit the src/main/webapp/WEB-INF/web.xml setting up Spring and JSF initialization:

    <?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_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>APP_NAME</display-name>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
    
    <!-- JSF Mapping -->
    <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>*.xhtml</url-pattern>
    </servlet-mapping>
    
    <!-- Faces config for development -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>com.sun.faces.enableMissingResourceLibraryDetection</param-name>
        <param-value>true</param-value>
    </context-param>
    
    <!-- Spring beans configuration -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath://appContext.xml</param-value>
    </context-param>
    
    <!-- filter enforcing charset UTF-8 - must be the first in the chain! -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- listeners do Spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    </web-app>
    
  • Configure JSF to find Spring beans, creating the src/main/webapp/WEB-INF/faces-config.xml file with the following content:

    <?xml version="1.0" encoding="UTF-8"?>
    <faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0">
            <application>
                <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
            </application>
    </faces-config>
    
  • Now, create a JSF page in src/main/webapp/index.xhtml to test it out:

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
        xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core"
        xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich">
        <h:head>
            <title>Hello, world</title>
        </h:head>
        <h:body>
            <rich:panel header="Hello, World">
            <p>This is a JSF page, testing access to Spring beans.</p>
            <ui:fragment rendered="#{entityManagerFactory != null}">
                EntityManagerFactory bean loaded successfully!
            </ui:fragment>
            <ui:fragment rendered="#{entityManagerFactory == null}">
                EntityManagerFactory didn't loaded successfully! :(
                Check out the logs!
        </ui:fragment>
    </rich:panel>
    </html>
    
  • That's it, now generate a WAR file with mvn package and test it out deploying it in a Tomcat 7 installation.

After that, you can try importing the project into Eclipse, adding some JPA entities, creating a Spring beans annotated with @Repository and @Transactional to make use of the transaction facilities, JSF pages and backing beans that make uses of those, and so on.

Notes: Tested with Maven 3.0.4 and Tomcat 7.0.27. You may have to tweak your Maven configuration, to set up the default compiler settings, see this question.

Community
  • 1
  • 1
Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107
0

The answer that was provided was just what I was after, however, I couldn't get the JSF page to be processed. I added the following to web.xml and requested the index.faces page and it worked perfectly.

<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>*.faces</url-pattern>
</servlet-mapping>
Marc Thomas
  • 393
  • 1
  • 3
  • 16