`
牧羊人
  • 浏览: 210766 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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



  • 大小: 51.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics