使用jsonrpc

JSON:JavaScript Object Notation,JavaScript对象的一种字面量描述格式,是一种轻量级的数据交换格式。
RPC:Remote procedure call,远程过程(函数、方法)调用。
jsonrpc 依赖 jsonrpc-1.0.jar 文件,以及一个 js文件(jsonrpc.js)
(1)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">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<listener>
<listener-class>
<!--注册配置的Bean对象-->
org.jsonrpc.service.RegistServiceToJsonrpcBridgeListener
</listener-class>
</listener>

<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>

<session-config>
<session-timeout>120</session-timeout>
</session-config>

<welcome-file-list>
<welcome-file> index.html </welcome-file>
</welcome-file-list>

</web-app>

(2)jsonrpc.xml
<?xml version="1.0" encoding="UTF-8"?>   
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">

<bean name="jsonrpcServiceConfigurer"
class="org.jsonrpc.service.spring.JsonrpcServiceConfigurer">
<property name="services">
<map>
<entry key="helloWorld">
<ref bean="helloWorld" />
</entry>
</map>
</property>
</bean>
</beans>

(3)applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd ">

<import resource="jsonrpc.xml"/>

<bean id="helloWorld"
class="com.logcd.service.impl.HelloWorldImpl">
</bean>
</beans>

(4)HelloWorld.java和HelloWorldImpl.java
public interface HelloWorld {
public String sayHelloWorld(String name);
}

public class HelloWorldImpl implements HelloWorld{

public String sayHelloWorld(String name) {
return "Hello World! hello, " + name;
}
}

(5)调用

<script type="text/javascript" src="script/jsonrpc.js"></script><script>
var jsonrpc = null;
//初始化JSONRpcClient对象
jsonrpc = new JSONRpcClient("JSON-RPC");
function jsonrpcHello(){
var name = "阿花!";
var result = jsonrpc.helloWorld.sayHelloWorld(resultCallback,name);
}
function resultCallback(result, e){
alert("The server replied: " + result);
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值