ext3.0 ext direct

具体的工程步骤:
首先,我们来搭建WEB服务端的程序:
这时,我们需要下载一个JAR。名为extdirect4j-0[1].3.1-source.jar,目前是最新版吧。因为这个包用到GG一些办法,那么,我们还需要下载gson-1.3.jar。
接下来,这些写一个继承ExtDirectRouter的类,如下面:



package com;

import ch.swissdotnet.extdirect4j.AnnotatedAction;
import ch.swissdotnet.extdirect4j.ExtDirectRouter;
import ch.swissdotnet.extdirect4j.ExtRemoteAction;
import ch.swissdotnet.extdirect4j.ExtRemoteMethod;
import ch.swissdotnet.extdirect4j.NamedAction;

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");
}

@ExtRemoteMethod
public String sayHello(String value){
return "Hello, "+value;
}

@ExtRemoteMethod
public String getTree(String value){
System.out.println("被调到了");
return "[{"+
"'id':10,"+
"'leaf':false,"+
"'children':[{"+
"'id':11,"+
"'leaf':true,"+
"'children':null,"+
"'text':'S600'"+
"},{"+
"'id':12,"+
"'leaf':true,"+
"'children':null,"+
"'text':'SLK200'"+
"}],"+
"'text':'Benz'"+
"}]";

/*return "text:'Online',"+
"children:[{"+
"text:'Friends',"+
"expanded:true,"+
"children:[{"+
"text:'Jack',"+
"iconCls:'user',"+
"leaf:true"+
"}]"+
"}]";*/
}
}

@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;
}
}



测试:主要是测试我们自定义的类。因为ExtDirectRouter是一个继承了HttpServlet,那么MyExtDirectRouter 也算是一个Servlet。那么我们找到WEB文件,找出它的映射地址:比如

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


我们用浏览器打开该Servlet看网页显示什么。如果是

Ext.app.MY_REMOTING_API = {"url":"remote/","type":"remoting","actions":{"Action":[{"name":"getTree","len":1},{"name":"multy","len":2},{"name":"multy","len":1},{"name":"multy2","len":2},{"name":"sayHello","len":1},{"name":"multy1","len":2}]}};

恭喜你,你已经成功啦
结尾:服务端到此为止。
补充服务端说明:在提交数据到客户端的时候,有可能遇到中文乱码问题,不怕,我们的MyExtDirectRouter是一个Servlet,在它的父类(ExtDirectRouter)里面,有doPost、doGet的办法,主要在这两个办法里面添加

response.setContentType("text/json; charset=utf-8");


[img]/upload/attachment/124061/d493463b-bc95-38de-8592-1dd7e53acd76.bmp[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值