繁体   English   中英

JSF 2 @PostConstruct方法调用两次

[英]JSF 2 @PostConstruct method called twice

我有一个奇怪的行为:用@PostConstruct注释的方法被调用了两次。

对其进行调试,我发现在调用命令链接的操作方法mbean.edit之前,我的搜索页面已对其进行了调用。 我的bean MBeanSearch是请求范围的,我的MBean是视图范围的。

我的看法search.xhtml:

<h:commandLink value="#{var.value}" action="#{mbean.edit}">
    <f:param name="id" value="#{var.id}"/>
</h:commandLink>

我也有一个目标视图var.xhtml。

我的MBean bean的相关摘录:

    public String edit() {
        return "/pages/var.xhtml";
    }

    @PostConstruct
    public void initialize() { }

使用此代码,我的@PostConstruct将在我的edit方法之后被调用,然后再次被调用。

我认为我以错误的方式使用了@PostConstruct (我认为MBean需要在任何方法之前都启动)。 但是,在不同于搜索页面的页面中编辑对象的替代方法是什么?

问题似乎是在search.xhtmlvar.xhtml使用了视图范围受管理的bean mbean (我认为这还不清楚)。

当您调用操作方法时,您仍在search.xhtml 您将获得一个绑定到该视图的视图范围的bean实例,并首次调用@PostConstruct方法。

action方法返回第二个页面的视图ID var.xhtml ,JSF导航到该页面。 如果在此页面中也使用mbean ,则随着视图的更改,您将获得该bean的新实例。 这说明了对@PostConstruct方法的第二次调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM