无法从JSF中的faces-config.xml创建bean

时间:2015-10-02 14:51:32

标签: maven jsf managed-bean faces-config

我是JSF的新手。我试图创建这个类的会话范围的bean:

package com.mybusiness.clients.myproject;

public class OAuth2ServiceClient {
    ...
    public OAuth2ServiceClient(String serviceURI, String clientName, String clientSecret) {
        ...
    }
    ...
}

我不能修改这个类(因为它是由库提供的),你必须通过faces-config.xml创建:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">

    <managed-bean>
        <managed-bean-name>oauth2Client</managed-bean-name>
        <managed-bean-class>com.mybusiness.clients.myproject.OAuth2ServiceClient</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
        <managed-property>
            <property-name>serviceURI</property-name>
            <value>http://localhost:8080/</value>
        </managed-property>
        <managed-property>
            <property-name>clientName</property-name>
            <value>myClient</value>
        </managed-property>
        <managed-property>
            <property-name>clientSecret</property-name>
            <value>secret</value>
        </managed-property>
    </managed-bean>
</faces-config>

我在我的LoginController类中使用这个oauth2Client,但是我试图通过这两种方式实现它而没有成功

@ManagedBean
@SessionScoped
public class LoginController {

     @ManagedProperty(value = "#{oauth2Client}")
     private OAuth2ServiceClient oauth2Client // Coming null

     public String login(){
         HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
session.getAttributeNames(); // Doesn't contains oauth2Client 
         ...
     }

}

这是我的pom.xml

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <primefaces.version>5.1</primefaces.version>
        <jsf-impl.version>2.2.4</jsf-impl.version>
        <jsf-api.version>2.2.4</jsf-api.version>
        <servlet.version>3.0.1</servlet.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>${primefaces.version}</version>
            <scope>compile</scope>
        </dependency>
        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>${jsf-impl.version}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>${jsf-api.version}</version>
        </dependency>
        ...
    </dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>8.1.9.v20130131</version>
            <configuration>
                <connectors>
                    <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                        <port>9290</port>
                    </connector>
                </connectors>
                <stopKey>alpha</stopKey>
                <stopPort>9092</stopPort>
                <scanIntervalSeconds>5</scanIntervalSeconds>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <port>${jetty.port}</port>
                <path>/</path>
            </configuration>
        </plugin>
    </plugins>

    <extensions>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ssh</artifactId>
            <version>1.0</version>
        </extension>
    </extensions>
</build>

我的web.xml:

<?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"
    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>myproject-frontend</display-name>

    <listener>
    <listener-class>
        com.sun.faces.config.ConfigureListener
    </listener-class>
    </listener>

    <servlet>
        <servlet-name>JSF Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>JSF Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>JSF Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>redirect.xhtml</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>primefaces.FONT_AWESOME</param-name>
        <param-value>true</param-value>
    </context-param>

</web-app>

我做错了什么?我错过了什么吗?

修改

