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文件
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>JSONRPCServlet</servlet-name> <servlet-class> com.metaparadigm.jsonrpc.JSONRPCServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>JSONRPCServlet</servlet-name> <url-pattern>/JSON-RPC</url-pattern> </servlet-mapping> </web-app>
四、新建一jsp文件jsonRpc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="JSONRPCBridge" scope="session" class="com.metaparadigm.jsonrpc.JSONRPCBridge"/>
<jsp:useBean id="userService" scope="request" class="jp.com.syspro.service.UserService"/>
<%
JSONRPCBridge.registerObject("userService",userService);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="script/jsonrpc.js"></script>
<script type="text/javascript" src="script/show.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css">
<title>JSONRPC+JSON+Java</title>
</head>
<body>
<fieldset>
<legend>查询用户</legend>
<input type="text" name="userId">
<input type="button" value="search" onClick="search()">
<select id="userList"></select>
</fieldset>
<table id="userTable" summary="user table">
<caption>用户列表</caption>
<tbody>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>生日</th>
<th>电话</th>
<th>地址</th>
</tr>
</tbody>
</table>
</body>
</html>
五、新建一JavaScript文件show.js
// 定义一个JSONRPC对象
var jsonrpc;
window.onload=function(){
jsonrpc=new JSONRpcClient("JSON-RPC");
}
// 事件处理函数
function search(){
var userId=document.getElementsByName("userId")[0].value;
if(userId==null||userId==""){
jsonrpc.userService.getNameList(showList);
jsonrpc.userService.getNameSet(showSet);
jsonrpc.userService.getNameMap(showMap);
}else{
jsonrpc.userService.getNameById(showName,userId);
jsonrpc.userService.getUserById(showUser,userId);
}
}
// 定义一个User类
function User(name,gender,age,phone,address,email){
this.name=name;
this.gender=gender;
this.age=age;
this.phone=phone;
this.address=address;
this.email=email;
}
// 返回List处理
function showList(result,exception){
if(exception==null){
if(result!=null){
var list=result.list;
var user=new User();
for(var i in list){
user.name=list[i];
update(user);
}
}else{
alert("no result");
}
}else{
alert(exception.message);
}
}
// 返回Set处理
function showSet(result,exception){
if(exception==null){
if(result!=null){
var set=result.set;
var user=new User();
for(var value in set){
user.name=value;
update(user);
}
}else{
alert("no result");
}
}else{
alert(exception.message);
}
}
// 返回Map处理
function showMap(result,exception){
if(exception==null){
if(result!=null){
var map=result.map;
for(var key in map){
fill(key,map[key]);
}
}else{
alert("no result");
}
}else{
alert(exception.message);
}
}
// 返回String处理
function showName(name,exception){
if(exception==null){
if(name!=null&&name!=""){
var user=new User();
user.name=name;
update(user);
}else{
alert("no result");
}
}else{
alert(exception.message);
}
}
// 返回JavaBean处理
function showUser(user,exception){
if(exception==null){
if(user!=null){
update(user);
}else{
alert("no result");
}
}else{
alert(exception.message);
}
}
// 更新表格
function update(user){
var table=document.getElementById("userTable");
// 在表格末尾插入一行
table.insertRow(-1);
// 获取当前表格的行数
var rows=table.rows.length;
// 在插入的一行插入7列
table.rows[rows-1].insertCell(-1);
table.rows[rows-1].insertCell(-1);
table.rows[rows-1].insertCell(-1);
table.rows[rows-1].insertCell(-1);
table.rows[rows-1].insertCell(-1);
table.rows[rows-1].insertCell(-1);
table.rows[rows-1].insertCell(-1);
// 填充数据
table.rows[rows-1].cells[0].innerHTML=rows-1;
if(user.name!=undefined){
table.rows[rows-1].cells[1].innerHTML=user.name;
}
if(user.gender!=undefined){
table.rows[rows-1].cells[2].innerHTML=user.gender;
}
if(user.age!=undefined){
table.rows[rows-1].cells[3].innerHTML=user.age;
}
if(user.birthday!=undefined){
table.rows[rows-1].cells[4].innerHTML=user.birthday;
}
if(user.phone!=undefined){
table.rows[rows-1].cells[5].innerHTML=user.phone;
}
if(user.address!=undefined){
table.rows[rows-1].cells[6].innerHTML=user.address;
}
}
// 填充下拉列表
function fill(userName,userId){
var userList=document.getElementById("userList");
var option=document.createElement("option");
option.text=userName;
option.value=userId;
for(var i=0;i<userList.options.length;i++){
// 如果有重复数据则返回
if(userList.options[i].value==option.value){
return;
}
}
// 添加到下拉列表
userList.options.add(option);
}
六、新建一Java文件UserService
package jp.com.syspro.service;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import jp.com.syspro.bean.UserBean;
import jp.com.syspro.dao.UserDao;
public class UserService implements Serializable{
private static final long serialVersionUID = 1L;
private UserDao userDao;
public String getNameById(String id){
userDao=new UserDao();
String name=userDao.getNameById(id);
return name;
}
public List<String> getNameList(){
userDao=new UserDao();
List<String> list=userDao.getNameList();
return list;
}
public Set<String> getNameSet(){
userDao=new UserDao();
Set<String> set=userDao.getNameSet();
return set;
}
public Map<String,String> getNameMap(){
userDao=new UserDao();
Map<String,String> map=userDao.getNameMap();
return map;
}
public UserBean getUserById(String id){
userDao=new UserDao();
UserBean user=userDao.getUserById(id);
return user;
}
}
注意:
1、UserBean里面的非基本类型数据的变量的值必须为空。
2、调用后台方法时,参数不能为数组。
7736

被折叠的 条评论
为什么被折叠?



