Spring RPC 入门学习(2)-获取Map对象
Spring RPC传递Map用例编写
1. 新建RPC接口类
package com.cvicse.ump.rpc.interfaceDefine;
import java.util.Map;
public interface MapTest {
public Map getByName(String name);
}
2.RPC接口实现类
package com.cvicse.ump.rpc.interfaceImp; import java.util.HashMap;
import java.util.Map; import com.cvicse.ump.rpc.interfaceDefine.MapTest; public class MapTestImp implements MapTest{ @Override
public Map getByName(String name) {
HashMap <String,String>retMap = new HashMap<String, String>();
retMap.put("name", name);
retMap.put("id", name+"_001");
retMap.put("age", name+"_25");
retMap.put("birthday", name+"_19880923");
retMap.put("address", name+"_山东济南"); return retMap;
} }
3. 搭建RPC和SPRING配置文件,web.xml文件配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringRPC</display-name> <!-- Spring相关配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- RPC Servlet相关配置 -->
<servlet>
<servlet-name>rpcServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-service.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>rpcServlet</servlet-name>
<url-pattern>/rpcService/*</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4. RPC配置:
<?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-3.2.xsd"> <description>Spring Service Configuration</description> <bean id="mapTest" class="com.cvicse.ump.rpc.interfaceImp.MapTestImp" />
<bean id="/MapTestImp" class="com.googlecode.jsonrpc4j.spring.JsonServiceExporter">
<property name="service" ref="mapTest" />
<property name="serviceInterface" value="com.cvicse.ump.rpc.interfaceDefine.MapTest" />
</bean> <bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> </beans>
5.Spring配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<description>Spring Context Configuration</description>
</beans>
6.引入相应的jar和js文件;
7.编写jsp文件:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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">
<title>Spring RPC TEST</title>
<script src="js/jsonrpcjs-0.1.8.min.js"></script>
<script type="text/javascript">
var rpc = new jsonrpc.JsonRpc("rpcService/MapTestImp"); function ceshi(){
var name = document.getElementById("name").value;
rpc.call('getByName',name, {
success : callback,
failure : errorcallback,
}); function callback(r){
var name=r.name;
var age = r.age;
alert(age);
}
function errorcallback(r){
alert(r);
}
} </script>
</head>
<body> <h1>Spring RPC 测试</h1>
请输入姓名:<input type="text" id="name" size="17">
<input type="button" value="Hello" id="but1" onclick="ceshi()">
</body>
</html>
如果不引入rpc的js文件,只用jquery的ajax实现,代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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">
<title>Spring RPC TEST</title>
<script src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript"> var url = "http://localhost:8080/SpringRpc2/rpcService/MapTestImp";
$(document).ready(
function(){ $("#but2").click(//添加点击事件
function(){
var name = $("#name").val();//获取文本框输入的内容 //p必须按照以下格式,id代表一次请求;method代表要调用的函数;params代表函数的参数
var p = {
"id" : 3,
"method" : "getByName",
//"params" : [ {"id":12,"name":"aaa"} ]
"params" : [ name ]
}; $.ajax( {
type : "post",
url : url,
dataType : "json",
data : JSON.stringify(p),//转JSON
contentType : "application/json; charset=UTF-8",
success : function(msg) {
var retValue = msg.result;
alert("Data Saved: "+retValue.address );
},
error:function(error){
alert("查询失败");
}
});
} );
} ); </script>
</head>
<body> <h1>Spring RPC 测试</h1>
请输入姓名:<input type="text" id="name" size="17"> <input type="button" value="测试2" id="but2">
</body>
</html>
工程源码:https://yunpan.cn/cP5JwPxgXyRER (提取码:a253)
Spring RPC 入门学习(2)-获取Map对象的更多相关文章
- Spring RPC 入门学习(3)-获取Student对象
Spring RPC传递对象. 1. 新建RPC接口:StudentInterface.java package com.cvicse.ump.rpc.interfaceDefine; import ...
- Spring RPC 入门学习(3)-插入Student对象
Spring RPC 向后台传递对象 1. 新建RPC接口:StudentInterface.java package com.cvicse.ump.rpc.interfaceDefine; impo ...
- Spring RPC 入门学习(1)-HelloWorld入门
Spring搭建RPC环境 第一,下载所需要的jar包,下载地址:https://yunpan.cn/cPErQeANrSMyB (提取码:63e5),见下图: 第二,新建动态WebProject,把 ...
- 转:el表达式获取map对象的内容 & js中使用el表达式 & js 中使用jstl 实现 session.removeattribute
原文链接: ①EL表达式取Map,List值的总结 ②在jsp中使用el表达式通过键获得后台的一个map<Long,String>的值 ③在javascript中使用el表达式(有图有真相 ...
- 在Spring应用中创建全局获取ApplicationContext对象
在Spring应用中创建全局获取ApplicationContext对象 1.需要创建一个类,实现接口ApplicationContextAware的setApplicationContext方法. ...
- Spring Boot入门学习
1. Spring Boot概述 1.1.什么是Spring Boot SpringBoot是一个可使用Java构建微服务的微框架.是Spring框架及其社区对"约定优先于配置"理 ...
- 将Spring容器跟随系统启动并获取容器对象
将Spring容器随系统启动的方法: 在web.xml中配置监听器,监听的对象为ContextLoaderListener <listener> <listener-class> ...
- spring mvc中几种获取request对象的方式
在使用spring进行web开发的时候,优势会用到request对象,用来获取访问ip.请求头信息等 这里收集几种获取request对象的方式 方法一:在controller里面的加参数 public ...
- Java反射学习-2 - 获取Class对象的三种方式
package cn.tx.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import ...
随机推荐
- 软件发布时的 GA、RC、Beta
今天在使用 ovirt 的时候,遇到了其 Pre-release 版本并看到如下版本号:ovirt-node-ng-image-update-4.2.7-0.1.rc1.el7.noarch.rpm ...
- 第3章 Git使用人门
[初识Github] 首先让我们大家一起喊一句“Hello Github”.YEAH!就是这样. Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理 ...
- 第 15 章 位操作(fields)
/*----------------------------------- fields.c -- 定义并使用字段 -----------------------------------*/ #inc ...
- 超详细!Github团队协作教程(Gitkraken版)
超详细!Github团队协作教程(Gitkraken版) 一.前期工作 1. 在 Github 上创建 organization step1. 登录Github网站,点击右上角头像,选择 " ...
- Alpha冲刺! Day8 - 砍柴
Alpha冲刺! Day8 - 砍柴 今日已完成 晨瑶:写了部分gitkraken团队协作教程:讨论关于继承baseActivity因为需要参数无法通过override去实现函数,并且initData ...
- C#用默认浏览器打开特定网址的几种方法
方法一:从注册表中读取默认浏览器可执行文件路径 private void button1_Click(object sender, EventArgs e) { //从注册表中读取默认浏览器可执行文件 ...
- XGBOOST应用及调参示例
该示例所用的数据可从该链接下载,提取码为3y90,数据说明可参考该网页.该示例的“模型调参”这一部分引用了这篇博客的步骤. 数据前处理 导入数据 import pandas as pd import ...
- linked-list-cycle (快慢指针判断是否有环)
class Solution { public: bool hasCycle(ListNode *head) { if (head == NULL) return NULL; //空表 ListNod ...
- k8s监控api调用
k8s监控api调用 curl -s --cacert /etc/kubernetes/ssl/ca.pem -basic -u fengjian:fengjian --insecure -X GET ...
- MVC思想及SpringMVC设计理念
1:什么是MVC MVC(Model View Controller)是一种软件设计的框架模式,它采用模型(Model)-视图(View)-控制器(controller)的方法把业务逻辑 ...