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
re: Trolling with Java Web Frameworks
People knowing me already know about my opinion about JSP and JSF. JSP is "OK" if you're designing UI with Java developers :'( However, a good separation of Concerns can be achieved only through a good separation of Roles, beautifully explained in Dr.Parr's paper Enforcing Strict Model-View Separation in Template Engines.
And I totally agree that having full access to the Turing-complete power, inherent in JSP (and a bit in Velocity) actually gets in our way.
On a funny note: Trolling with Java Web Frameworks
The blog entry above is so funny, especially the JSF part, a must read :)
Have a nice week-end!
-florin
PS
Oh yeah, and you will be able to execute JPublish Actions from DWR AJAX client calls :) slightly easier than doing the same thing from Webwork. Check the JPublish code repository sometimes this week-end.
And I totally agree that having full access to the Turing-complete power, inherent in JSP (and a bit in Velocity) actually gets in our way.
On a funny note: Trolling with Java Web Frameworks
The blog entry above is so funny, especially the JSF part, a must read :)
Have a nice week-end!
-florin
PS
Oh yeah, and you will be able to execute JPublish Actions from DWR AJAX client calls :) slightly easier than doing the same thing from Webwork. Check the JPublish code repository sometimes this week-end.
Cayenne ORM support for JPublish
We finally did it :) JPublish applications can now use a very nice ORM support through the beautiful Cayenne framework. I have to admit that initially I started to implement the Hibernate support but then I became more and more attracted by the vibrant community of Cayenne.
The implementation is very simple, a JPublish module and a lot of help from Cayenne's user list and Click's author generous support. Thank you all!
To enable Cayenne in a JPublish application you will have to configure the JPCayenneModule in your JPublish application. This is very simple. Edit the jpublish.xml file and add:
<!--JPublish Cayenne support -->(please verify that you have the JPCayenne.jar in your WEB-INF/lib folder. You can build it from our SVN or simply get it from our demo lib folder)
<module classname="org.jpublish.module.cayenne.JPCayenneModule">
<cayenne-config-path>/WEB-INF/cayenne</cayenne-config-path>
<auto-rollback>true</auto-rollback>
<session-scope>false</session-scope>
<shared-cache>true</shared-cache>
<!--
~ Http request paths using a per-request or a per-session Cayenne ObjectContext (OC),
~ the read-only paths will be interpreted first and will use a global OC one defined
~ per web app instance.
~ -->
<cayenne-enabled-urls>
<url path="/info/*" readonly="true"/>
<url path="/status/*" readonly="true"/>
<url path="/rss/*" readonly="true"/>
<url path="/users/*" readonly="false"/>
<url path="/companies/*"/> <!-- readonly="false" by default, if not defined -->
</cayenne-enabled-urls>
<debug>true</debug>
</module>
In the configuration above we demonstrate how one can enable ORM support for certain URLs. For example, any request for /rss/* will use a read-only Cayenne DataContext, while the requests coming for /companies/* will use a per-thread DataContext. The Cayenne's DataContext life-cycle is handled in a before/after advice style, so you don't have to worry about the internals :)
The Cayenne support is available in the JPublish context, Actions and Pages (Velocity, Freemarker or StringTemplate) being able to access the Cayenne service (JPCayenneModule.JPCAYENNE_SERVICE_NAME).
So for example in our simple demo TODOs app, any Beanshell action can access the Cayenne service like this (excerpt from actions/GetUsers.bsh):
jpCayenneService = context.get( JPCayenneModule.JPCAYENNE_SERVICE_NAME);
if( jpCayenneService!= null){
Expression qual = ExpressionFactory.likeIgnoreCaseExp("name", "%");
SelectQuery select = new SelectQuery(User.class, qual);
users = jpCayenneService.performQuery( select);
if( users != null)
context.put( "users", users);
}
You can find a very simple demo in our download section: jpcayenne.war, basically a simple TODO web application having the following model:
This demo is just scratching the Cayenne framework and I did it in less than three hours, so please be indulgent :) For learning Cayenne I kindly recommend you to visit Cayenne's web site for updated documentation and more examples.
I am now going to update our JPublish Wiki with more details about this implementation, so ... stay tuned and send us your feedback.
Give it a try and let us know how it goes.
Cheers,