mvn jetty:运行

Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building example-myproject Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> jetty-maven-plugin:8.1.9.v20130131:run (default-cli) @ myproject-frontend >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ myproject-frontend ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] Copying 12 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ myproject-frontend ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.3:testResources (default-testResources) @ myproject-frontend ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/jscherman/Documents/Workspace/myproject/myproject-frontend/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ myproject-frontend ---
[INFO] No sources to compile
[INFO] 
[INFO] <<< jetty-maven-plugin:8.1.9.v20130131:run (default-cli) @ myproject-frontend <<<
[INFO] 
[INFO] --- jetty-maven-plugin:8.1.9.v20130131:run (default-cli) @ myproject-frontend ---
[INFO] Configuring Jetty for project: example-myproject Maven Webapp
[INFO] webAppSourceDirectory not set. Defaulting to /home/jscherman/Documents/Workspace/myproject/myproject-frontend/src/main/webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = /home/jscherman/Documents/Workspace/myproject/myproject-frontend/target/classes
[INFO] Context path = /
[INFO] Tmp directory = /home/jscherman/Documents/Workspace/myproject/myproject-frontend/target/tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = file:/home/jscherman/Documents/Workspace/myproject/myproject-frontend/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = /home/jscherman/Documents/Workspace/myproject/myproject-frontend/src/main/webapp
2015-10-02 15:39:17.860:INFO:oejs.Server:jetty-8.1.9.v20130131
2015-10-02 15:39:19.328:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one.
2015-10-02 15:39:27.609:INFO:/:Spring WebApplicationInitializers detected on classpath: [org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration@76494bed]
2015-10-02 15:39:27.628:INFO:/:Initializing AtmosphereFramework
Oct 02, 2015 3:39:28 PM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Initializing Mojarra 2.2.4 ( 20131003-1354 https://svn.java.net/svn/mojarra~svn/tags/2.2.4@12574) for context ''
Oct 02, 2015 3:39:28 PM com.sun.faces.spi.InjectionProviderFactory createInstance
INFO: JSF1048: PostConstruct/PreDestroy annotations present.  ManagedBeans methods marked with these annotations will have said annotations processed.
Oct 02, 2015 3:39:29 PM org.primefaces.webapp.PostConstructApplicationEventListener processEvent
INFO: Running on PrimeFaces 5.2
2015-10-02 15:39:29.904:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:9000
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 5 seconds.

mvn tomcat7:运行

Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building example-myproject Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) @ myproject-frontend >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ myproject-frontend ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] Copying 12 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ myproject-frontend ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) @ myproject-frontend <<<
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ myproject-frontend ---
[INFO] Running war on http://localhost:9000/
[INFO] Using existing Tomcat server configuration at /home/jscherman/Documents/Workspace/myproject/myproject-frontend/target/tomcat
[INFO] create webapp with contextPath: 
Oct 02, 2015 3:47:04 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-9000"]
Oct 02, 2015 3:47:04 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Oct 02, 2015 3:47:04 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Oct 02, 2015 3:47:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: 
        http://java.sun.com/jsf/html
     is already defined
