Jsf 2 JSF2@PostConstruct方法调用了两次
我有一个奇怪的行为:我用Jsf 2 JSF2@PostConstruct方法调用了两次,jsf-2,postconstruct,Jsf 2,Postconstruct,我有一个奇怪的行为:我用@PostConstruct注释的方法被调用了两次 调试它时,我看到我的搜索页面在调用命令链接的action methodosmbean.edit之前调用了它。我的beanMBeanSearch是请求范围,我的MBean是视图范围 My view search.xhtml: <h:commandLink value="#{var.value}" action="#{mbean.edit}"> <f:param name="id" value="#
@PostConstruct
注释的方法被调用了两次
调试它时,我看到我的搜索页面在调用命令链接的action methodosmbean.edit
之前调用了它。我的beanMBeanSearch
是请求范围,我的MBean
是视图范围
My view search.xhtml:
<h:commandLink value="#{var.value}" action="#{mbean.edit}">
<f:param name="id" value="#{var.id}"/>
</h:commandLink>
有了这段代码,我的@PostConstruct
将在我的编辑方法之后被调用,然后再次调用
我认为我以错误的方式使用了
@PostConstruct
(我认为MBean
需要在任何方法之前启动)。但是,在不同于搜索页面的页面中编辑对象的替代方法是什么呢?问题似乎是在search.xhtml
和var.xhtml
中使用了视图范围的托管beanmbean
(我认为有点不清楚)
当您调用操作方法时,您的视图仍在查看search.xhtml
。您将获得一个绑定到此视图的视图范围的bean实例,以及对@PostConstruct
方法的第一次调用
action方法返回第二个页面的视图ID
var.xhtml
,JSF导航到此页面。如果您在这个页面中也使用mbean
,那么随着视图的更改,您会得到一个新的bean实例。这解释了对@PostConstruct
方法的第二次调用。我们需要更多信息来跟踪问题。照目前的情况看,米奇的解释是最有可能的。
public String edit() {
return "/pages/var.xhtml";
}
@PostConstruct
public void initialize() { }