I have managed bean by name studentManagedBean. In that bean I have used post construct to intialize studentsList. In another managed bean testbean I was using
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
and redirecting to page students.xhtml where I used to display students.
My question is when I used the FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); and redirected to student.xtml page, init method(post construct) used to call two times. When I commented the above line, init method(post construct) now calls only one time.
can any one tell me what is this invalidate session will exactly do.
#ManagedBean(name = "studentManagedBean" )
#SessionScoped
public class StudentManagedBean implements Serializable {
private List<SBean> stud;
#PostConstruct
private void init(){
this.stud=dao.getAllStudInfo();
}
#ManagedBean(name = "testBean" )
#SessionScoped
public class TestBean implements Serializable {
public String navigate(String name){
if(name.equals("Add student")){
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "student";
}
Apparently, the session scoped bean StudentManagedBean is also referenced in the current view. When the view get built/restored, it may create the bean if it's referenced during view build time. But if you're invalidating the session thereafter, the session scoped bean get destroyed (obviously, since it's stored in the session scope) and will be re-created again when the target view still references it during rendering of the view.
This must make completely sense. If you don't want that the bean is created before you invalidate the session, simply don't reference it anywhere in the current view, either directly in the view, or indirectly as a managed property or a programmatic EL evaluation of another bean which is directly referenced in the current view.
If you can't immediately figure out where it's been referenced, just put a debug breakpoint in the bean's constructor and explore the call stack for the who/what/why.
Related
In my view scoped managedBean, i need to populate a list with data from DB.
Im doing this through a direct call from the constructor, something like this:
public MyClass(){
list=populateFromDb();
}
but this method can be called in a #PostConstruct annotated method, like:
public MyClass(){
}
#PostConstruct
populateFromDb(){...}
what is the difference between this?
If the bean has request scope, #PostConstruct will get executed every time. It will be called after the managed bean is instantiated, but before the bean is placed in scope. Such a method take no arguments, return void, and may not declare a checked exception to be thrown. Method may be public, protected, private, or package private. If the method throws an unchecked exception, the JSF implementation must not put the managed bean into service and no further menthods on that managed bean instance will be called.
public TrainingClassForm() {
}
#PostConstruct
public void init() {
if (this.trainingListModel.getListDataModel() != null) {
this.trainingListModel.getAllTrainingClasses();
}
}
Reffering you to this question of stackIn a managed bean, #PostConstruct is called after the regular Java object constructor.when the constructor is called, the bean is not yet initialized - i.e. no dependencies are injected. In the #PostConstruct method the bean is fully initialized and you can use the dependencies
#PostConstruct is the contract that guarantees that this method will be invoked only once in the bean lifecycle . It may happen (though unlikely) that a bean is instantiated multiple times by the container in its internal working, but it guarantees that #PostConstruct will be invoked only once.
If your class performs all of its initialization in the constructor, then #PostConstruct is indeed redundant.
However, if your class has its dependencies injected using setter methods, then the class's constructor cannot fully initialize the object, and sometimes some initialization needs to be performed after all the setter methods have been called, hence the use case of #PostConstructAlso see this and this
When the page loads for the first time, the #PostConstruct is called, but when I perform a postback on this page, the #PostConstruct is called again.
How can I make it to run only on the initial request and not on every postback?
#PostContruct
public void init() {
// charge combos....
}
public void submit() {
// action
}
Apparently your bean is request scoped and thus reconstructed on every HTTP request. I'm not exactly sure why you'd like to prevent the #PostConstruct from being called again, as you would otherwise end up with an "empty" bean state which might possibly lead to form submit errors, but okay, you could add a check if the current request is not a postback.
public void init() {
if (!FacesContext.getCurrentInstance().isPostback()) {
// charge combos....
}
}
This way the "charge combos" part won't be invoked on postbacks.
Or, maybe your actual question is not "How to prevent postconstruct from being called on postback?", but more "How to retain the same bean instance on postback?". In that case, you'd need to put the bean in the view scope instead of in the request scope.
#ManagedBean
#ViewScoped
public class Bean implements Serializable {
// ...
}
As long as you return null from action methods, this way the same bean instance will live as long as you're interacting with the same view by postbacks. This way the #PostConstruct won't be invoked (simply because the bean isn't been reconstructed).
See also:
How to choose the right bean scope?
use this import:
import javax.faces.view.ViewScoped; for #ViewScoped
I've tried every solution from a couple dozen google searches: getRequestMap, ElResolver, evaluateExpressionGet and so on. Nothing, absolutely nothing, works. I get null every time. Here is the relevant code:
#ManagedBean(name="readerBean")
#ViewScoped
public class ReaderBean implements Serializable {...
And in the PhaseListener:
public void beforePhase(PhaseEvent event) {
if (event.getPhaseId() == PhaseId.RESTORE_VIEW) {
ReaderBean r = null; //The Managed Bean
try {
FacesContext fctx = FacesContext.getCurrentInstance();
r=(ReaderBean) fctx.getExternalContext().getRequestMap().get("readerBean");
r=(ReaderBean) fctx.getELContext().getELResolver().getValue(fctx.getELContext(), null, "readerBean");
r=(ReaderBean) fctx.getApplication().getExpressionFactory().createValueExpression(fctx.getELContext(), "#{readerBean}", ReaderBean.class).getValue(fctx.getELContext());
r=(ReaderBean) fctx.getApplication().evaluateExpressionGet(fctx, "#{readerBean}", ReaderBean.class);
Nothing works!!!
As to the request map approach, it fails because a view scoped bean is not stored in the request scope. It's instead stored in the view scope. As to the other approaches, they fail because the view scoped bean is stored in the view and thus only available after the view has been restored. However, you're trying to get it before the view has been restored. This is a chicken-egg problem.
You need to revise your approach. Perhaps the bean needs to be a request scoped one? Perhaps the logic needs to be executed at a different phase? Perhaps the logic needs to be performed in the bean itself instead? Etc.
As the concrete functional requirement for which you thought that this is the right solution is unmentioned in the question, I can't point you in the right direction.
This question already has an answer here:
#SessionScoped bean looses scope and gets recreated all the time, fields become null
(1 answer)
Closed 6 years ago.
ello
I have 2 Managed beans, one View scoped, the other Session scoped. The View scoped bean is defined as
#ManagedBean
#ViewScoped
public class InvoiceController implements Serializable {
private static final long serialVersionUID = 1L;
#ManagedProperty(value="#{invoiceService}")
private InvoiceService invoiceService;
The session scoped bean as
#ManagedBean
#SessionScoped
public class InvoiceService implements Serializable{
I am using the session scoped bean to hold a flag used to decide if a panel should be rendered, when I run this through the debug I find that every time I call the method on the sesison bean, it is a new instance of the bean and therefore does not retain the value of my flag between requests.
What am I doing wrong?
That can happen if you have imported #SessionScoped from the javax.enterprise.context package instead of from the javax.faces.bean package. The former works on CDI #Named beans only, while the latter works on JSF #ManagedBean beans only.
A JSF #ManagedBean without any valid scope would default to #NoneScoped which means that it's newly constructed on every single EL expression referencing the bean, such as the #ManagedProperty. This explains the symptoms you're seeing.
I had a similar problem. I use a save-method in the view scoped bean that calls a method in the session scoped bean to update some values.
This is what I found out by debugging (excuse my non-Java-guru English):
When first loading the page, the instance number of the injected session bean was for example 111111.
But in the save-method (and all other methods called by an action like a commandButton or action listeners btw), suddenly the session bean was of another instance (say 222222).
Both instances 111111 and 222222 contained the very same values.
All methods I called now were done in the 222222 instance and it changed values in there as I wanted it. But the 111111 instance remained untouched and unchanged.
So 222222 was basically a deep(?) clone of 111111, and not even a copy.
But, after the save-method was done and the page got reloaded, the original 111111 instance was used again in the view scope bean.
The 222222 instance just got thrown to the garbage.
My solution for this problem:
I'm not using the ManagedProperty injection anymore.
Instead I made some helper code to get the session bean wherever I need it (aka in the view scoped bean methods):
public Object getSessionBean(String sessionBeanName)
{
return FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(FacesContext.getCurrentInstance().getELContext(), null, sessionBeanName);
}
For your example above, the call would be:
InvoiceService invoiceService = (InvoiceService) helper.getSessionBean("invoiceService");
Call it in your methods, do not store it as a field in the view scoped bean.
I hope this somehow helps you fix your problem.
If I am navigating from page one to page two, When I click on Page one's submit button, control goes to the managed bean written corresponding to page one, I want to fetch some result from database and show them to page two, If I set the Database values into the Managed bean corresponding to page two in the first Managed Bean's action method, then will I be able to get those on page two.
Please suggest if my approach is right?
You can inject let's say bean named Bean1 into Bean2 this way
#ManagedBean
#RequestScoped
public Bean2 {
#ManagedProperty(value="#{bean1}")//this is EL name of your bean
private Bean1 injectedBean;
public void setBean1(Bean1 value) {
injectedBean = value; //provide setter for it
}
#PostConstruct
private void init() {
List<YourData> list = injectedBean.getDBData(); //now you can fetch your list
}
Note that you have to provide setter for injection and scope of the injected bean should be always same or higher then the your beans scope (in this case Bean1 has to be at least RequestScope)
P.S. Also you could have some kind of DAO bean which could do all the database database operations for you (it can be EJB bean or even JSF Managed Bean) and then inject it to every ManagedBean which will require cooperation with your DB.