所有 @ViewScoped 注释(JSF、CDI、OmniFaces)都像 @RequestScoped

All @ViewScoped annotations (JSF, CDI, OmniFaces) act like @RequestScoped

我正在使用:Wildfly 8.2、JSF 2.2 (Mojarra 2.2.8)、Omnifaces 2.1 和 java 7.

问题是我创建了一些对象,并且每个请求都会重新创建它们(空)。 我也尝试过使用 conversationScope,但同样的事情发生了。

使用 sessionScope 就可以了。

控制器:

@Named
@ViewScoped
public class Teste implements Serializable {

    private static final long   serialVersionUID = 1L;
    private Map<String, String> passwordReminderUsers;
    private String              key;

    @PostConstruct
    public void init() {
        if (!FacesContext.getCurrentInstance().isPostback())
            passwordReminderUsers = new HashMap<String, String>();
    }

    public void add() {
        passwordReminderUsers.put(key, "b");
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}

每次回发时 passwordReminderUsers 都是 null,导致 NPE。

查看:

    <h:form id="form-login" prependId="false">

        <h:inputText value="#{teste.key}" />
        <h:commandButton value="add" action="#{teste.add()}" />
    </h:form>

我做了一些测试,在没有 primefaces 的情况下执行请求,使用 f:ajax 和其他几个,没有任何效果。

此代码:JSF view scoped bean: #{authenticator} 显示每个请求都会重新创建 bean。

可能相关: 我们使用的是 JSF 2.0,几个月前更新到 JSF 2.2。 我必须使用这些命名空间:

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:c="http://java.sun.com/jsp/jstl/core" 
    xmlns:pe="http://primefaces.org/ui/extensions" 
    template="/WEB-INF/templates/default.xhtml">

因为新的 xmlns.jcp.org 无法正常工作并开始破坏页面并且不呈现 jsf 组件。

MAVEN pom:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- maven-compiler-plugin -->
        <maven.compiler.target>1.7</maven.compiler.target>
        <maven.compiler.source>1.7</maven.compiler.source>
    </properties>

    <repositories>
        <repository>
            <id>jvnet-nexus-releases</id>
            <name>jvnet-nexus-releases</name>
            <url>http://maven.java.net/content/repositories/releases/</url>
        </repository>
        <repository>
            <id>jboss-public-repository-group</id>
            <name>JBoss Public Repository Group</name>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
            <layout>default</layout>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
            </snapshots>
        </repository>
        <repository>
            <id>prime-repo</id>
            <name>Prime Repo</name>
            <url>http://repository.primefaces.org</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>jboss-public-repository-group</id>
            <name>JBoss Public Repository Group</name>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.wildfly.bom</groupId>
                <artifactId>jboss-javaee-7.0-with-hibernate</artifactId>
                <version>8.2.0.Final</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>5.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.omnifaces</groupId>
            <artifactId>omnifaces</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>

Wildfly 登录启动

java.class.path = C:\wildfly-8.2.0.Final\jboss-modules.jar
    java.class.version = 51.0
    java.endorsed.dirs = C:\Program Files\Java\jre7\lib\endorsed
    java.ext.dirs = C:\Program Files\Java\jre7\lib\ext;C:\windows\Sun\Java\lib\ext
    java.home = C:\Program Files\Java\jre7
    java.io.tmpdir = C:\Users\VICTOR~1.BEL\AppData\Local\Temp\
    java.library.path = C:\Program Files\Java\jre7\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;native;C:\Program Files (x86)\Java\jre7\bin;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\maven\apache-maven-3.2.1\bin;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Microsoft SQL Server0\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC0\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Microsoft SQL Server0\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;C:\Program Files (x86)\Java\jre7\bin;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\maven\apache-maven-3.2.1\bin;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Microsoft SQL Server0\Tools\Binn\;C:\Program Files\Microsoft SQL Server0\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;c:\Program Files (x86)\Microsoft SQL Se;c:\cvs.exe;C:\Program Files (x86)\IDM Computer Solutions\UltraEdit\;.;;.;;.;;.
    java.net.preferIPv4Stack = true
    java.runtime.name = Java(TM) SE Runtime Environment
    java.runtime.version = 1.7.0_51-b13
    java.specification.name = Java Platform API Specification
    java.specification.vendor = Oracle Corporation
    java.specification.version = 1.7
    java.util.logging.manager = org.jboss.logmanager.LogManager
    java.vendor = Oracle Corporation
    java.vendor.url = http://java.oracle.com/
    java.vendor.url.bug = http://bugreport.sun.com/bugreport/
    java.version = 1.7.0_51
    java.vm.info = mixed mode
    java.vm.name = Java HotSpot(TM) 64-Bit Server VM
    java.vm.specification.name = Java Virtual Machine Specification
    java.vm.specification.vendor = Oracle Corporation
    java.vm.specification.version = 1.7
    java.vm.vendor = Oracle Corporation
    java.vm.version = 24.51-b03
    javax.management.builder.initial = org.jboss.as.jmx.PluggableMBeanServerBuilder
    javax.xml.datatype.DatatypeFactory = __redirected.__DatatypeFactory
    javax.xml.parsers.DocumentBuilderFactory = __redirected.__DocumentBuilderFactory
    javax.xml.parsers.SAXParserFactory = __redirected.__SAXParserFactory
    javax.xml.stream.XMLEventFactory = __redirected.__XMLEventFactory
    javax.xml.stream.XMLInputFactory = __redirected.__XMLInputFactory
    javax.xml.stream.XMLOutputFactory = __redirected.__XMLOutputFactory
    javax.xml.transform.TransformerFactory = __redirected.__TransformerFactory
    javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema = __redirected.__SchemaFactory
    javax.xml.xpath.XPathFactory:http://java.sun.com/jaxp/xpath/dom = __redirected.__XPathFactory
    jboss.bind.address = localhost
    jboss.bind.address.management = localhost
    jboss.home.dir = C:\wildfly-8.2.0.Final
    jboss.host.name = wvox-000805
    jboss.modules.dir = C:\wildfly-8.2.0.Final\modules
    jboss.modules.system.pkgs = org.jboss.byteman
    jboss.node.name = wvox-000805
    jboss.qualified.host.name = wvox-000805
    jboss.server.base.dir = C:\wildfly-8.2.0.Final\standalone
    jboss.server.config.dir = C:\wildfly-8.2.0.Final\standalone\configuration
    jboss.server.data.dir = C:\wildfly-8.2.0.Final\standalone\data
    jboss.server.deploy.dir = C:\wildfly-8.2.0.Final\standalone\data\content
    jboss.server.log.dir = C:\wildfly-8.2.0.Final\standalone\log
    jboss.server.name = wvox-000805
    jboss.server.persist.config = true
    jboss.server.temp.dir = C:\wildfly-8.2.0.Final\standalone\tmp
    line.separator = 

