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.
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.
An IDE is no substitute for an Intelligent Developer.
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.