使用<h:link includeviewparams =“true”>

时间:2015-09-22 16:41:47

标签: jsf destroy view-scope viewparams

我使用的是Mojarra 2.2.12。我有一个案例,@ViewScoped @ManagedBean会在页面加载时立即销毁,尽管视图没有结束。这个问题可以在<h:body>

中单独使用以下内容重现
<h:outputText value="#{testBean.value}" />
<h:link outcome="other" includeViewParams="true">link</h:link>

other必须引用不同的视图而不是相同的视图。重现问题不需要<f:viewParam>

下面的bean:

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class TestBean implements Serializable {

    @PostConstruct
    public void init() {
        System.out.println("@PostConstruct on " + this);
    }

    @PreDestroy
    public void clear() {
        System.out.println("@PreDestroy on " + this);
    }

    public String getValue() {
        return "test";
    }

}

如果我们删除includeViewParams="true"属性,那么bean不会立即被销毁。为什么includeViewParams="true"导致这种行为?

1 个答案:

答案 0 :(得分:0)

将此依赖项添加到pom.xml,我被传递了。

<dependency>
    <groupId>javax.enterprise</groupId>
    <artifactId>cdi-api</artifactId>
    <version>1.2</version>
</dependency>