无法在servlet容器中使用带有jsf 2.3的bean [重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

我不久前开始测试JSF 2.3。但我无法获得最重要的功能之一。使用ManagedBeans。我尝试了很多,使用不同的servlet容器(Tomcat 8&9,Jetty 9.2)。但没有任何帮助。希望有人能看到我在资源上的失败。这令人沮丧。我调试但是从未到达bean。 primefaces组件工作正常(primefaces lib不是原因)。但我从来没有得到bean数据。 PS。我正在使用myfaces但是使用mojarra我遇到了同样的问题。

我的豆子:

import javax.enterprise.context.RequestScoped;
import javax.faces.annotation.FacesConfig;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import java.io.Serializable;

@Named(value = "sampleBean")
@RequestScoped
@FacesConfig(version = FacesConfig.Version.JSF_2_3)
public class SampleBean implements Serializable {
    private String specTitle;
    private String specVersion;
    private String implTitle;
    private String implVersion;


    public SampleBean() {
        Package facesPackage = FacesContext.class.getPackage();
        specVersion = facesPackage.getSpecificationVersion();
        specTitle = facesPackage.getSpecificationTitle();
        implTitle = facesPackage.getImplementationTitle();
        implVersion = facesPackage.getImplementationVersion();
    }

    public String info() {
        return "hello from sampleBean!";
    }

    public String getSpecTitle() {
        return specTitle;
    }

    public String getSpecVersion() {
        return specVersion;
    }

    public String getImplTitle() {
        return implTitle;
    }

    public String getImplVersion() {
        return implVersion;
    }

}

我的配置bean:

import javax.faces.annotation.FacesConfig;

@FacesConfig(version = FacesConfig.Version.JSF_2_3)
public class ConfigurationBean {
}

我的脸蛋:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Hello JSF 2.3</title>
    <style>
        .col-1 { text-align: right; }
        .col-2 { font-weight: bold; }
    </style>
</h:head>
<h:body>

<div style="border-style: dashed" class="ui-g">
<p:calendar mode="inline"/>
</div>
<div style="border-style: double">
<h3>Hello from JSF 2.3!</h3>

<h:panelGrid columns="2" columnClasses="col-1, col-2">

    <h:outputText value="Specification:"/>
    <h:outputText value="#{sampleBean.specTitle}"/>

    <h:outputText value="Specification version:"/>
    <h:outputText value="#{sampleBean.specVersion}"/>

    <h:outputText value="Implementation:"/>
    <h:outputText value="#{sampleBean.implTitle}"/>

    <h:outputText value="Implementation version:"/>
    <h:outputText value="#{sampleBean.implVersion}"/>
</h:panelGrid>

<p>CDI injection support: <b>#{sampleBean.facesContextValue}</b></p>
<p>Running on: <b>#{application.serverInfo}</b></p>
</div>

我的web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app 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-app_3_1.xsd"
             version="3.1"><!-- Use web-app_4_0.xsd, version=4.0 after update to Java EE 8 -->

    <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>*.xhtml</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.ENABLE_CDI_RESOLVER_CHAIN</param-name>
        <param-value>truet</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.ENABLE_WEBSOCKET_ENDPOINT</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.validator.ENABLE_VALIDATE_WHOLE_BEAN</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>0</param-value> <!-- No Cache  -->
    </context-param>

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

</web-app>

我的build.gradle:

plugins {
    id 'war'
    id 'org.gretty' version '2.2.0'
}

group 'de.danri'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

gretty{
    //servletContainer="tomcat8"
    //servletContainer="tomcat9"
}

dependencies {
    // https://mvnrepository.com/artifact/org.apache.myfaces.core/myfaces-impl
    compile group: 'org.apache.myfaces.core', name: 'myfaces-impl', version: '2.3.1'
    // https://mvnrepository.com/artifact/org.apache.myfaces.core/myfaces-api
    compile group: 'org.apache.myfaces.core', name: 'myfaces-api', version: '2.3.1'
    // https://mvnrepository.com/artifact/org.primefaces/primefaces
    compile group: 'org.primefaces', name: 'primefaces', version: '6.2'
    // https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api
    compile group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.2.1-b03'
    // https://mvnrepository.com/artifact/javax.enterprise/cdi-api
    compile group: 'javax.enterprise', name: 'cdi-api', version: '2.0.SP1'
    // https://mvnrepository.com/artifact/javax.websocket/javax.websocket-api
    compile group: 'javax.websocket', name: 'javax.websocket-api', version: '1.1'
    // https://mvnrepository.com/artifact/javax.servlet/jstl
    compile group: 'javax.servlet', name: 'jstl', version: '1.2'
    // https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
    // https://mvnrepository.com/artifact/javax.validation/validation-api
    compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

beans.xml中:

<?xml version="1.0" encoding="UTF-8"?>

<beans 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/beans_1_1.xsd"
       version="1.1" bean-discovery-mode="all">
</beans>

faces-config.xml中:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.3"
              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_3.xsd">
</faces-config>
cdi managed-bean servlet-container jsf-2.3
1个回答
0
投票

谢谢你的女主人。好地址!解决方案:我添加了/META-INF/context.xml

<Context>
    <Resource name="BeanManager"
              auth="Container"
              type="javax.enterprise.inject.spi.BeanManager"
              factory="org.jboss.weld.resources.ManagerObjectFactory" />
</Context>

我添加了焊接servlet阴影到我的依赖项:

...
compile group: 'org.jboss.weld.servlet', name: 'weld-servlet-shaded', version: '3.0.5.Final'
...

最后,我使用本地tomcat来运行工件,而不是gradle'gretty'插件。看来这个插件有cdi的问题。

© www.soinside.com 2019 - 2024. All rights reserved.