在每次回发时重建 ViewScoped bean

ViewScoped bean reconstructed on every postback

我有一个 @ViewScoped 豆子

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class MyBean implements Serializable

它在每次回发时都会重建(具有所有明显的后果,例如丢失初始属性)。这是怎么引起的,我该如何解决?如果无法解决,是否有任何解决方法?

由于非技术原因,我无法将 bean 的范围更改为 @SessionScoped

配置了以下状态保存方法:

<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>

我的 pom.xml

中的所有 JSF 相关文件
        <!-- JSF Files -->
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>mojarra-jsf-impl</artifactId>
        <version>2.0.0-b04</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.12</version>
    </dependency>

    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.0.2-FCS</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>javax.faces-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

这没有意义。

<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>mojarra-jsf-impl</artifactId>
    <version>2.0.0-b04</version>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-api</artifactId>
    <version>2.2.12</version>
</dependency>
<dependency>
    <groupId>javax.faces</groupId>
    <artifactId>jsf-impl</artifactId>
    <version>2.0.2-FCS</version>
</dependency>
<dependency>
    <groupId>javax.faces</groupId>
    <artifactId>javax.faces-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
</dependency>

全部摆脱。他们彼此不相容。这可能会产生很多后果,其中破坏视图范围确实是其中之一。

要在 Tomcat 上使用 Mojarra,只需获取单个 org.glassfish:javax.faces 依赖项。

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.faces</artifactId>
    <version><!-- Check javaserverfaces.java.net for latest version --></version>
</dependency>

Latest stable 2.2.x version 目前是 2.2.12.

相关的潜在原因:

  • @ViewScoped bean recreated on every postback request when using JSF 2.2
  • @ViewScoped Managed bean loads many times during postback
  • @ViewScoped calls @PostConstruct on every postback request