2

I'm interested What is the proper way to use JSF pages with AJAX when I use CDI. I tested to configure the CDI beans with @SessionScoped but I found that there is a problem with AJAX.

Is it proper to use AJAX with CDI beans configured with @ConversationScoped?

And I found that I have to put conversation.begin(); into the Bean constructor and conversation.end(); into Java method which must be when the session is completed. Can I somehow do this automatically?

P.S Can I use this code to automatically free the resource when the user closes the page?

@Remove
public void finishIt(){
     conversation.end();
}
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • What CDI implementation are you using? Seam or Apache CODI? CODI has the `@ViewAccessScoped` that I quite like. It lives as long one of the jsf pages references it. It's a bit like an automatic conversation – phoenix7360 Mar 15 '13 at 08:57
  • I use the package `javax.inject.Named`. I suppose that this is part of the standard JSF framework. – Peter Penzov Mar 15 '13 at 09:30
  • _"... but I found that there is a problem with AJAX."_ What exactly is the problem? – Matt Handy Mar 15 '13 at 10:22
  • @MattHandy I described the problem here: http://stackoverflow.com/questions/9761568/jsf-input-field-is-not-updated – Peter Penzov Mar 15 '13 at 11:21
  • CODI, SEAM3, DeltaSpike (= merger of CODI, SEAM3 and others) are CDI extensions/add-ons. Esp. CODI and DeltaSpike can be used with Weld and OpenWebBeans (= the real CDI implementations). See e.g. https://builds.apache.org/view/A-F/view/DeltaSpike/ linked in http://incubator.apache.org/deltaspike/build.html – Dar Whi Mar 17 '13 at 23:30
  • javax.inject.Named is not part of JSF. It's an own specification. See JSR 330 (http://jcp.org/en/jsr/detail?id=330) which is used by CDI (http://jcp.org/en/jsr/detail?id=299) – Dar Whi Mar 17 '13 at 23:36

2 Answers2

4

And I found that I have to put conversation.begin(); into the Bean constructor and conversation.end(); into Java method which must be when the session is completed.

That's correct. See also among others How to replace @ManagedBean / @ViewScope by CDI in JSF 2.0/2.1 for a concrete code example.

Can I somehow do this automatically?

If you want a bean which must live as long as you're postbacking on a single view, then upgrade to at least JSF 2.2. It provides a CDI compatible @ViewScoped out the box.

If you however want a bean which must live as long as you reference it in a view, regardless of the view you're sitting in, then consider using @ViewAccessScoped of DeltaSpike instead. Once you navigate to a view which doesn't reference the bean anywhere, it will be trashed.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Can I use `@Remove public void finishIt(){ conversation.end(); }` to call `conversation.end();` when the JSF is closed. And also when I can expect JSF 2.2 to be ready to testing? – Peter Penzov Mar 15 '13 at 12:38
  • No. Your statement "the JSF is closed" makes also no sense by the way. JSF 2.2 is currently available as milestone release. See http://javaserverfaces.java.net/download.html The official release is planned around April 2013, as part of Java EE 7. – BalusC Mar 15 '13 at 12:42
  • One more interesting question about the problem. Is it planned CODI to replace CDI into the near future? I just want to plan which technology to use. – Peter Penzov Mar 15 '13 at 12:47
  • 1
    CODI does not replace CDI. It is just an extension to CDI. Like as that e.g. PrimeFaces is an extension to JSF. – BalusC Mar 15 '13 at 12:47
  • Ok, Thank you for the answer! I will wait for the new JSF version. – Peter Penzov Mar 15 '13 at 12:49
  • It's easy to go with CODI and move to DeltaSpike (= merger of CODI, SEAM and others) as soon as it is ready. We are using both in parallel without issues for large applications. – Dar Whi Mar 17 '13 at 23:24
1

By default the Conversation object is in transient state. Invocation of the begin method marks it as long-running (when a real conversation starts). Ending conversation (by invoking end method) marks Conversation object as transient.

A transient conversation scoped bean will live for a life cycle of single request .

long-ending conversation(initiated by conversation.begin) will run unless conversation.end is called.

Avinash Singh
  • 3,421
  • 2
  • 20
  • 21