    logging.configuration = file:/C:/wildfly-8.2.0.Final/standalone/configuration/logging.properties
    module.path = C:/wildfly-8.2.0.Final/modules
    org.jboss.boot.log.file = C:/wildfly-8.2.0.Final/standalone/log/boot.log
    org.jboss.logmanager.nocolor = true
    org.jboss.resolver.warning = true
    org.xml.sax.driver = __redirected.__XMLReaderFactory
    os.arch = amd64
    os.name = Windows 7
    os.version = 6.1
    path.separator = ;
    program.name = JBossTools: WildFly 8.x
    sun.arch.data.model = 64
    sun.boot.class.path = C:\Program Files\Java\jre7\lib\resources.jar;C:\Program Files\Java\jre7\lib\rt.jar;C:\Program Files\Java\jre7\lib\sunrsasign.jar;C:\Program Files\Java\jre7\lib\jsse.jar;C:\Program Files\Java\jre7\lib\jce.jar;C:\Program Files\Java\jre7\lib\charsets.jar;C:\Program Files\Java\jre7\lib\jfr.jar;C:\Program Files\Java\jre7\classes
    sun.boot.library.path = C:\Program Files\Java\jre7\bin
    sun.cpu.endian = little
    sun.cpu.isalist = amd64
    sun.desktop = windows
    sun.io.unicode.encoding = UnicodeLittle
    sun.java.command = org.jboss.modules.Main -mp C:/wildfly-8.2.0.Final/modules -jaxpmodule javax.xml.jaxp-provider org.jboss.as.standalone -b localhost --server-config=standalone.xml -Djboss.server.base.dir=C:\wildfly-8.2.0.Final\standalone
    sun.java.launcher = SUN_STANDARD
    sun.jnu.encoding = Cp1252
    sun.management.compiler = HotSpot 64-Bit Tiered Compilers
    sun.os.patch.level = Service Pack 1
    sun.rmi.dgc.client.gcInterval = 3600000
    sun.rmi.dgc.server.gcInterval = 3600000
    user.country = US
    user.country.format = BR
    user.dir = C:\wildfly-8.2.0.Final\bin

Maybe related: We were using the JSF 2.0 and updated some months ago to JSF 2.2. I have to use these namespaces [snip] Because the new xmlns.jcp.org ones didn't work and started to break the pages and not render jsf components

这绝对是一个强有力的线索,表明您实际上没有使用 JSF 2.2,但仍然是旧版本。如果在 运行 WildFly 上发生这种情况,它应该已经提供开箱即用的 JSF 2.2,那么这意味着您的运行时类路径被多个不同的 JSF 版本污染,或者 WildFly 实例被修改。

它应该可以在原始和未修改的 WildFly 实例上正常工作。