JSF 2.0, Annotations, Spring Framework and Maven

January 19th, 2010 by Chris lutje Spelberg Leave a reply »

Hello world!

I wanted to use JSF and Spring in my application. For most of my Spring configuration I’m using annotations to keep things simple. In this blog entry I will show how link it all up.

Here are the changes I had to do to get it to work:

Configuration files

  • web.xml – configure the spring context and the JSF servlet.
    <web-app>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:application-context.xml</param-value>
        </context-param>
        <listener>
            <listener-class>
                org.springframework.web.context.ContextLoaderListener
            </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>
  • application-context.xml – configure annotation component-scan:
    <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"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
    
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    
    http://www.springframework.org/schema/context
    
                http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <context:annotation-config/>
        <context:component-scan base-package="nl.softwareart.package"/>
        <context:component-scan base-package="nl.softwareart.other.package"/>
        ...
    </beans>
  • faces-config.xml – configure the Spring-JSF application-resolver:
    <faces-config>
        <application>
            <variable-resolver>
                org.springframework.web.jsf.DelegatingVariableResolver
            </variable-resolver>
        </application>
        ...
    </faces-config>

Java annotations
To have Spring automatically detect your classes as beans add the @Component annotation to the class. Optionally you can add the @Scope annotation to define the scope to be for example ‘singleton’ (default), ‘session’, ‘request’ and more, please consult the spring reference documentation.
The @ManagedBean annotation is normally used by JSF but it looks like it is optional. I left it in because IntelliJ IDEA provides nice code completion and refactoring in html now ;-)

  • MyService.java
    package nl.softwareart.jsfspring;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyService {
    
        public String surroundWithBlocks(String value) {
            return "[" + value + "]";
        }
    
    }
  • MyTextBean.java
    package nl.softwareart.jsfspring;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    
    import javax.faces.bean.ManagedBean;
    
    @Component
    @Scope(value = "session")
    @ManagedBean
    public class MyTextBean {
    
        @Autowired
        MyService myService;
    
        private String text;
    
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
            this.text = text;
        }
    
        public String getSurroundedText() {
            return myService.surroundWithBlocks(text);
        }
    }
  • index.jsp
    In the jsp you just use the simple class name of the bean, first letter lowercase. In this example the bean name is myTextBean.

    <%@page contentType="text/html" %>
    <%@page pageEncoding="UTF-8" %>
    
    <%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
    <%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
    
    <f:view>
        <html>
        <head>
    		<title>Hello Jsf 2.0 and Spring!</title>
    	</head>
    	<body>
    		<h2>JSF and Spring demo</h2>
    
    		<h:form id="myForm">
    
    			<p>
    				<h:inputText id="text" label="Enter text here:" value="#{myTextBean.text}"> </h:inputText>
    			</p>
    
    			<p><h:commandButton value="Submit"> </h:commandButton></p>
    
    			<p><h:outputText value="#{myTextBean.surroundedText}"> </h:outputText></p>
    
    		</h:form>
    
    	</body>
    	</html>
    </f:view>

The demo application

You can download my demo application here: jsf-spring.zip

Unzip the archive, ‘cd’ to it, and execute ‘mvn jetty:run-war’ to start the demo application. Browse to http://localhost:8080/ to see the demo app.

Have fun!

Chris lutje Spelberg

Maven
If you use maven you can use my pom.xml to get started with the correct dependencies:

  • Snippet of the 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">
      ...
      <repositories>
        <repository>
          <id>org.codehaus.mojo</id>
          <url>http://repository.codehaus.org/</url>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </repository>
      </repositories>
    
      <properties>
        <spring.version>2.5.6</spring.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>com.sun.faces</groupId>
          <artifactId>jsf-api</artifactId>
          <version>2.0.2</version>
        </dependency>
        <dependency>
          <groupId>com.sun.faces</groupId>
          <artifactId>jsf-impl</artifactId>
          <version>2.0.2</version>
        </dependency>
        <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.1.2</version>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.6</source>
              <target>1.6</target>
            </configuration>
          </plugin>
          ...
          <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>7.0.0.RC6</version>
            <configuration>
              <scanIntervalSeconds>5</scanIntervalSeconds>
              <webAppConfig>
                <contextPath>/</contextPath>
              </webAppConfig>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
Advertisement

Leave a Reply