In my application the objects put into sessionMap doesn't stored. I can access it in the same object where I have done the put operation, but when I want to do it from another bean, the object doesn't exist. I put the objects into the session in the following way:
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put( "myKey", myObject );
This is the first time that happens. In another apps I can access the objects stored in the sessionMap.
Best Regards
DrakoRod
Thanks for your help, finally I resolved the problem, really a don't saw exactly what's was. I explain what I did.
First. My project was in a normal template with this frameworks: JSF 2, primefaces, Spring, Hibernate and log4j. So, all was going well. I added a new template to project and happened the problem with session.
But I just copy/paste code in menu, so I decided write tag by menu's tag and vuala! everything worked.
How I say, I don't saw what's was the problem, but I solved it.
Related
I got the following issue on the specific layer we have on Alfresco (version 3, using hibernate 3 + spring):
When a page is first loaded, everything goes fine. No problem.
When we do an interaction on this page that leads to a refresh, we have some
lazyinitializationexception could not initialize proxy - no session.
I know the lazyinit is a common issue. But because of the fact that we want only make changes on the specific layer not on the alfresco layer, many resolutions can't be applied.
The one consisting in forcing the sessions to stay open is not possible for us.
The one using Hibernate.Initialize leads to this error:
User:admin ERROR [org.hibernate.LazyInitializationException] failed to lazily initialize a collection of role: org.jbpm.taskmgmt.exe.TaskInstance.variableInstances, no session or session was closed
So I got three questions:
Does someone has an idea of why on the first load, we have no problem and on refresh we got the lazyInit Exception ?
How could we make the hibernate.initialize work ?
Is there any other way to solve this issue ?
To say a little more about the context:
We want to show to user via IHM the task history. To do that we fill a table, to do so we collect the current taskInstance ti and the previous ones and for each of them we do a ti.GetName(), ti.GetDescription(), etc. The issue seems to come that after collecting the Task Instances, the hibernate session is close, so we can't perform ti.GetName() and other operations on ti.
In advance thank you all for your help,
Regards
Okay, after a lot of days of working on that, we just found how to solve it. A colleague just solve it when I was writing the desperate post :)
So the solution was to use
getHibernateTemplate().Hibernate.Initialize(ti.GetName());
directly in the Callback part of the DAO class.
Hope it will be useful to others and thanks for your efforts to help and I'm always unsure on whether I should post code or not to keep the post simple.
I just started learning with JSF and I'm wondering about this issue.
I have 16 Checkboxes. When I open the Page and my Brother is opening it too it seems like we have the same session. When I click one of the checkboxes it is checked on my brothers browser too.
The selected checkboxes are saved in a private ArrayList<String> selectedBoes = new ArrayList<String>();
My ManagedBean ist SessionScoped.
Why is this happening and how to avoid it?
My brothers session ID is cdbbb126e96060ddb924b9d5e591. Mine is cd9a5c4180bd9dfef0ec8db2ac4d
Then the only possible cause is that you're actually not storing them in the session scope. The symptoms indicate that you're either storing them in the application scope, or even that you declared it static (and thus the code in the question does not represent the real code at all; the code in your comments indicate that also less or more, you used a different property name, apparently you attempted to type it from top of head, you should not do that, you should copypaste the real code).
Note that when you're using CDI's #Named instead of JSF's #ManagedBean, then it would default to the application scope, unless otherwise specified with a javax.enterprise.context scope annotation. So if you're using JSF's own javax.faces.bean.SessionScoped annotation, which is not supported by CDI, then it would still be in the application scope.
In our Java web application which uses Struts 1, we have a lot of code that essentially does this:
HttpSession session = httpServletRequest.getSession();
MyObject myObject = session.getAttribute(MY_OBJECT_KEY);
//code that mutates myObject - setting properties or whatever
session.setAttribute(MY_OBJECT_KEY, myObject);
My question: Is the last line, session.setAttribute(..) necessary? It seems pointless to me - 'myObject' and 'session.getAttribute(..)' refer to the same location in memory, right? So re-setting the attribute in the session should not be required? Does this do anything I am not aware of? The object does not implement HttpSessionBindingListener which is mentioned in the documentation.
I feel like I need to double check because this is done all over this app and I certainly don't want to break anything just because I am cleaning up code. Thanks
It depends what you mean by "mutate". If "mutate" in this context means you changed properties, but not the instance, then no, you don't need to setAttribute at the end.
myObject is a reference to the underlying object. The session has its own reference. They both point to the same underlying object, so you'd be changing properties on the same instance.
Now, if you do
myObject = new MYObject();
THEN you need to pass the new object to the session. In this case, your reference myObject now points to a different instance than the session's reference.
Note, why not write a unit test and test it out?
#emulcahy,
I know this is an old post. I just stumbled on this while researching on Servlet listeners. Did you find out the reason for those "redundant" setAttribute(s)?
I am wondering if they were added to trigger events in Servlet listeners. You may want to check if there are any object(s) that implement HttpSessionAttributeListener or HttpSessionBindingListener. They may be used to log information in those session variables. Just a thought. If you already solved your problem, please ignore this message.
I'm wondering should i use Singleton for my presenters?
What is the benefit of it?
I have a weird behavior in my simple project, i have a form which is used to add new records in db and display in a table, so after the first add "click" it works ok, but in the next second or third "click" then it comes weird. For example on the second click the event is called twice and the input is inserted twice in db, if you do third click the input will be inserted three time in db, why this happen?
Thanks
Edit
So far my presenters and views are Singleton , also EventBus and PlaceManager.
Implementing presenters as a singletons can reduce testability of your code.
Consider using a Dependency Injection. Gin works perfectly with GWT apps.
totaly difficult to answer that without seeing the code, maybe you can post the methods which are called after pressing the button?
I want to do something like this: index.xhtml -> createPerson.xhtml -> addAddress.xhtml -> index.xhtml.
In Managed-Bean CreatePerson a new Person-Object will be created, filled and saved, in Managed-Bean AddAddress I need the Person-Object in order to add an address to this person and after this, it should navigate back to the starting point. I think, the Managed-Bean CreatePerson should be #SessionScoped, because I have to inject the Person-Object into AddAdress (here #ViewScoped).
I want to have a loop or rather the possibility to create more than one person, but how can I do this if I have a SessionScoped-Bean, that lives longer than I need it? Is there any command or annotation to destroy it? How do you handle such workflows related to the scopes?
I know about MyFaces Orchestra with conversation-scope, but I will, if possible, do it only with Eclipse/Tomcat (7.0.11)/Mojarra (2.0.3).
Use a single view and make use of the rendered attribute to show/hide included views with "subforms" depending on each other. This way you can go with a #ViewScoped bean.
Alternatively, just save the Person in the DB after create and pass its id as request parameter to the next view and let it load from the DB again.
If you only want (or can) use Tomcat and Mojarra then both solutions mentioned by BalusC work perfectly, although with the limitations that you have to stay on a single page or redo queries between page navigation. Your use case is exactly what the conversation scope in Java EE 6 (through CDI) is made for. You can add this too to Tomcat via the Weld reference implementation.
When using the method BalusC outlined for staying on a single page, to give the user a tiny bit the impression of dealing with separate pages, it might be an option to display the rendered parts of the page using 'dialogs' (floating, css, ...). If using a third party component library is an option for you, you could add RichFaces or PrimeFaces that both contain ready to use dialog components.