接上例:http://blog.csdn.net/kunshan_shenbin/archive/2008/11/25/3369384.aspx
这次看看Java自定义对象在 JSON-RPC 中是如何取值的。
如下图所示建立工程:
Cat.java
- package lee;
- public class Cat
- {
- private String name;
- public Cat() {
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return (this.name);
- }
- }
Hello.java
- package lee;
- import java.io.Serializable;
- public class Hello implements Serializable {
- private static final long serialVersionUID = -7660970518915343703L;
- public Cat getCat(String name)
- {
- Cat cat = new Cat();
- cat.setName(name);
- return cat;
- }
- public String sayHello(Person p)
- {
- return p.getName() + "对象被传到了Java方法";
- }
- }
Person.java
- package lee;
- public class Person {
- private String name;
- public Person() {
- }
- public Person(String name) {
- this.name = name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return (this.name);
- }
- }
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <servlet-name>jsonServlet</servlet-name>
- <servlet-class>
- com.metaparadigm.jsonrpc.JSONRPCServlet
- </servlet-class>
- </servlet>
- <!-- 此处的url-pattern可以随便更改,只是JavaScript代码中JSONRpcClient以此地址创建 -->
- <servlet-mapping>
- <servlet-name>jsonServlet</servlet-name>
- <url-pattern>/myjson</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>hello.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
hello.js
- var jsonrpc;
- function onLoad()
- {
- jsonrpc = new JSONRpcClient("myjson");
- }
- function getCat()
- {
- var whoNode = document.getElementById("name");
- jsonrpc.hello.getCat(cb, whoNode.value);
- }
- function sendObj()
- {
- var whoNode = document.getElementById("name");
- var personObj =
- {
- "javaClass": "lee.Person",
- "name": whoNode.value
- };
- jsonrpc.hello.sayHello(cb, personObj);
- }
- function cb(result, exception)
- {
- if(result == null && exception != null)
- {
- alert(exception.message);
- }
- if (result != null && exception == null && typeof result == "object")
- {
- alert("使用回调函数:成功获得猫对象,猫名为:" + result.name);
- }
- if (result != null && exception == null && typeof result != "object")
- {
- alert("使用回调函数:" + result);
- }
- }
hello.jsp
- <%@page contentType="text/html;charset=utf-8"%>
- <jsp:useBean id="JSONRPCBridge" scope="session" class="com.metaparadigm.jsonrpc.JSONRPCBridge" />
- <jsp:useBean id="hello" scope="session" class="lee.Hello" />
- <%
- //将某个JavaBean实例注册成JavaScript实例
- //JSONRPCBridge实例得到名称必须是JSONRPCBridge,因为JSONRPCServlet会自动注册该名字的桥
- JSONRPCBridge.registerObject("hello", hello); %>
- <html>
- <head>
- <script type="text/javascript" src="jsonrpc.js"></script>
- <script type="text/javascript" src="hello.js"></script>
- <title>JSON-RPC-Java的Ajax</title>
- </head>
- <body bgcolor="#ffffff" onLoad="onLoad()">
- <h2>JSON-RPC-Java的Ajax</h2>
- <p>
- <strong>请输入用户名</strong>
- <input type="text" id="name" size="30" value="" />
- <P>
- <input type="button" value="对象响应" onclick="getCat()" />
- <input type="button" value="发送对象" onclick="sendObj()" />
- </p>
- </body>
- </html>
jsonrpc.js和前例一致。