0

I am having trouble getting the JSF annotations to work. I have spent some time trying to figure this out. I am useing Maven + Tomcat7 + JSF2.16 + JDK1.7 + Spring 3 + Hibernate 4. Please forgive me in advance if it is something simple as I am a newbie.

Here is my 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">

<modelVersion>4.0.0</modelVersion>
<groupId>com.uk.todolist</groupId>
<artifactId>UkToDoList</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>UkToDoList</name>
<url>http://maven.apache.org</url>

<repositories>
    <repository>
        <id>prime-repo</id>
        <name>PrimeFaces Maven Repository</name>
        <url>http://repository.primefaces.org</url>
        <layout>default</layout>
    </repository>
</repositories>

<properties>
    <spring.version>3.1.1.RELEASE</spring.version>
    <spring.security.version>3.0.5.RELEASE</spring.security.version>
</properties>


<dependencies>

    <!-- Spring 3 dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>   

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>            
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- Spring Security -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>${spring.security.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring.security.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring.security.version}</version>
    </dependency>

    <!-- JSF library -->
    <dependency>
      <groupId>com.sun.faces</groupId>
      <artifactId>jsf-api</artifactId>
      <version>2.1.6</version>
    </dependency>

    <dependency>
      <groupId>com.sun.faces</groupId>
      <artifactId>jsf-impl</artifactId>
      <version>2.1.6</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Primefaces library -->
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>3.1.1</version>
    </dependency>

    <!-- Hibernate library -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.0.Final</version>
    </dependency>

    <!-- MySQL Java Connector library -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.20</version>
    </dependency>

    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>            

    <!-- Log4j library -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

</dependencies>

<build>     
    <finalName>UkToDoList</finalName>
</build> 

My Faces config file is declared as JSF 2 as follows

    <?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">

My Managed bean is annotated as follows

    @ManagedBean
    @ViewScoped
    public class TaskManagedBean implements Serializable {

private static final long serialVersionUID = 1L;
private static final String SUCCESS = "success";
private static final String ERROR   = "error";

//NOTE: Task Service is injected
@ManagedProperty(value="#{TaskService}")
TaskService taskService;

If I declare the managed beans in faces-config like so

     <managed-bean>
       <managed-bean-name>taskManagedBean</managed-bean-name>
       <managed-bean-          class>com.uk.todolist.managed.bean.TaskManagedBean</managed-bean-class>
       <managed-bean-scope>session</managed-bean-scope>     
    </managed-bean>

This works but I do not understand why the annotations do not.

Any Help or direction and I would be very grateful! Thanks!

Curt
  • 2,944
  • 6
  • 29
  • 34
  • I added to faces-config " metadata-complete="false"> " and placed an empty faces-config in the META-INF folder as suggested on this post http://stackoverflow.com/questions/2987266/why-doesnt-jsf-2-0-ri-mojarra-scan-my-class-annotations just incase, but this did not seem to change anything. – Curt Jun 22 '12 at 15:28

2 Answers2

0

Try adding this to your spring context XML config file.

<context:component-scan base-package="your.base.package" />

It should scan your classes for beans.

Petter Nordlander
  • 22,053
  • 5
  • 50
  • 84
0

I don't think you can use the JSF annotation for beans. But may be I'm wrong

I use the same technologies as you and I don't use JSF annotation. I do like this instead :

@Component
@Scope(value = "session")
public class TaskManagedBean implements Serializable {

private static final long serialVersionUID = 1L;
private static final String SUCCESS = "success";
private static final String ERROR   = "error";

//NOTE: Task Service is injected
@Autowired
TaskService taskService;

And don't forget to insert like Petter said.

<context:component-scan base-package="your.base.package" />

You should know that there is no view scope in Spring and you should implement yourself. But don't be afraid there is a lot of examples : https://www.google.fr/search?sugexp=chrome,mod=7&sourceid=chrome&ie=UTF-8&q=spring+custom+viewscope

La Chamelle
  • 2,927
  • 4
  • 36
  • 54