During the Struts days, there was a library called DwR (Direct Web Remoting) that was popularly used to access the Java classes directly from JavaScript. DwR provided a JavaScript wrapper for the server side Java classes, so the code will look as if you’re directly calling the Java code from JavaScript.
A similar concept in Ext JS 4 is the Ext.direct pack. It provides you a facility to invoke the Java classes directly from Ext JS 4 code. There are multiple implementations of Ext direct available. One such implementation is DirectJNgine. DirectJNgine provides the infrastructure to invoke the Java classes directly from Ext JS 4 API.
A server side Java class “WeatherReporter” that can be invoked from client is shown below. It is decorated with the annotations provided by DirectJNgine.
package com.durasoft; import com.softwarementors.extjs.djn.config.annotations.DirectAction; import com.softwarementors.extjs.djn.config.annotations.DirectMethod; @DirectAction(action="WeatherReporter") public class WeatherReporter { @DirectMethod public String getTemperature(String city) { return "Temperature of " + city + " is " + (Math.random()*100); } }
The Ext JS 4 code that is used to invoke WeatherReporter’s getTemperature method is shown below.
Ext.onReady(function(){ Ext.Direct.addProvider( DuraSoft.direct.REMOTING_API ); WeatherReporter.getTemperature(city,function(result){ alert(result); }); });
There’s a JavaScript class WeatherReporter generated by the DirectJNgine facility. This class sends an AJAX request to a DirectJNgineServlet that actually invokes our Java class. The web.xml file is provided with all these configuration details.
The complete project created in Eclipse is available at http://durasoftindia.com/downloads/ExtDirect.rar