1

On a JSF page, every handle file upload event creates a new Instance of Bean. As if it is like a new client each time. Also it seems like the web browser is a different client too.

So, Is there a way that these FileUploadEvents coming from only one browser instantiates only one Bean?

I'm trying to use CDI instead of @ViewScoped JSF bean.

index.xhtml:

<p:fileUpload fileUploadListener="#{bean.handleFileUpload}"/>

Bean:

@Named
@ConversationScoped
public class Bean {

//code

@Inject
Conversation conversation;

public void HandleFileUpload(FileUploadEvent fileUploadEvent){
if(conversation.isTransient()){ //Always true :(
   conversation.begin(); 
}
//Beans get created each time it needs to access this method.

}
jacktrades
  • 7,224
  • 13
  • 56
  • 83
  • That code creats a new conversation every time it's called - what else could you possibly expect? – Matt Ball Jan 23 '13 at 19:48
  • Ok, so Can you give me ideas on how to avoid that, and have just one 1 conversation with 1 browser session? (avoiding @SessionScoped) Maybe programatically checking HttpSession? – jacktrades Jan 23 '13 at 19:52
  • 3
    You're looking for a scope in the middle of request and session, that's `@ViewScoped`. If you use a `@RequestScoped` that looks for the session, you're somewhat simulating the `@ViewScoped` and falling in JSF 1.x (already solved) problems. – Luiggi Mendoza Jan 23 '13 at 19:59
  • Looks like conversation id (cid) would be suited for this... – jacktrades Jan 23 '13 at 20:25
  • 1
    I can't answer this from top of head as I've never really used `@ConversationScoped`. I can only tell that you can use [MyFaces CODI](http://myfaces.apache.org/extensions/cdi/) to transparently bridge the JSF `@ViewScoped` annotation to CDI, so that you can basically have a `@Named @ViewScoped` bean. Does that work with PF file upload then? – BalusC Jan 23 '13 at 21:15
  • BalusC, Yes indeed this works on PrimeFaces – jacktrades Jan 23 '13 at 22:28
  • 1
    @BalusC How is it possible that you haven't worked with @ConversationScoped? Is it used too few? – jacktrades Jan 23 '13 at 22:37
  • 1
    Until the day of today, I've always used JSF managed beans in production applications. – BalusC Jan 23 '13 at 22:39
  • @BalusC Any particular reason of why not going into ConversationScoped? – jacktrades Jan 23 '13 at 22:47
  • We are using also `Weld` thus we are using the `ConversationScoped` implemented by `Weld` which is not provided in jsf implementation. The corresponding scope in JSF is `@ViewScoped` as far as I understood, but I haven't worked with them. Btw why don'T you just use SessionScoped bean since you want to keep the bean alive as long as the browser is open. – cubbuk Jan 24 '13 at 07:51
  • @cubbuk Because I like the different conversation per each browser tab behaviour. – jacktrades Jan 24 '13 at 11:03
  • Ok but can't you make `fileUploadBean` `sessionScoped` so that it can be shared for other `ConversationScoped` beans. So create a new `SessionScoped` bean which handles fileUploadEvent and inject that bean into your other `ConversationScoped` beans. – cubbuk Jan 24 '13 at 11:32
  • Yes I can do that, but I think what the creators of these technologies are trying to avoid, see @LuiggiMendoza comment – jacktrades Jan 24 '13 at 11:40
  • 1
    There was just no need to step over to CDI. But in future applications, we likely will and if JSF 2.2 isn't out yet, then in combination with CODI. Note that the upcoming JSF 2.2 will get a new `@FlowScoped` annotation which is CDI compatible and actually exactly what you need if you want to span multiple views. Also, the `@ViewScoped` will in JSF 2.2 finally be CDI compatible. JSF is this way slowly moving to CDI. For more, see also this article of my fellow Arjan Tijms: http://jdevelopment.nl/jsf-22/ – BalusC Jan 24 '13 at 19:57
  • As you said I have read that JSF is moving to CDI... that's why I give all my efforts to learn CDI :) – jacktrades Jan 24 '13 at 20:15

1 Answers1

2

I had a misconception of @ConversationScoped.

Conversation should start before visiting a given link.

Means that if one visit a link, with no conversation started previously, the bean will act similar to a @RequestScoped bean. Therefore refreshing one page attached to a @ConversationScoped will create & destroy each time.

Solution goes to start the conversation before visiting the link, so ?cid=10 will be included in the URL.

jacktrades
  • 7,224
  • 13
  • 56
  • 83
  • 1
    So, essentially, you forgot a `@PostConstruct public void init() { conversation.begin(); }`? See also http://stackoverflow.com/questions/14384369/how-to-replace-managedbean-viewscope-by-cdi/14384824#14384824 – BalusC Jan 24 '13 at 20:00
  • 1
    @BalusC Not at all, doing that will create lot of conversations if the conversation hasn't been initiated. – jacktrades Jan 24 '13 at 20:13
  • 1
    Yes right. That's also one of tradeoffs of `@ConversationScoped`. – BalusC Jan 24 '13 at 20:14