-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Help!!
I have a project built using Spring + Hibernate + ZK.
I utilize JPA for persistence annotations and Spring annotations such as @Service @Repository @Controller @Autowire etc for creating my beans directly in the code for the classes rather than having it all wired up in XML.
But I can't find a way to access HTTP session in my Spring-MVC-ZK-controllers!! I have used data binding so that I have a ZK UI listbox (defined in a ZUL-page with a window) that is bound to properties in the controller (for retrieving all items, retrieving/storing current selected item asf).
I have managed fine until I was to bind a ZK ui listbox to a controller that extends the GenericForwardController. The listbox binds the attribute "selectedItem" to a propoerty in my controller class accessed through "getCurrentItem()" as well as "setCurrentItem(Item i)".
However the controller is wired up using Spring annotation @Controller, it has access to an application service using @Autowired, in short it is a Spring MVC controller. But it is also a ZK controller as it extends "GenericForwardController".
Something like
@Controller
public class MyController extends GenericForwardController {
@Autowired
private MyService service;
public Item getSelectedItem() {
HttpSession session = HELP ME TO GET HOLD OF THIS ONE in this Spring-annotated-wired-up-ZK-window-controller!!
return (Item) session.getAttribute("CURRENTLY_SELECTED_ITEM_IN_ZK_UI_LISTBOX");
}
....
}
Now, normally in the case of only using ZK and creating a controller within it, ZK would auto-assign (if I have understood things right) the HTTP session to the "session" field. But as I'm using Spring to wire the controller up, and use a "DelegatingVariableResolver" within my zul page to access the Spring-controller-bean that doesn't seem to be the case. The session field (inherited from GenericForwardController) is null.
I then tried autowiring a session-scoped bean in which I had a map to store/retrieve session objects such as the "selected item" (of the ZK listbox). That didn't work. No web request bound to current thread, it said. I added the RequestListenener, I configured the Transaction Syncronization Manager in ZK.XML and added a value for the threadlocal preference so that it would syncronize the RequestContextHolder (from which HTTP session attributes should be accessible).
Something like (zk.xml):
<listener>
<description>ThreadLocal Variables Synchronizer</description>
<listener-class>org.zkoss.zkplus.util.ThreadLocalListener</listener-class>
</listener>
<preference>
<name>ThreadLocal</name>
<value>
org.springframework.transaction.support.TransactionSynchronizationManager=resources,synchronizations,currentTransactionName,currentTransactionReadOnly,actualTransactionActive;
org.springframework.orm.hibernate3.SessionFactoryUtils=deferredCloseHolder;
org.springframework.transaction.interceptor.TransactionAspectSupport=transactionInfoHolder;
org.springframework.web.context.request.RequestContextHolder=requestAttributesHolder,inheritableRequestAttributesHolder;
</value>
</preference>
But nothing works! I can't get access to Http Session, nor Http session-scoped beans as Spring always complains there is no web request associated with thread, or in the case of using session scoped beans as a work-around to get access to http session, it still complains that is can't support "session scoped" beans..
Anyone with a clue on how to give Spring-ZK-controllers access to HttpSession!?
I would be so glad if anyone could help me clear this one out..Cuz I'm really stuck in my work because of this!!
Ideally you should get hold of ZK sessioin which is internally HttpSession.
To get ZK's session you can use the static methods that are available in org.zkoss.zk.ui.Sessions class.
Session session = Sessions.getCurrent(false); //This will give you current session object
session.getAttribute("name"); //This will retrieve the attribute value for the given name
If you really need the HttpSession object you can call session.getNativeSession().
For more info refer javadoc http://zkoss.org/javadoc/5.0/zk/org/zkoss/zk/ui/Sessions.html
Hi juwian,
the first step is your well configurated web.xml
Normally you need no explicite call of getting the HttpSession. That do spring self.
Checkout the svn sources of the Zksample2 project and have a look on all config-files. Page 15 in the documentation.
best
Stephan
Hi & thanks for your replies! Didn't solve the issue though I'm afraid.. =/
Session session = Sessions.getCurrent(false)
..results in the following error:
java.lang.NoSuchMethodError: org.zkoss.zk.ui.Sessions.getCurrent(Z)Lorg/zkoss/zk/ui/Session;
I then tried Sessions.getCurrent() without the boolean parameter which seems to work. The page loads and my getSelectedItem() method is called which makes use of
Session session = Session.getCurrent()
return (Item) session.getAttribute("SELECTED_ITEM")
However when clicking an item in the listbox it sometimes results in the timeout.zul showing up:
"The page or component you request is no longer available. This is normally caused by timeout, opening too many Web pages, or rebooting the server." (I guess that could be resolved by adding some configuration?)
The big issue though is that the listbox also makes use of
controller.setSelectedItem(Item item)
which in turn also makes use of the ZK session and then attempts to store the currently selected item as a session attribute and that fails.
public void setSelectedItem(Item item) {
Session session = Sessions.getCurrent();
session.setAttribute("SELECTED_ITEM", item)
}
which results in the somewhat strange error:
org.zkoss.zk.ui.impl.UiEngineImpl handleError:1130
java.lang.NoSuchMethodError: org.zkoss.zk.ui.Session.setAttribute(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;
I'm using ZK version 5 and I how no idea on why this method session.setAttribute(key, obj) doesn't exist. According to the documentation it should exist. According to Eclipse auto-suggestions it exists. But in runtime it obviously doesn't. Any ideas?
I will look into the Zksample2 project, cuz I would still like to get spring session scoped beans to work in my Spring-ZK app.. However the major issue right now is not being able to store anyting in the ZK session using setAttribute(..) which is strange.. I suspect ZK creates some kind of proxy for the ZK session object and it seems that proxy can't find the underlying proxied method.. Which in turn makes me suspect it hasn't really been hooked up to any underlying session.. Just a wild guess.. Or dare I ask if this could be an issue with the version 5 of ZK?
I'd really appreciate any help on this one, cuz right now I'm clueless.. Spring + ZK was so smooth until I ran into the need of storing things in HTTP session or something similiar, like in a ZK session..
I tried using session.getNativeSession() as Kesavkolla suggested and then use that HttpSession instead and that seems to work.. i.e:
Session zkSession = Sessions.getCurrent();
HttpSession nativeHttpSession = (HttpSession) zkSession.getNativeSession();
nativeHttpSession.setAttribute("SELECTED_ITEM", item);
and Item item = (Item) nativeHttpSession.getAttribute("SELECTED_ITEM");
But how come I can't use the ZK session's setAttribute("SELECTED_ITEM", item)? And then there's the issue with not being able to get session scoped spring-beans to work properly in my ZK-Spring controller..
Check all the zk jars that are in your classpath. I used Sessions object and works well for me. The error you pasted:
java.lang.NoSuchMethodError: org.zkoss.zk.ui.Session.setAttribute(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;
indicates you were trying to set an array object rather than string. The L indicates it's an array and ofcourse setAttribute doesn't take array.
I having same problem
session varible whatever i set in spring controller i am able to get in zul
<zscript><![CDATA[
List root = sessionScope.get("root");
]]></zscript>
but when i am trying to get in the composer class of this zul it is showing null
Session zkSession = Sessions.getCurrent(false);
HttpSession nativeHttpSession = (HttpSession) zkSession.getNativeSession();
List root=(List) nativeHttpSession.getAttribute("root");
System.out.println("session attributes===================="+root.get(0));
please help me to do it
Hi anilsikandra,
It is not recommend to use zscript, you can access session attribute in the controller.
SimpleSession s = (SimpleSession) Executions.getCurrent().getDesktop().getSession();
HttpSession session = (HttpSession) s.getNativeSession();
Try to get current session with this code in your event handler.
Look at this
Asked: 2010-01-22 09:09:29 +0800
Seen: 2,182 times
Last updated: Dec 30 '14