PDA

View Full Version : [3.0] Ext.Direct with Java Servlet



piziwate
16 Apr 2009, 1:20 AM
Hello,

ExtDirect4j is a LGPL Java library to implement Ext.Direct for Java Servlet.

Here is the project url : http://code.google.com/p/extdirect4java

Current release : 0.3.2

Help, comments and suggestions are welcome !

This is a small sample :



import ch.swissdotnet.extdirect4j.*;
public class MyExtDirectRouter extends ExtDirectRouter {
@ExtRemoteAction(name = "Action", lazyInit = true)
static public class TestAction {
@ExtRemoteMethod
public Result multy(long a) {
return new Result(a * 2, "just a result");
}
}
@Override
protected void configureActions() {
this.addAction(new AnnotatedAction(TestAction.class));
}
}


a HTTP Get on the servlet will produce :

Ext.app.REMOTING_API = {"url":"http://localhost:8080/ExtDirectDemo/MyExtDirectRouter","type":"remoting","actions":{"Action":[{"name":"multy","len":1}]}};

and a HTTP Post like
{"action":"Action","method":"multy","data":["3"],"type":"rpc","tid":2}

will answer with :


{"result":{"result":6,"msg":"just a result"},"action":"Action","method":"multy","type":"rpc","tid":2}

Best Regards

Piziwate

Animal
8 May 2009, 10:55 PM
How about a JSP tag which can output the



Ext.app.REMOTING_API = {"url":"/router","type":"remoting","actions":{"TestAction":[{"name":"multiply","len":1,"formHandler":false}]}};


into your page?


like:



<extdirect:api ns="Ext.app" class="TestAction,OtherClass,YetAnotherClass..."/>


or



<extdirect:api ns="Ext.app" jar="MyRemoteMethodsJar.jar"/>


And it analyses the class TestAction (and any others specified or found) using reflection, and class annotations:



public class TestAction{
public TestAction() {
}

@ExtRemoteMethod
public int multiply(int n) {
return n * 4;
}

// This doesn't get exported
public void otherMethod {
}
}

christocracy
10 May 2009, 9:12 AM
import ch.swissdotnet.extdirect4j.*;

@piziwate You're from Switzerland, I assume? I happen to be in Zurich for the next week. I've worked with Ext.Direct a fair bit myself. Send me a PM with your email.

ealpha
11 May 2009, 3:29 AM
where‘s the doc

how to run it in tomcat?

chalu
13 May 2009, 12:12 PM
Sorry if I sound late on this but first of all, I am just beginning to get my feet wet with DWR and now we've got Ext.Driect, assuming all I care about is Java on the server side, with DWR do I still need Ext.Direct ??

Ext.Direct is great news all the same

stefanorg
16 May 2009, 1:51 AM
any news about this library?!? The development seems to be stopped ...

steverc
18 May 2009, 7:08 AM
I am eagerly seeking updates and a full working example of Ext.Direct for Java. Any updates? (I looked at the Google project for Ext.Direct for Java but it lacks the HTML/JS front-end to drive the example. As a newbie I am needing a complete example to help get up to speed.

Thanks!

-Steve

maxm165
19 May 2009, 12:11 AM
Hi all!
Steve has added me in the project.
I'm working on it at the moment.

-Maxim-

maxm165
28 May 2009, 1:36 AM
Hi, guys!
I have just released a new 0.2 version of extdirect4j.
It is available on project page http://code.google.com/p/extdirect4java/downloads/list
This is simple example of how to use it:


public class MyExtDirectRouter extends ExtDirectRouter {

static public class Result {
private Long result;
private String msg;

public Result(Long result, String msg) {
this.result = result;
this.msg = msg;
}
}

static abstract public class AbstractAction {
abstract Long multy(long a, long b);

@ExtRemoteMethod(name = "multy1")
public Long multy(int a, long b) {
return -a * b;
}
}

@ExtRemoteAction(name = "Action", lazyInit = true)
static public class TestAction extends AbstractAction {
@ExtRemoteMethod
public Long multy(long a, long b) {
return a * b;
}

@ExtRemoteMethod(name = "multy2")
public Long multy(int a, long b) {
return a * b;
}

@ExtRemoteMethod
public Result multy(long a) {
return new Result(a * 2, "just a result");
}
}

@Override
protected void configureActions() {
this.addAction(new AnnotatedAction(TestAction.class)); //OK
//or so..
this.addAction(new AnnotatedAction("com.test.ExtdirectServlet$TestAction")); //OK
//or another else way in case we have not an access to action class in compile time
//but it is less scaled and has some restrictions
this.addAction(new NamedAction("com.test.ExtdirectServlet$TestAction")
.addMethod("multy", 1) // OK
.addMethod("multy", 2)); //error there is 2 methods called "multy" with 2 arguments;
}
}
Returned values and arguments can be any type.
As json serialization/deserializatiois mechanism based on gson liblary you can also extend as it is specified in gson documentation: http://sites.google.com/site/gson/gson-user-guide (http://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserializ)


@Override
protected void configureActions() {
this.getGsonBuilder().registerTypeAdapter(MyType.class, new JsonSerializer(){...});
...
}
Sorry about it is not enough information. It will appear later more...
Any questions/feature requests are wellcome!;)
Here those that are already known:

