JSON-RPC for Java使用说明.doc_第1页
JSON-RPC for Java使用说明.doc_第2页
JSON-RPC for Java使用说明.doc_第3页
JSON-RPC for Java使用说明.doc_第4页
JSON-RPC for Java使用说明.doc_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JSON-RPC for Java使用说明2008/8/6JavaScript高级应用与实践的延伸 | 夏天忍JSON-RPC for Java使用说明轻量级、零入侵、级联调用JSON-RPC for JAVA AJAX框架 QQ: 11602011夏天2008JSON-RPC for Java使用说明JSON-RPC for Java使用说明目 录概述4链接4作者相关链接4开源项目地址4工程svn下载地址4示例工程下载地址4支持的浏览器5Java对象到JavaScript对象的对照表5功能介绍6自动捕获异常6JavaScript中释放注册的Java服务对象6级联调用功能7使用7Web.xml配置7引入Jar包7AJAX服务Java类的编写8自己基类的编写9AJAX服务Java类的注册10自己注册基类的编写10JSP中的使用12引入JsonRpcClient.js12调用12英语版本13概述继JavaScript高级应用与实践之后推出的json-rpc-for-java开源代码,是仅仅100行的javascript代码和不到10个java文件实现的超级轻量级的通过 javaScript快速调用java对象并返回任意对象的轻量级框架,并且支持级联调用,也就是说不需要额外 的JavaScript编程,就可以通过javascript调用被注册的java对象并返回java对象,如果被返回的对象还有方法,这个在javascript中返回的java对象的变量,还可以继续调用它的方法.这就是这个轻量级json-rpc-for-java的神奇之处。链接作者相关链接作者csdn博客 作者新浪600多万次点击博客 作者网站 开源项目地址/p/json-rpc-for-java/工程svn下载地址/svn/trunk/不需要用户名和密码。示例工程下载地址/files/JsonRpcExample2008-08-05.rar测试环境:MyEclipse、JRE1.4(或1.6)、tomcat 5.0(或6.0) 如果你要测试,可以采用相应的环境,不一定要那么高版本的环境 ,Import工程后请注意修改工程中JRE为正确的路径:支持的浏览器IE4、IE5、IE6、IE7、IE8、 FireFox、Opera、Safari等等。Java对象到JavaScript对象的对照表调用异常时返回false。Java对象JavaScript对象说明java.lang.StringStringjava.lang.ObjectString调用java对象的toString()后转换到JavaScript里java.util.Date、java.sql.TimestampString格式为yyyy-MM-dd HH: mm:ss.000,如果时分秒都为0,则为:yyyy-MM-ddjava.lang.BooleanBlooean对应的值:true、falsejava.lang.CharacterString 单引号的字符串,例如:cjava.lang.Short、java.lang.Integer、java.lang.Long、java.lang.Float、java.lang.Double、java.math.BigDecimalNumber到JavaScript中都为数字对象,可以直接参与加、减、乘、除运算java.util.MapObject例如:obj“key1”、obj“key3”、obj.key3,唯独没有以function作为属性的方法,当然,属于Ototype的function属性依然有的java.util.ListArray例如:a0、a2.getList()也就是说List里也可以存在复合对象,这些对象依然可以有自己的方法nullnull空对象其他Java对象Object例如:obj.displayName()、obj.aac001,可以有属性和方法功能介绍自动捕获异常在你编写的java服务类的方法中不需要try.catch(Exception e),本框架会为你捕获异常错误消息,当你在javascript中没有获取到正确的数据,可以调用异步对象的方法getErrMsg()获取异常消息,该方法封装在mon.JsonRpcObject中,也就是AJAX服务java基类中。JavaScript中释放注册的Java服务对象你只需要在JavaScript中调用release()就可以释放注册的Java对象资源,详细见示例工程,或者见:/p/json-rpc-for-java/wiki/Wiki32级联调用功能不明白的地方请结合示例工程进行理解。1、Java中注册复合对象myjsonrpc2、JSP JavaScript中获取该对象:var myjsonrpc = JsonRpcClient().myjsonrpc;3、调用被注册的java对象的方法getMyObj,返回复合的java对象TestDomain:var oDomain = myjsonrpc. getMyObj();/ 继续调用该返回的java对象的方法alert(oDomain. toXml()或者:alert(myjsonrpc. getList()1.toXml();如果toXml返回的还是一个复合的Java对象,你可以继续在JavaScript中继续调用,而不需要额外的编程。使用Web.xml配置需要在web.xml中加入下面的配置JSONRPCServletjcore.jsonrpc.servlet.JSONRPCServlet2JSONRPCServlet/JRPC引入Jar包需要在工程中引入:JSON-RPC.jar、commons-logging.jar、commons-logging-api.jar,其中后面两个jar在示例工程中的JsonRpcExamplewebappWEB-INFlib 下。示例工程下载地址:/files/JsonRpcExample2008-08-05.rar而,JSON-RPC.jar,你也可以引入源代码重新进行打包。AJAX服务Java类的编写必须继承与mon.JsonRpcObject,并实现接口java.io.Serializable。例如示例工程中的AJAX服务Java类:package test;import java.io.Serializable;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import mon.JsonRpcObject;public class TestObject extends JsonRpcObject implements Serializableprivate static final long serialVersionUID = 1L;private List myList = new ArrayList();private Map map = new HashMap();public TestObject()myList.add(good);myList.add(new TestDomain();/ map中也可以放入复合对象map.put(first, 第一条值);map.put(p2, new Date();map.put(domain, myList.get(1);/* * 返回Map对象 * return */public Map getMap()return map;/* * 获取一个普通对象 * return */public Object getStr()return myList.get(0);/* * 获取一个复合对象 * return */public Object getMyObj()return myList.get(1);/* * 获取List对象 * return */public List getList()return myList;自己基类的编写同样,你可以继承mon.JsonRpcObject实现一些基类,这样在自己的项目中更加方便实用,例如:package mon;import com.yinhai.webframework.session.UserSession;import mon.JsonRpcObject;public abstract class Yhsi2JsonRpcObj extends JsonRpcObject private UserSession us = null; public Yhsi2JsonRpcObj() super();public UserSession getUs() if(null = us)us = UserSession.getUserSession(getRequest();return us;AJAX服务Java类的注册/ 注意,被注册的类必须是能被实例化的类mon.JsonRpcRegister.registerObject(us, myjsonrpc, test.TestObject.class);使用test.TestObject.class的方式是保证多次注册不至于test.TestObject被多次注册而执行多次实例化,从而提高性能,并允许多次注册实际上内部只注册了一次。自己注册基类的编写当然,你也可以继承mon.JsonRpcRegister以便使得在应用菜单切换的时候释放资源,例如:package mon;import javax.servlet.http.HttpServletRequest;import mon.Content;import mon.JSONRPCBridge;import com.yinhai.webframework.session.UserSession;import mon.JsonRpcRegister;/* * 注册JsonRpc对象 * author just * */public class JsonRpcRegister extends mon.JsonRpcRegister/* * 通过request来注册对象 * param request * param szKeyName * param o */public static void registerObject(HttpServletRequest request, String szKeyName, Object o)registerObject(UserSession.getUserSession(request), szKeyName, o);/* * 通过request来注册对象 * param request * param szKeyName * param o */public static void registerObject(UserSession us, String szKeyName, Object o)if(null != us) JSONRPCBridge brg = (JSONRPCBridge)us.getCurrentBusiness().getSessionResource(Content.RegSessionJSONRPCName); / 如果是第一次就注册对象 if(null = brg) us.getCurrentBusiness().putSessionResource(Content.RegSessionJSONRPCName, brg = new JSONRPCBridge().setSession(us.getHttpSession(); brg.registerObject(szKeyName, o); /* * 通过request来注册对象 * param request * param szKeyName * param o */public static void registerObject(HttpServletRequest request, String szKeyName, Class o)registerObject(UserSession.getUserSession(request), szKeyName, o);/* * 通过request来注册对象 * param request * param szKeyName * param o */public static void registerObject(UserSession us, String szKeyName, Class o)if(null != us) JSONRPCBridge brg = (JSONRPCBridge)us.getCurrentBusiness().getSessionResource(Content.RegSessionJSONRPCName); / 如果是第一次就注册对象 if(null = brg) us.getCurrentBusiness().putSessionResource(Content.RegSessionJSONRPCName, brg = new JSONRPCBridge().setSession(us.getHttpSession(); try brg.registerObject(szKeyName, o.newInstance(); catch (InstantiationException e) catch (IllegalAccessException e) JSP中的使用引入JsonRpcClient.js调用!-/ myjsonrpc就是通过JsonRpcRegister.registerObject注册的名字/ 这时候这里的rpc就拥有了通过JsonRpcRegister.registerObject注册的/ 异步对象的相应方法了var rpc = JsonRpcClient().myjsonrpc;/ 传入个人编号获取人的基本信息并填充到界面上if(dto(aac001) = & 0 = aab001.getValue().length) if(0 o.value.length) / 获取到的myjsonrpc同样有aac001,aab001等等属性,/ 你可以直接使用,同样有getAac001()等方法,/ 可以直接使用,而不需要额外的编码 var myjsonrpc = rpc.getEmployeeBaseInfo(o.value), errMsg = rpc.getErrMsg(); if(0 英语版本Contents 4 Summary Links 4 Author Links 4 Open-source projects address 4 Download works svn address 4 Examples of projects download address 4 Supported browser 5 Java objects to the JavaScript object tables 5 Features 6 Automatic caught 6 JavaScript registered in the release of the Java clients 6 Cascade called functional 7 Use 7 Web.xml configuration 7 7 package introduced Jar AJAX Java services such as the preparation of 8 The preparation of their base class 9 AJAX Java class registration services 10 Their registration-type of preparation of 10 JSP in the use of 12 The introduction of JsonRpcClient.js 12 Calling 12 Overview Following the JavaScript and practice of advanced applications, after the launch json-rpc-for-java open source code, is only 100 lines of javascript code and less than 10 java super lightweight paper to achieve the rapid adoption of javaScript call java objects and Back to the arbitrary target lightweight framework, and support the cascade call, which means do not need additional JavaScript programming, it can be registered through the javascript call object and return to the java java object, if there are ways to return to the target, In this javascript in the java object to return to the variables, you can also continue to call it the way . This is the lightweight json-rpc-for-java the Shenqizhichu. Link Author Links Author csdn blog Sina author of more than 600 million hits blog Author site Open-source projects address /p/json-rpc-for-java/ Download address works svn /svn/trunk/ Does not require a username and password. Examples of projects download address /files/JsonRpcExample2008-08-05.rar Test environment: MyEclipse, JRE1.4 (or 1.6), tomcat 5.0 (or 6.0) If you want to test, can use the appropriate environment, need not be so high version of the environment, Import projects in the works Please note amended to correct JRE The path: Supported browser IE4, IE5, IE6, IE7, IE8, FireFox, Opera, Safari and so on. Java objects to the JavaScript object tables JavaScript object that Java objects java.lang.String String java.lang.Object String call java object toString (), after conversion to JavaScript java.util.Date, java.sql.Timestamp String But for yyyy-MM-dd HH: mm: ss.000, if at all Fenmiao 0, is: yyyy-MM-dd java.lang.Boolean Blooean the corresponding value: true, false java.lang.Character String single string, such as: c java.lang.Short, java.lang.Integer, java.lang.Long, java.lang.Float, java.lang.Double, java.math.BigDecimal Number in JavaScript are for the number of objects, can be directly involved in the increase, subtraction, multiplication, with the exception of computing java.util.Map Object, for example: obj key1, obj key3, obj.key3, not only function of the way, of course, the function Ototype are still some properties java.util.List Array, for example: a 0, a 2. getList () In other words, can also exist List of objects, these objects can still have their own methods null null space object Object other Java objects such as: obj.displayName (), obj.aac001, can have properties and methods Features Automatically be caught In your java services prepared by the method does not require try (.) Catch (Exception e) (), this framework will you catch an error message, but you do not get javascript to the correct data, can be called asynchronous Object methods getErrMsg () access to unusual sources, the method in mon.JsonRpcObject in the package, which is java-based AJAX services category. JavaScript registered in the release of the Java clients You only need to call in JavaScript release () on the release of the Java objects registered resources, see detailed examples of works, or see /p/json-rpc-for-java/wiki/Wiki32 Cascade called functional Please do not understand the combination of local examples of the works to understand. 1, Java in the registration of objects myjsonrpc 2, JSP JavaScript in access to the object: var myjsonrpc = JsonRpcClient (). Myjsonrpc; 3, the call was registered java object methods getMyObj, the return of the java object TestDomain: var oDomain = myjsonrpc. getMyObj (); / / To the call to return to the java object methods alert (oDomain. toXml () Or: alert (myjsonrpc. getList () 1. ToXml (); If toXml return or a composite of Java objects, you may continue to call in JavaScript, without the need for additional programming. Use Web.xml configuration Web.xml need to add the following configuration JSONRPCServlet Jcore.jsonrpc.servlet.JSONRPCServlet 2 JSONRPCServlet / JRPC The introduction of Jar package The need to introduce the works: JSON-RPC.jar, commons-logging.jar, commons-logging-api.jar, which followed two examples of projects in the jar in the JsonRpcExample webapp WEB-INF lib under. Examples of projects download address: /files/JsonRpcExample2008-08-05.rar And, JSON-RPC.jar, you can re-introduction of the source code package. AJAX Java services such as the preparation of We must inherit and mon.JsonRpcObject, and to achieve interface java.io.Serializable. Examples of projects such as AJAX services in the Java class: package test; import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import mon.JsonRpcObject; public class TestObject extends JsonRpcObject implements Serializable ( Private static final long serialVersionUID = 1L; Private List myList = new ArrayList (); Private Map map = new HashMap (); Public TestObject () ( MyList.add ( good); MyList.add (new TestDomain (); / / Map of the compound can also Add to object Map.put ( first, The first duty); Map.put ( p2, new Date (); Map.put ( domain, myList.get (1); ) / * * To Map object * Return * / Public Map getMap () ( Return map; ) / * Get an ordinary object * Return * / Public Object getStr () ( Return myList.get (0); ) / * * Access to a composite target * Return * / Public Object getMyObj () ( Return myList.get (1); ) / * Get List object * Return * / Public List getList () ( Return myList; ) ) The preparation of their base class Similarly, you can achieve some of succession mon.JsonRpcObject base class, so in their own projects in a more convenient and practical, such as: package mon; import com.yinhai.webframework.session.UserSession; import mon.JsonRpcObject; public abstract class Yhsi2JsonRpcObj extends JsonRpcObject ( Private UserSession us = null; Public Yhsi2JsonRpcObj () ( Super (); ) Public UserSession getUs () ( If (null = us) Us = UserSession.getUserSession (getRequest (); Return us; ) ) AJAX Java class registration services / / Note that the class must be registered is to be examples of the type mon.JsonRpcRegister.registerObject (us, myjsonrpc, test.TestObject.class); Test.TestObject.class way is to use multiple registration does not guarantee test.TestObject been repeatedly registered the implementation of several examples, and then to improve performance and allow multiple registration - indeed, within only registered once. Registration of their own kind of preparation Of course, you can also inherit mon.JsonRpcRegister in the application menu in order to make the switch when the release of resources, such as: package mon; import javax.servlet.http.HttpServletRequest; import mon.Content; import mon.JSONRPCBridge; import com.yinhai.webframework.session.UserSession; import mon.JsonRpcRegister; / * * Registration JsonRpc target * Author just * * / public class JsonRpcRegister extends mon.JsonRpcRegister ( / * * The request to register objects * Param request * Param szKeyName * Param o * / public static void registerObject (HttpServletRequest request, String szKeyName, Object o) ( registerObject (UserSession.getUserSession (request), szKeyName, o); ) / * * The request to register objects * Param request * Param szKeyName * Param o * / public static void registerObject (UserSession us, String szKeyName, Object o) ( If (null! = Us) ( JSONRPCBridge brg = (JSONRPCBridge) us.getCurrentBusiness (). GetSessionResource (Content.RegSessionJSONRPCName); / / If this is the first time on the registration target If (null = brg) us.getCurrentBusiness (). putSessionResource (Content.RegSessionJSONRPCName, brg = new JSONRPCBridge (). setSession (us.getHt

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论