wood burning stoves 2.0*
The moose likes JSF and the fly likes Help Required Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Help Required" Watch "Help Required" New topic
Author

Help Required

Hemant Gupta Hg
Greenhorn

Joined: Jan 23, 2011
Posts: 11
I am using a sessionScoped JSF Managed Bean. Now, I want to call a method every time that bean is called. If I put the method in the SessionScoped Managed Bean then that method will be called only once as long as the session is there.

Can anyone help me about where should I put the method code so that every time that Session Bean is called, the method gets executed? This method gets some value from database.

Although, RequestScope bean will solve my purpose of calling that method every time the bean is called, but I lose some of the values like primary key which I am not using to set anywhere and also the variables in RequestScope bean will be reset, which I don't want.

I hope I am able to explain my problem clearly.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

You get better results and get them faster if you make your question topic reflect what you want to know. A lot of people only look at the message summaries, and a generic "Help required" topic doesn't tell them that there's anything of interest in your question.

It helps sometimes to be nit-picky about terms: You don't "call" a bean. You reference properties in a bean, and you invoke methods in a bean. The bean itself is just a container and does nothing.

So there are a number of things you might actually want when you say you want to "call a method every time that bean is called". You could be referring to property read/write operations, you could be referring to method invocations, or both. In JSF, since you don't have direct public access to the properties, that means that at least 3 different kinds of methods (4, including constructor) that could be looked at: property accessors (set/get), action processors, and event listeners. It would help if you could narrow that down.


Customer surveys are for companies who didn't pay proper attention to begin with.
Tina Ma
Ranch Hand

Joined: Nov 29, 2007
Posts: 198
What kind of method are you calling on the backing bean? And how are you calling the backing bean from the JSF?
To call the bean :MyBean, let's say you have to use this line in your JSF page:

This will call the getter/setter of the method getProp1() in MyBean.java - everytime you make that reference.
You can also use this:

That will also call the setProp1() in the MyBean.java, everytime you make that reference.

Now, if you want to call a separate method, say beanMethod() everytime you make a reference to the myBean from your JSF page in one way or the other, then I think this is what you should do:
Separate the variables(V) which should not be changed (into an application scoped bean(B1)) and the method(M) which needs to be called everytime in the constructor of a request scoped bean(B2). Then use the B2 as the backing bean of your JSF page.


Tina
SCJP 1.4, SCWCD 1.4
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

Tina Ma wrote:
To call the bean :MyBean, let's say you have to use this line in your JSF page:




That wasn't "calling" a bean. You can tell because the bean name has a property name attached to it. You're calling the property's accessors for that bean.
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218
See what I mean Tim?
Tina Ma
Ranch Hand

Joined: Nov 29, 2007
Posts: 198
technicality duly noted guys.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

No mere annoyingly pedantic human can ever be a match for a computer when it comes to insisting on knowing trivial distinctions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help Required