1.multiple transactions per request support(as it specified in Ext.Direct spec)
2.form handling support

Best Regards.
Maxim

maxm165
30 May 2009, 3:36 AM
New feature added:
1.multiple transactions per request support

http://code.google.com/p/extdirect4java/downloads/list

web.xml snippet:


<servlet>
<servlet-name>MyExtDirectRouter</servlet-name>
<servlet-class>ch.swissdotnet.extdirect4j.MyExtDirectRouter </servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>remotingApiVar</param-name>
<param-value>Ext.app.MY_REMOTING_API</param-value>
</init-param>
<init-param>
<param-name>remoteUrl</param-name>
<param-value>remote/</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>MyExtDirectRouter</servlet-name>
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>

maxm165
1 Jun 2009, 6:48 AM
New update 0.3.1 version

http://code.google.com/p/extdirect4java/downloads/list

1. null result bug fix:
calling of this method:


@ExtRemoteMethod
public void remoteMethod(arg...) {
...
}

will produce result json string like this:

{"result":null,"action":"Action","method":"remoteMethod","type":"rpc","tid":28}

2. feature: manually setting instance of remote object(this is extension of Ext.Direct spec)


@ExtRemoteAction
public class TestAction {
public TestAction(Integer n) {
this.n = n;
}

@ExtRemoteMethod
public Integer getN() {
return this.n;
}
...
}

...
this.addAction(new AnnotatedAction(new TestAction(new Integer(28))));

haattila
3 Jun 2009, 8:07 AM
Hi!

It is amazing library. But one thing is missing. Could You drop full functional example. It would help everyone to use it easyly.

Thanks!

Georgioa
25 Jun 2009, 11:52 PM
Hi !

Any news about this?

I'm using it to test ExtDirect with my tomcat server and it works fine.

I found a bug :

When POSTing if data is null, Gson raises an exception :

{"action":"TestDirect","method":"getCoucou","data":null,"type":"rpc","tid":3}Here is the code to fix it in Utils.java line 63 :



final JsonArray dataArray;
final int dataSize;

if(jsonObj.get("data").isJsonArray()) {
dataArray = jsonObj.get("data").getAsJsonArray();
dataSize = dataArray.size();
}else {
dataArray = new JsonArray();
dataSize = 0;
}
About "data":null, is it a bug from ExtDirect? In specs it is said that a HTTP POST must be like this :

{"action":"AlbumList","method":"getAll","data":[],"type":"rpc","tid":2}

maxm165
26 Jun 2009, 2:24 AM
Hi, haattila and Georgioa!
haattila, sorry about long time silence! I'll post most complete "how to use" manual soon.
Georgioa, thanks a lot for bug report, it will be fixed in next release. You comment is very useful!
Also, a form handling support as a new implemented feature expected. Something else?
Unfortunately I have been very occupied last weeks.
I'll prepare next release as soon as it possible.

Thanks.

Georgioa
26 Jun 2009, 2:44 AM
Hey !


We begun to implement formHandler support. (we are 2 devs)

We have sources with annotation support to create the REMOTE_API for provider. but we have'nt done anything about the request support for formHandler.

My src directory is linked below if you want to see we've done

jcalfee
23 Dec 2009, 2:38 PM
This is great, I'm running into a snag though. It seems to be missing the support for formHandler (I have searched the code). How do I include formHandler: true in my API? I need to create valid form handler methods so that I can use the Ext.Direct api submit, load methods.

Georgioa mentioned implementing doing some work on this. I would like to see what has been done so far.

Thank you,
Jimmy

maxm165
24 Dec 2009, 2:07 AM
Hi, Jimmy!
I stoped developing extdirect4j many months ago after more sophisticated directjngine (http://www.extjs.com/forum/showthread.php?t=73027) project had released. So, I strongly recommend to use directjngine (http://www.extjs.com/forum/showthread.php?t=73027) instead. There you can find formHandler supporting and many other features as you looking for.
Sorry if I disappointed you. :(

Maxim.

jcalfee
18 Jan 2010, 11:34 AM
Sophisticated is right. I'm asking them about Google AppEngine compatibility now. I don't think it is going to work though out of the box. It appears that they are writing the Api out to a file at runtime. If that were done at build-time we would be better. Access to the file system is not allowed in the AppEngine. Also, they are using threads. AppEngine will not allow you to create threads.

They even have Rhino in there, but I think that is only to support a yahoo JavaScript minimization library (they must have implemented some huge APIs to need this). I did not see any in-process JavaScript stuff that you would expect if you saw Rhino.

I like your approach of creating the API on request. I bet that is fast enough and could be cached if that really happened a lot and the API grew.

So, I brought this up and hopefully they will react to it. Is the form handler a lot of work? I'm a capable programmer. The PHP code in ExtJs did this with very little code. That is what I want to see. All with minimal dependencies.