0

Spring + ZK + JPA renders problems in how to access HTTP session in controllers! Anyone? Help!!

asked 2010-01-22 09:09:29 +0800

juwian gravatar image juwian
96 1 3

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!!

delete flag offensive retag edit

9 Replies

Sort by ยป oldest newest

answered 2010-01-22 09:27:42 +0800

kesavkolla gravatar image kesavkolla
180 3

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

link publish delete flag offensive edit

answered 2010-01-22 10:08:06 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

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

link publish delete flag offensive edit

answered 2010-01-23 11:17:39 +0800

juwian gravatar image juwian
96 1 3

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..

link publish delete flag offensive edit

answered 2010-01-23 11:24:49 +0800

juwian gravatar image juwian
96 1 3

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..

link publish delete flag offensive edit

answered 2010-01-24 10:57:02 +0800

kesavkolla gravatar image kesavkolla
180 3

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.

link publish delete flag offensive edit

answered 2012-06-09 10:52:45 +0800

anilsikandra gravatar image anilsikandra
21 1

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

link publish delete flag offensive edit

answered 2012-08-09 10:39:37 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

Hi anilsikandra,

It is not recommend to use zscript, you can access session attribute in the controller.

link publish delete flag offensive edit

answered 2014-12-30 10:17:13 +0800

harunalfat gravatar image harunalfat
1
SimpleSession s = (SimpleSession) Executions.getCurrent().getDesktop().getSession();
HttpSession session = (HttpSession) s.getNativeSession();

Try to get current session with this code in your event handler.

link publish delete flag offensive edit

answered 2014-12-30 13:40:10 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...

Look at this

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-01-22 09:09:29 +0800

Seen: 2,182 times

Last updated: Dec 30 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More