【问题标题】:All @ViewScoped annotations (JSF, CDI, OmniFaces) act like @RequestScoped所有@ViewScoped 注释(JSF、CDI、OmniFaces)都像@RequestScoped
【发布时间】:2015-09-01 14:33:48
【问题描述】:

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

问题是我创建了一些对象,并且每个请求都重新创建了它们(null)。 我也尝试过使用 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 Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\120\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 Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL Server\100\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

【问题讨论】:

  • 为了排除一个和另一个,请分享初始请求和后续回发请求的HTTP请求/响应头和表单数据。
  • @BalusC 我不明白。我正在方法“@postConstruct”中实例化一个地图。没有非空结果。只是创建bean。在下一个请求中,该对象已经为空(它在返回任何东西之前)。我错过了什么吗?
  • @BalusC 已编辑。还是会发生。在 postConstruct 之后,当按钮被调用时,我得到了空指针,因为我创建了实例,所以它不应该发生。
  • 好的。谢谢。我收集到JSESSIONID cookie 标头和javax.faces.ViewState 请求参数在每次回发时都具有相同的值。所以这可以排除在原因之外。我减少了问题中的噪音。 xmlns.jcp.org XML 命名空间不起作用绝对是一个线索。 /WEB-INF/lib 中有任何 JavaEE/JSF JAR 吗?类路径中的库版本冲突会导致奇怪的副作用,尽管我以前从未见过这种情况。
  • 2.1.7-jbossorg 被识别为捆绑在 JBoss AS 7.1 或 EAP 6.0 中的 Mojarra 版本。这绝对不是 JSF 2.2。您确定要部署到 WildFly 8.2 吗?它是本地服务器还是远程服务器?

标签: jsf cdi jsf-2.2 omnifaces view-scope


【解决方案1】:

可能相关:我们使用的是 JSF 2.0,几个月前更新到 JSF 2.2。我必须使用这些命名空间 [snip] 因为新的 xmlns.jcp.org 不起作用并开始破坏页面并且不呈现 jsf 组件

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

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

【讨论】:

    猜你喜欢
    • 2015-04-15
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 2015-07-14
    • 1970-01-01
    • 2011-07-10
    相关资源
    最近更新 更多