1

My understanding is that

@Stateful
@ConversationScoped 

is allowed, and is normal usage.

With the new JSF 2.2 ViewScoped (javax.faces.view.ViewScoped) annotation, which is documented to be fully compatible with CDI scope annotations, does this mean that:

@Stateful
@ViewScoped

Is a viable combination?

2 Answers2

0

@Stateful is an EJB annotation, so technically your bean will be a stateful EJB bean, not a CDI bean. And it will only work in a full-blown application server. In case of (only) JSF 2.2 + CDI use:

@javax.inject.Named // to expose a bean in the EL context  
@javax.faces.view.ViewScoped // to make it view scoped

Also don't mix up the latter annotation with @javax.faces.bean.ViewScoped, it once took me a while to find the issue.
There's a nice example.

Yuri
  • 1,695
  • 1
  • 13
  • 23
  • Technically, all EJB beans are CDI beans ;-) – mabi Mar 11 '14 at 15:44
  • I am using a full application server. I think I mentioned in the question that I was referring to javax.faces.view.ViewScoped - I am particularly querying the viability of combined use of the 2 annotations. – user1575841 Mar 11 '14 at 20:08
0

keep in mind that with @stateful, every time you inject it you will get a new concrete instance, because you're indicating storing state with that annotation. also the EJB layer concept of session is not the same as the JSF layer concept of session, and confusing the two can create all sorts of issues for you.

JSF session is tied to a specific client maintained by either a cookie or url-rewriting (container configuration).

EJB session is tied to a specific method execution (@Stateless) or a concrete instance (@Stateful).

unless you need to have durable serialization and state maintenance within the EJB, stateful is not going to give you want you want. you're best bet will be to separate the transactional aspects into a @Stateless and store your state in a @ViewScoped that gets passed into the @Stateless methods.

him
  • 608
  • 5
  • 15