简体   繁体   中英

Same SessionBeans with different states

I have an issue that makes me think that i'm doing something in the wrong way. I have a sessionBean with an entity called CashRegister that holds the cash register. When this property is null , then it means that no cash register has been created.

I have an action in a viewScope bean that in the method GoToOrdersQueue checks if the cash register (obtained from the session bean) is not null and then redirect to certain page... if the cash register is null , it shows a message saying that the cash register must be created first.

In the other hand, I have a customFilter (implementing javax.servlet.Filter ) thats checks if the cash register of the session bean is not null , allowing to continue to the redirected page, and if the cash register is null , then redirects to a page that says: "Cannot see the page because the Cash register must be created first"

Know, my problem is that I create the cash register, then I call the method GoToOrdersQueue of the viewScoped bean and when it checks if the cash register is not null , the result is correct...not null so it redirect to the desired page, but .. when the filter is invoked to check if the cash register is not null , the result is incorrect ! because the session bean has the property cash register with null !

Is it the way I invoke the session bean the problem ?

In the method GoToOrdersQueue of the viewScoped bean i invoke the sessionbean like this:

ControllerSession sessionbean = JsfUtil.findBean("controllerSession");

where the method findBean is declared like this:

public static <T> T findBean(String beanName) {
    FacesContext context = FacesContext.getCurrentInstance();
    return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class);
}

And in the filter I invoke the sessionbean like this:

HttpServletRequest req = (HttpServletRequest) request;
HttpSession session = req.getSession(false);
ControllerSession sessionbean = (ControllerSession) session.getAttribute("controllerSession");

Why they have differents states (in that particular property) if they are suppose to be the same object ? I use other properties like the logued user and works fine everytime.

thanks in advance for any tip !

by the way i'm using tomcat 7.0.40, jsf 2.2 and primefaces 3.5, but i think has nothing to do with this.

I found the problem... In the ViewScoped bean, I was getting the instance of the sessionbean at the constructor, like this:

public ControllerViewScoped(){
    sessionbean = JsfUtil.findBean("controllerSession");
}

Well, don't know why but if I use that code in every method that use the sesisoncontroller, it works...

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM