-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi, I'm trying to get the current url using groovy. The code is:
String script = """
function getCurrentUrl(){
return window.location.href;
}
"""
def engine = new ScriptEngineManager().getEngineByName('javascript')
engine.eval(script)
Invocable inv = (Invocable) engine
def response = inv.invokeFunction("getCurrentUrl")
But I get
org.zkoss.zk.ui.UiException: javax.script.ScriptException: ReferenceError: "window" is not defined in <eval> at line number 3
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.zkoss.lang.Classes.newInstance(Classes.java:74)
at org.zkoss.lang.Exceptions.wrap(Exceptions.java:163)
at org.zkoss.zk.ui.UiException$Aide.wrap(UiException.java:51)
at org.zkoss.bind.impl.ParamCall.call(ParamCall.java:157)
at org.zkoss.bind.impl.BinderImpl.doExecute(BinderImpl.java:2035)
at org.zkoss.bind.impl.BinderImpl.doCommand(BinderImpl.java:1748)
at org.zkoss.bind.impl.BinderImpl.access$1300(BinderImpl.java:131)
Any idea?
What you are doing looks like you are executing a JS function at server side which will run in a JS engine inside your JVM. Even though it's JS it is not running in the browser - and that's why the window
variable is not defined.
Assuming you want the URL in the browser then you have to send a script back to the browser to evaluate, and then fire an event back to the server in order to get the result.
To me this only sounds necessary when the client side URL changes during the lifetime of your ZK Desktop or if it is changed transparently by a proxy in the middle and the real browser URL is needed.
If it doesn't change and you are only interested in the requestPath and queryString the straight forward way is to call (in Java - I don't know the corresponding groovy out of my head)
Executions.getCurrent().getDesktop().getRequestPath();
Executions.getCurrent().getDesktop().getQueryString();
I can elaborate more on the first option if you think the information stored in the desktop object is not sufficient for your needs.
Robert
Asked: 2017-11-10 22:31:41 +0800
Seen: 10 times
Last updated: Nov 13 '17
How to Integrate Groovy In ZK Web application? [closed]
Facing issue in masking on phone [closed]
coerceFromString_ override no longer works from 6.5.1 [closed]
Execute Javascript on component visible
Is there a way to resolve view model properties as input to client side javascripts?
scrollbar listner for 2 grid [closed]