0

Call javascript from server within java codes

asked 2013-08-25 04:40:35 +0800

jasonhoo gravatar image jasonhoo
84 4

updated 2013-08-26 05:16:18 +0800

I want to implement digital signature method:
1,press button to call java logic in server which caculate the whole datas
2,call the Java script in client which get the digital signature
3,store the result into database in server.

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-08-26 02:44:29 +0800

cor3000 gravatar image cor3000
4332 1 7
ZK Team

for 1 you simply fire a command to the server

<button onClick="@command('calculate')" label="click me" />

and for 2 you implement a command handler in your ViewModel and call Clients.evalJavaScript()

@Command("calculate")
public void onCalculate() {
    //your calculations
    Clients.evalJavaScript("someScriptFunction();")
}

http://www.zkoss.org/javadoc/latest/zk/org/zkoss/zk/ui/util/Clients.html#evalJavaScript%28java.lang.String%29

and 3 fire an event back to the server using the ZK Client API

http://books.zkoss.org/wiki/SmallTalks/2010/April/ClientSideProgramming#Communicateto_Server

Here is also a running example... "button three" shows you all your 3 steps (be aware that the onClick code is executed at the server side)

This example will send 2 events to the server... an onClick event, and an onTest event, with a server roundtrip in the middle.

The data sent with the last event you can then save into the database, of your choice

link publish delete flag offensive edit
0

answered 2013-08-26 07:28:35 +0800

cor3000 gravatar image cor3000
4332 1 7
ZK Team

Of course it has no return value, this method will just add this JS to the response of the current execution. The JS will be rendered and executed at the client side.

I just adapted the example from above and added the remaining steps: see here

As you can see the result of the doClientSignature function is passed to the server in a separate command "storeToDB"

Also check the network tab in firebug or in chrome developer tools... you'll see that 2 ajax requests are made to the server. One with the onClick event, and the second with the custom onStore event. The server translates these events into the appropriate commands defined in the MVVM command bindings.

I hope that clarifies your questions.

If not, please let me know.

Robert

link publish delete flag offensive edit

Comments

I want to do these three steps in the synchronous method, like: do1();do2();do3();. I try it again with your suggestion.

jasonhoo ( 2013-08-26 08:15:40 +0800 )edit

then why not just execute "do2();" completely on the server? Is there a specific reason to do the signature in JS on the client side?

cor3000 ( 2013-08-26 08:41:26 +0800 )edit

Signature is only a sample which can understand easyly. In fact, I call the ActiveX by JavaScript and get the result and process it on server right now, so I try to find the synchronous method.

jasonhoo ( 2013-08-26 10:14:20 +0800 )edit

as far as I know ZK has no server side synchronous JS call mechanism to retrieve the result directly. ZK uses Events to communicate with the client, so you always have to listen on an event and use the data attributs for payloads containing the responses.

cor3000 ( 2013-08-26 10:56:54 +0800 )edit
Your answer
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
1 follower

RSS

Stats

Asked: 2013-08-25 04:40:35 +0800

Seen: 140 times

Last updated: Aug 26 '13

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