Oct 02, 2015 3:47:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://java.sun.com/jsf/core is already defined
Oct 02, 2015 3:47:09 PM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://mojarra.dev.java.net/mojarra_ext is already defined
Oct 02, 2015 3:47:09 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing AtmosphereFramework
Oct 02, 2015 3:47:09 PM org.apache.catalina.core.ApplicationContext log
INFO: Spring WebApplicationInitializers detected on classpath: [org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration@5de1865]
Oct 02, 2015 3:47:09 PM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Initializing Mojarra 2.2.4 ( 20131003-1354 https://svn.java.net/svn/mojarra~svn/tags/2.2.4@12574) for context ''
Oct 02, 2015 3:47:10 PM com.sun.faces.spi.InjectionProviderFactory createInstance
INFO: JSF1048: PostConstruct/PreDestroy annotations present.  ManagedBeans methods marked with these annotations will have said annotations processed.
Oct 02, 2015 3:47:10 PM org.primefaces.webapp.PostConstructApplicationEventListener processEvent
INFO: Running on PrimeFaces 5.2
Oct 02, 2015 3:47:10 PM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Initializing Mojarra 2.2.4 ( 20131003-1354 https://svn.java.net/svn/mojarra~svn/tags/2.2.4@12574) for context ''
Oct 02, 2015 3:47:10 PM com.sun.faces.spi.InjectionProviderFactory createInstance
INFO: JSF1048: PostConstruct/PreDestroy annotations present.  ManagedBeans methods marked with these annotations will have said annotations processed.
Oct 02, 2015 3:47:11 PM com.sun.faces.mgbean.BeanManager addBean
WARNING: JSF1074: Managed bean named 'oauth2Client' has already been registered.  Replacing existing managed bean class type com.mybusiness.clients.myproject.OAuth2ServiceClient with com.mybusiness.clients.myproject.OAuth2ServiceClient.
Oct 02, 2015 3:47:11 PM org.primefaces.webapp.PostConstructApplicationEventListener processEvent
INFO: Running on PrimeFaces 5.2
Oct 02, 2015 3:47:11 PM org.primefaces.webapp.PostConstructApplicationEventListener processEvent
INFO: Running on PrimeFaces 5.2
Oct 02, 2015 3:47:11 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-9000"]

mvn依赖:树

[INFO] com.mybusiness.myproject:myproject-frontend:war:0.0.1-SNAPSHOT
[INFO] +- org.primefaces:primefaces:jar:5.2:compile
[INFO] +- javax.servlet:javax.servlet-api:jar:3.0.1:provided
[INFO] +- com.sun.faces:jsf-impl:jar:2.2.4:compile
[INFO] +- com.sun.faces:jsf-api:jar:2.2.4:provided
[INFO] +- com.mybusiness.myproject:myproject-service-client:jar:0.0.1-SNAPSHOT:compile
[INFO] |  +- com.mybusiness.myproject:myproject-api:jar:0.0.1-SNAPSHOT:compile
[INFO] |  |  +- org.springframework.hateoas:spring-hateoas:jar:0.16.0.RELEASE:compile
[INFO] |  |  |  \- org.objenesis:objenesis:jar:2.1:compile
[INFO] |  |  \- com.mybusiness.myproject:myproject-framework:jar:0.0.1-SNAPSHOT:compile
[INFO] |  |     +- org.springframework.boot:spring-boot-starter-web:jar:1.2.5.RELEASE:compile
[INFO] |  |     |  +- org.springframework.boot:spring-boot-starter:jar:1.2.5.RELEASE:compile
[INFO] |  |     |  |  +- org.springframework.boot:spring-boot:jar:1.2.5.RELEASE:compile
[INFO] |  |     |  |  \- org.springframework.boot:spring-boot-autoconfigure:jar:1.2.5.RELEASE:compile
[INFO] |  |     |  |     \- org.yaml:snakeyaml:jar:1.14:compile
[INFO] |  |     |  \- org.hibernate:hibernate-validator:jar:5.1.3.Final:compile
[INFO] |  |     |     +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  |     |     +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] |  |     |     \- com.fasterxml:classmate:jar:1.0.0:compile
[INFO] |  |     +- org.springframework.boot:spring-boot-starter-aop:jar:1.2.5.RELEASE:compile
[INFO] |  |     |  +- org.aspectj:aspectjrt:jar:1.8.6:compile
[INFO] |  |     |  \- org.aspectj:aspectjweaver:jar:1.8.6:compile
[INFO] |  |     +- org.springframework.boot:spring-boot-starter-security:jar:1.2.5.RELEASE:compile
[INFO] |  |     +- org.springframework.boot:spring-boot-starter-test:jar:1.2.5.RELEASE:compile
[INFO] |  |     |  +- junit:junit:jar:4.12:compile
[INFO] |  |     |  +- org.mockito:mockito-core:jar:1.10.19:compile
[INFO] |  |     |  +- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] |  |     |  +- org.hamcrest:hamcrest-library:jar:1.3:compile
[INFO] |  |     |  \- org.springframework:spring-test:jar:4.1.7.RELEASE:compile
[INFO] |  |     +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.2.5.RELEASE:compile
[INFO] |  |     |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.2.5.RELEASE:compile
[INFO] |  |     |  |  +- org.springframework:spring-jdbc:jar:4.1.7.RELEASE:compile
[INFO] |  |     |  |  \- org.springframework:spring-tx:jar:4.1.7.RELEASE:compile
[INFO] |  |     |  +- org.hibernate:hibernate-entitymanager:jar:4.3.10.Final:compile
[INFO] |  |     |  |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  |     |  |  +- org.hibernate:hibernate-core:jar:4.3.10.Final:compile
[INFO] |  |     |  |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |     |  |  |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] |  |     |  |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |     |  |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  |     |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  |     |  |  \- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  |     |  +- javax.transaction:javax.transaction-api:jar:1.2:compile
[INFO] |  |     |  +- org.springframework:spring-orm:jar:4.1.7.RELEASE:compile
[INFO] |  |     |  +- org.springframework.data:spring-data-jpa:jar:1.7.3.RELEASE:compile
[INFO] |  |     |  |  +- org.springframework.data:spring-data-commons:jar:1.9.3.RELEASE:compile
[INFO] |  |     |  |  \- org.slf4j:jcl-over-slf4j:jar:1.7.12:runtime
[INFO] |  |     |  \- org.springframework:spring-aspects:jar:4.1.7.RELEASE:compile
[INFO] |  |     +- org.springframework.plugin:spring-plugin-core:jar:1.1.0.RELEASE:compile
[INFO] |  |     +- mysql:mysql-connector-java:jar:5.1.35:compile
[INFO] |  |     +- com.mybusiness.framework:mybusiness-fwk-logging:jar:1.4.148:compile
[INFO] |  |     +- commons-io:commons-io:jar:1.3.2:compile
[INFO] |  |     +- org.apache.commons:commons-lang3:jar:3.4:compile
[INFO] |  |     \- ma.glasnost.orika:orika-core:jar:1.4.5:compile
[INFO] |  |        +- org.javassist:javassist:jar:3.16.1-GA:compile
[INFO] |  |        +- com.thoughtworks.paranamer:paranamer:jar:2.3:compile
[INFO] |  |        +- com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:jar:1.2_jdk5:compile
[INFO] |  |        \- com.carrotsearch:java-sizeof:jar:0.0.4:compile
[INFO] |  +- com.mybusiness.myproject:myproject-core-commons:jar:0.0.1-SNAPSHOT:compile
[INFO] |  +- org.springframework:spring-web:jar:4.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-aop:jar:4.2.0.RELEASE:compile
[INFO] |  |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  |  +- org.springframework:spring-beans:jar:4.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-context:jar:4.2.0.RELEASE:compile
[INFO] |  |  |  \- org.springframework:spring-expression:jar:4.2.0.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-core:jar:4.2.0.RELEASE:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.5.3:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.5.3:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.5.3:compile
[INFO] |  +- org.jenkins-ci.plugins:testInProgress-client:jar:1.4:compile
[INFO] |  |  \- org.json:json:jar:20140107:compile
[INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.7.7:compile
[INFO] |  |  \- log4j:log4j:jar:1.2.17:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO] |  +- org.springframework.security.oauth:spring-security-oauth2:jar:2.0.7.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-webmvc:jar:4.0.9.RELEASE:compile
[INFO] |  |  +- org.springframework.security:spring-security-core:jar:3.2.6.RELEASE:compile
[INFO] |  |  +- org.springframework.security:spring-security-config:jar:3.2.6.RELEASE:compile
[INFO] |  |  +- org.springframework.security:spring-security-web:jar:3.2.6.RELEASE:compile
[INFO] |  |  +- commons-codec:commons-codec:jar:1.6:compile
[INFO] |  |  \- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.13:compile
[INFO] |  |     \- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
[INFO] |  \- org.apache.httpcomponents:httpclient:jar:4.5:compile
[INFO] |     +- org.apache.httpcomponents:httpcore:jar:4.4.1:compile
[INFO] |     \- commons-logging:commons-logging:jar:1.2:compile
[INFO] +- org.glassfish:javax.faces:jar:2.2.8:compile
[INFO] \- org.atmosphere:atmosphere-runtime:jar:2.4.0-RC3:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.767s
[INFO] Finished at: Fri Oct 02 16:04:53 ART 2015
[INFO] Final Memory: 19M/341M
[INFO] ------------------------------------------------------------------------

mvn依赖:analyze-dep-mgt

Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building example-myproject Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:analyze-dep-mgt (default-cli) @ myproject-frontend ---
[INFO] Found Resolved Dependency / DependencyManagement mismatches:
[INFO]    Nothing in DepMgt.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.788s
[INFO] Finished at: Fri Oct 02 16:13:04 ART 2015
[INFO] Final Memory: 15M/216M
[INFO] ------------------------------------------------------------------------

0 个答案:

没有答案