Jsf ManagedBean已构建,但不应';T
我正在开发一个使用JSF2.0、NetBeans7.0和Glassfish 3.1的企业应用程序 我有一个ViewScope的托管bean。这是该类的声明:Jsf ManagedBean已构建,但不应';T,jsf,jsf-2,Jsf,Jsf 2,我正在开发一个使用JSF2.0、NetBeans7.0和Glassfish 3.1的企业应用程序 我有一个ViewScope的托管bean。这是该类的声明: @ManagedBean(name = "myBean") @ViewScoped public class MyMBean implements Serializable { 在@PostConstruct中,它具有以下内容: String id = FacesContext.getCurrentInstance().getExtern
@ManagedBean(name = "myBean")
@ViewScoped
public class MyMBean implements Serializable {
在@PostConstruct中,它具有以下内容:
String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
if (id == null) {
try {
FacesContext.getCurrentInstance().getExternalContext().redirect("home.xhtml");
FacesContext.getCurrentInstance().responseComplete();
} catch (Exception e) { }
return;
}
如果我转到使用这个托管bean的页面,并且id为null,那么一切正常,我会被重定向到主页。
问题是,当我导航到一个不使用这个托管bean的不同页面(比如说“otherpage.xhtml”)时,会执行PostConstruct方法,而它不应该执行!更糟糕的是:由于另一个页面的url没有“id”参数,bean试图重定向到主页;我得到了一个非法的例外
知道为什么在导航到不使用ViewScope的页面时构造ViewScope托管bean吗
编辑:
如果为了导航到“otherpage.xhtml”,我使用了“home.xhtml”中的commandlink,那么会创建6个额外的bean。
但是,如果我没有使用链接,而是在浏览器中键入url;它很好用。没有创建额外的bean。也许我实现链接的方式有问题。代码如下:
<h:form>
<h:commandLink value="Go to other page" action="otherPage" />
</h:form>
这是faces配置中的导航规则:
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>otherPage</from-outcome>
<to-view-id>/views/otherPage.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
*
其他页面
/views/otherPage.xhtml
有什么问题吗
谢谢!
Damian您肯定在视图或其模板/include/tag/composite文件中的某个地方有一个{myBean}
,或者作为视图引用的bean的@ManagedProperty
。在(post)构造函数中放置断点并调查stacktrace应该能够提供足够的信息,了解是谁/什么触发了bean的构造
与具体问题无关,
ExternalContext#redirect()
已经隐式调用了FacesContext#responseComplete()
,您不需要自己调用它。另见
更新:a
将其父帖子提交到当前页面(从而创建其所有相关bean!),然后根据导航结果转发/重定向到结果页面。您不应该使用commandlinks/commandbuttons进行简单的页到页导航。改用
<h:link value="Go to other page" outcome="views/otherPage" />
你最终也可以摆脱这个
。如果您真的坚持要保留该导航案例,请使用outcome=“otherPage”
<h:link value="Go to other page" outcome="views/otherPage" />
另见: