Executing JPublish Actions from DWR? What else can be simpler? :)
I was always envying the ability of Webwork to execute Actions via DWR and it was clear that any JPublish user will need something similar. However, I wanted a simpler implementation.
As promised, now you have it:) If you'll update your JPublish framework from SVN, including the DWR module, you'll see what I am talking about . Yup, you can execute any JPublish action (BSH, JS, JRUBY, GROOVY or Java compiled code) making remote calls via DWR. In the SVN there is also a demo showing you how to do this: SimpleDWR
In the small explanation below I presume you're already familiar with how JPublish and DWR work together.
This is what you need to remotely execute a JPublish Action:
- first modify the dwr.xml file and define the Action you want to publish through the DWR.
<init>
<creator id="jpublish" class="ca.flop.jpublish.dwr.JPublishCreator"/>
</init>
...
<create creator="jpublish" javascript="InfoAction" scope="application">
<param name="class" value="ca.flop.jpublish.dwr.DWRJPublishActionManager"/>
<param name="actionName" value="InfoAction.bsh"/>
</create>
- then define the InfoAction.bsh. Here is the code from our SVN example:
String infoMsg = "Hi, this is JPublish 4";
context.put("infoMsg", infoMsg);
context.put("serverDate", new Date());
print( infoMsg);
- now let's call the InfoAction in an AJAXified HTML page and display the infoMsg and the serverDate values returned by the InfoAction script, a server side BSH script executed by JPublish. Would that be hard? What do you think? Here is the code:
..
<script type='text/javascript' src='$request.ContextPath/dwr/interface/InfoAction.js'/>
...
Below is a simple demonstration of the ability to invoke JPublish Actions via DWR:<br/>
<input type="button" value="Info" onclick='InfoAction.execute( params, function(response) {
dwr.util.setValue("serverDate", response.serverDate);
dwr.util.setValue("info", response.infoMsg);
});'/><br/>
Messages received from server:<br/>
<div class="dateField" id="serverDate">-server date-</div>
<div class="salute" id="info">JPublish Salute...</div>
Guess what? Uhhh..... that's all :)
Have fun!
-florin
