JSONRPC+JSON+Java

来源:互联网 发布:双轨直销软件开发 编辑:程序博客网 时间:2024/06/04 18:54

JSONRPC+JSON+Java

    博客分类:
  • Ajax
JavajsonServletJavaScriptJavaEE 

1、简介

JSON-RPC-Java是一个用Java来实现动态JSON-RPC的框架。 利用它内置的一个轻级量JSON-RPC JavaScripIt客户端,可以让你透明地在JavaScript中调用Java代码。

 

2、实例

一、下载JSON-RPC包并解压

http://oss.metaparadigm.com/jsonrpc-dist/json-rpc-java-1.0.1.zip

 

二、将jsonrpc.js文件拷贝到项目WebRoot下任意目录,将jsonrpc jar包拷贝到项目的WEB-INF/lib目录下

 

三、web.xml文件

Xml代码  收藏代码
  1. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"  
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  4.   
  5.     <servlet>  
  6.         <servlet-name>JSONRPCServlet</servlet-name>  
  7.         <servlet-class>  
  8.             com.metaparadigm.jsonrpc.JSONRPCServlet  
  9.         </servlet-class>  
  10.     </servlet>  
  11.   
  12.     <servlet-mapping>  
  13.         <servlet-name>JSONRPCServlet</servlet-name>  
  14.         <url-pattern>/JSON-RPC</url-pattern>  
  15.     </servlet-mapping>  
  16.   
  17. </web-app>  

   

 

四、新建一jsp文件jsonRpc.jsp

Html代码  收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <jsp:useBean id="JSONRPCBridge" scope="session" class="com.metaparadigm.jsonrpc.JSONRPCBridge"/>  
  4. <jsp:useBean id="userService" scope="request" class="jp.com.syspro.service.UserService"/>  
  5. <%  
  6.     JSONRPCBridge.registerObject("userService",userService);  
  7. %>  
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  9. <html>  
  10. <head>  
  11. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  12. <script type="text/javascript" src="script/jsonrpc.js"></script>  
  13. <script type="text/javascript" src="script/show.js"></script>  
  14. <link rel="stylesheet" type="text/css" href="css/style.css">  
  15. <title>JSONRPC+JSON+Java</title>  
  16. </head>  
  17. <body>  
  18.     <fieldset>  
  19.         <legend>查询用户</legend>  
  20.         <input type="text" name="userId">  
  21.         <input type="button" value="search" onClick="search()">  
  22.         <select id="userList"></select>  
  23.     </fieldset>  
  24.     <table id="userTable" summary="user table">  
  25.         <caption>用户列表</caption>  
  26.         <tbody>  
  27.             <tr>  
  28.                 <th>编号</th>  
  29.                 <th>姓名</th>  
  30.                 <th>性别</th>  
  31.                 <th>年龄</th>  
  32.                 <th>生日</th>  
  33.                 <th>电话</th>  
  34.                 <th>地址</th>   
  35.             </tr>  
  36.         </tbody>  
  37.     </table>  
  38. </body>  
  39. </html>  

  

五、新建一JavaScript文件show.js

