从页面到页面导航为什么@postconstruct init()方法被调用两次?

时间:2017-10-13 20:30:33

标签: jsf-2

我有以下情况。当页面导航的页面出现时,我试图将参数从一个bean(例如fooBean)传递到另一个bean(例如barBean)。

两个bean都是ViewScoped。

加载foo.xhtml页面时会创建fooBean。从那里我将参数“id”传递给bar.xhtml页面。

这是foo.xhtml页面:

<h:form>
    <p:dataTable var="item" value="#{fooBean.items}" ....>
        <p:column ..>
            <h:commandLink action="#{fooBean.next}">
                <h:outputText value="#{item.name}"/>
                <f:setPropertyActionListener value="#{item.id}" target="#{barBean.newId}" />
            </h:commandLink>
        </p:column>
    </p:dataTable>
</h:form>

这是fooBean.next()方法:

public String next()  { return "bar.xhtml?faces-redirect=true" }

当我从foo.xhtml页面点击h:commandLink(记录或行)时,它被重定向到“bar.xhtml”。

这是barBean:

@ManagedBean
@ViewScoped
public class BarBean implements ... {

    private String newId;

    @PostConstruct
    public void init() { ... }

    // getter and setter for newId
}

我注意到barBean的@PostConstruct init()方法被触发了两次。使用第一个调用/触发器时,我期望barBean的this.newId变量将被设置为传递值(例如id = 12345),它通过检查日志语句(setter被调用并且this.newId = 12345)来完成。

然后再次触发init()方法,但我认为this.newId将保留传递的值但它变为null。

有人可以向我解释发生了什么吗?

0 个答案:

没有答案