@ViewScoped bean recreated on every postback request when using JSF 2.2

China☆狼群 提交于 2019-11-26 13:46:57
BalusC

This,

import javax.faces.view.ViewScoped;

is the JSF 2.2-introduced CDI-specific annotation, intented to be used in combination with CDI-specific bean management annotation @Named.

However, you're using the JSF-specific bean management annotation @ManagedBean.

import javax.faces.bean.ManagedBean;

You should then be using any of the scopes provided by the very same javax.faces.bean package instead. The right @ViewScoped is over there:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class MenusBean implements Serializable{

If you use the wrong combination, the bean behaves as a @RequestScoped bean and be recreated on each call.

Alternatively, if your environment supports CDI (GlassFish/JBoss/TomEE with Weld, OpenWebBeans, etc), then you could also replace @ManagedBean by @Named:

import javax.inject.Named;
import javax.faces.view.ViewScoped;

@Named
@ViewScoped
public class MenusBean implements Serializable{

It's recommended to move to CDI. The JSF-specific bean management annotations are candidate for deprecation in future JSF / Java EE versions as everything is slowly moving/unifying towards CDI.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!