aspose file tools*
The moose likes JSF and the fly likes Question about bean creating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Question about bean creating " Watch "Question about bean creating " New topic
Author

Question about bean creating

Adam Kronicki
Ranch Hand

Joined: Sep 01, 2009
Posts: 68
Hi,

I'm quite new with JSF and while creating my first page I came up to a situation when I had to initialize the JAXBcontex. This usually takes some seconds and should be done only once, at the start of the application. I began reading about initializing in jsf and run into some solutions concerning creating another servlet that would be launched on the startup. Although the solution seemed correct for my desire it was as well quite vague and hard for me to comprehend. First I wanted to check how exactly the proces of creating beans looks like. I though that with each request send to the servlet (f.e. pressing a button) a new bean is created. So I've made a simple test to see how it works using the random generator. I have created 3 variables and changed them in different places of the code. The first two were in the constructor of the bean and the third was in a function called by pressed button.


....
public Bean()
{
randomGenerator = new Random();
randomInt2 = randomGenerator.nextInt(100);
if (jc == null)
{
m = new HlResults();
jc = m.getJAXBContext();
randomInt1 = randomGenerator.nextInt(100);
}
...
search{
...
randomInt3 = randomGenerator.nextInt(100);
}
}


To my big surprise with each press of the button only the third variable changed. Even closing and opening the window resulted in the same values for the first two variables. This was even more puzzling since I set the bean for session scope.
Could any one please explain it in a noob-firendly way? :> Is it possible that if I would create the JAXB context the same way as above it would be done only once for the session? If I'm wrong please give me some advice how to manage it properly.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

In JSF, there is the concept of managed beans. Managed beans are simply beans that are constructed by the JSF framework instead of by explicit program logic. To make that happen, you declare them - plus any initialization information for them - in the faces-config file.

One of the things you declare for a managed bean is its scope. Scope is actually a basic JSP bean concept and comes in 4 flavors: application, session, request, and page, but page scope is not meaningful in basic JSF, due to the way it operates. It sounds like you want an application-scope bean, but the default, I believe, is request scope. In request scope, the bean is created and destroyed each time you send a request to the server (NOT when you enter and leave a page, which may entail multiple requests).


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about bean creating