0

我无法让 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> 

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

这可行,但我不明白为什么注释不起作用。

任何帮助或方向,我将不胜感激!谢谢!

4

2 回答 2

0

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

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

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

于 2012-06-22T20:23:30.237 回答
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;

并且不要忘记像 Petter 所说的那样插入。

<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

于 2012-06-23T09:07:59.263 回答