使用Spring时JSF2注释无法正常工作

时间:2012-06-22 15:08:56

标签: java spring java-ee jsf-2

我无法使JSF注释工作。我花了一些时间试图解决这个问题。我正在使用Maven + Tomcat7 + JSF2.16 + JDK1.7 + Spring 3 + Hibernate 4。 如果我是新手,请提前原谅我。

这是我的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配置文件声明为JSF 2,如下所示

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

我的托管bean注释如下

    @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;

如果我在faces-config中声明托管bean,那么

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

这有效,但我不明白为什么注释没有。

任何帮助或指示,我将非常感激! 谢谢!

2 个答案:

答案 0 :(得分:0)

尝试将此添加到spring上下文XML配置文件中。

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

它应该扫描您的类以查找bean。

答案 1 :(得分:0)

我认为你不能使用bean的JSF注释。但可能是我错了

我使用与您相同的技术,但我不使用JSF注释。我喜欢这样:

@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;

不要忘记像佩特说的那样插入。

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

你应该知道Spring中没有view范围,你应该自己实现。但不要害怕有很多例子:https://www.google.fr/search?sugexp=chrome,mod=7&sourceid=chrome&ie=UTF-8&q=spring+custom+viewscope