Js代码  收藏代码
  1. // 定义一个JSONRPC对象  
  2. var jsonrpc;  
  3. window.onload=function(){  
  4.     jsonrpc=new JSONRpcClient("JSON-RPC");  
  5. }  
  6. // 事件处理函数  
  7. function search(){  
  8.     var userId=document.getElementsByName("userId")[0].value;  
  9.     if(userId==null||userId==""){  
  10.         jsonrpc.userService.getNameList(showList);  
  11.         jsonrpc.userService.getNameSet(showSet);  
  12.         jsonrpc.userService.getNameMap(showMap);  
  13.     }else{  
  14.         jsonrpc.userService.getNameById(showName,userId);  
  15.         jsonrpc.userService.getUserById(showUser,userId);  
  16.     }  
  17. }  
  18. // 定义一个User类  
  19. function User(name,gender,age,phone,address,email){  
  20.     this.name=name;  
  21.     this.gender=gender;  
  22.     this.age=age;  
  23.     this.phone=phone;  
  24.     this.address=address;  
  25.     this.email=email;  
  26. }  
  27. // 返回List处理  
  28. function showList(result,exception){  
  29.     if(exception==null){  
  30.         if(result!=null){  
  31.             var list=result.list;  
  32.             var user=new User();  
  33.             for(var i in list){  
  34.                 user.name=list[i];  
  35.                 update(user);  
  36.             }  
  37.         }else{  
  38.             alert("no result");  
  39.         }  
  40.     }else{  
  41.         alert(exception.message);  
  42.     }  
  43. }  
  44. // 返回Set处理  
  45. function showSet(result,exception){  
  46.     if(exception==null){  
  47.         if(result!=null){  
  48.             var set=result.set;  
  49.             var user=new User();  
  50.             for(var value in set){  
  51.                 user.name=value;  
  52.                 update(user);  
  53.             }  
  54.         }else{  
  55.             alert("no result");  
  56.         }  
  57.     }else{  
  58.         alert(exception.message);  
  59.     }  
  60. }  
  61. // 返回Map处理  
  62. function showMap(result,exception){  
  63.     if(exception==null){  
  64.         if(result!=null){  
  65.             var map=result.map;  
  66.             for(var key in map){  
  67.                 fill(key,map[key]);  
  68.             }  
  69.         }else{  
  70.             alert("no result");  
  71.         }  
  72.     }else{  
  73.         alert(exception.message);  
  74.     }  
  75. }  
  76. // 返回String处理  
  77. function showName(name,exception){  
  78.     if(exception==null){  
  79.         if(name!=null&&name!=""){  
  80.             var user=new User();  
  81.             user.name=name;  
  82.             update(user);  
  83.         }else{  
  84.             alert("no result");  
  85.         }  
  86.     }else{  
  87.         alert(exception.message);  
  88.     }  
  89. }  
  90. // 返回JavaBean处理  
  91. function showUser(user,exception){  
  92.     if(exception==null){  
  93.         if(user!=null){  
  94.             update(user);  
  95.         }else{  
  96.             alert("no result");  
  97.         }  
  98.     }else{  
  99.         alert(exception.message);  
  100.     }  
  101. }  
  102. // 更新表格  
  103. function update(user){  
  104.     var table=document.getElementById("userTable");  
  105.     // 在表格末尾插入一行  
  106.     table.insertRow(-1);  
  107.     // 获取当前表格的行数  
  108.     var rows=table.rows.length;  
  109.     // 在插入的一行插入7列  
  110.     table.rows[rows-1].insertCell(-1);  
  111.     table.rows[rows-1].insertCell(-1);  
  112.     table.rows[rows-1].insertCell(-1);  
  113.     table.rows[rows-1].insertCell(-1);  
  114.     table.rows[rows-1].insertCell(-1);  
  115.     table.rows[rows-1].insertCell(-1);  
  116.     table.rows[rows-1].insertCell(-1);  
  117.     // 填充数据  
  118.     table.rows[rows-1].cells[0].innerHTML=rows-1;  
  119.     if(user.name!=undefined){  
  120.         table.rows[rows-1].cells[1].innerHTML=user.name;  
  121.     }  
  122.     if(user.gender!=undefined){  
  123.         table.rows[rows-1].cells[2].innerHTML=user.gender;    
  124.     }  
  125.     if(user.age!=undefined){  
  126.         table.rows[rows-1].cells[3].innerHTML=user.age;  
  127.     }  
  128.     if(user.birthday!=undefined){  
  129.         table.rows[rows-1].cells[4].innerHTML=user.birthday;  
  130.     }  
  131.     if(user.phone!=undefined){  
  132.         table.rows[rows-1].cells[5].innerHTML=user.phone;  
  133.     }  
  134.     if(user.address!=undefined){  
  135.         table.rows[rows-1].cells[6].innerHTML=user.address;   
  136.     }  
  137. }  
  138. // 填充下拉列表  
  139. function fill(userName,userId){  
  140.     var userList=document.getElementById("userList");  
  141.     var option=document.createElement("option");  
  142.     option.text=userName;  
  143.     option.value=userId;  
  144.     for(var i=0;i<userList.options.length;i++){  
  145.         // 如果有重复数据则返回  
  146.         if(userList.options[i].value==option.value){  
  147.             return;  
  148.         }  
  149.     }  
  150.     // 添加到下拉列表  
  151.     userList.options.add(option);  
  152. }  

 

六、新建一Java文件UserService

Java代码  收藏代码
  1. package jp.com.syspro.service;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import java.util.Set;  
  7.   
  8. import jp.com.syspro.bean.UserBean;  
  9. import jp.com.syspro.dao.UserDao;  
  10.   
  11. public class UserService implements Serializable{  
  12.   
  13.     private static final long serialVersionUID = 1L;  
  14.   
  15.     private UserDao userDao;  
  16.   
  17.     public String getNameById(String id){  
  18.         userDao=new UserDao();  
  19.         String name=userDao.getNameById(id);  
  20.         return name;  
  21.     }  
  22.       
  23.     public List<String> getNameList(){  
  24.         userDao=new UserDao();  
  25.         List<String> list=userDao.getNameList();  
  26.         return list;  
  27.     }  
  28.   
  29.     public Set<String> getNameSet(){  
  30.         userDao=new UserDao();  
  31.         Set<String> set=userDao.getNameSet();  
  32.         return set;  
  33.     }  
  34.       
  35.     public Map<String,String> getNameMap(){  
  36.         userDao=new UserDao();  
  37.         Map<String,String> map=userDao.getNameMap();  
  38.         return map;  
  39.     }  
  40.       
  41.     public UserBean getUserById(String id){  
  42.         userDao=new UserDao();  
  43.         UserBean user=userDao.getUserById(id);  
  44.         return user;  
  45.     }  
  46.       
  47. }  

 

 

注意:

1、UserBean里面的非基本类型数据的变量的值必须为空。

2、调用后台方法时,参数不能为数组。

 

  • JsonRpc.rar (596.6 KB)
  • 下载次数: 58

原创粉丝点击