I have a backing bean as following:
@Named
@RequestScoped
public class ClientNewBackingBean {
@Inject
private ClientFacade facade;
private Clien
If you are using JSF 2, you should use ViewScoped
bean.
If you were using standard JSF bean management annotation @ManagedBean
, you could have solved it by just placing the bean in the view scope by @ViewScoped.
@ManagedBean
@ViewScoped
public class ClientNewBackingBean implements Serializable {
@EJB
private ClientFacade facade;
// ...
In CDI, the @ViewScoped
however doesn't exist, the closest alternative is @ConversationScoped. You only have to start and stop it yourself.
@Named
@ConversationScoped
public class ClientNewBackingBean implements Serializable {
@Inject
private Conversation conversation;
// ...
@PostConstruct
public void init() {
conversation.begin();
}
public String submitAndNavigate() {
// ...
conversation.end();
return "someOtherPage?faces-redirect=true";
}
}
You can also use the CDI extension MyFaces CODI which will transparently bridge the JSF @ViewScoped
annotation to work properly together with @Named
:
@Named
@ViewScoped
public class ClientNewBackingBean implements Serializable {
@Inject
private ClientFacade facade;
// ...
A CODI alternative is to use @ViewAccessScoped
which lives as long as the subsequent requests reference the very same managed bean, irrespective of the physical view file used.
@Named
@ViewAccessScoped
public class ClientNewBackingBean implements Serializable {
@Inject
private ClientFacade facade;
// ...