Spring RPC 向后台传递对象

1. 新建RPC接口:StudentInterface.java

package com.cvicse.ump.rpc.interfaceDefine;

import com.cvicse.ump.student.Student;

public interface StudentInterface {
public Student getStudentById(String id);
public Boolean insertStudent(Student student); }

2. 新建RPC接口实现类:StudentManager.java

package com.cvicse.ump.rpc.interfaceImp;

import com.cvicse.ump.rpc.interfaceDefine.StudentInterface;
import com.cvicse.ump.student.Student; public class StudentManager implements StudentInterface { @Override
public Student getStudentById(String id) {
Student student = new Student();
student.setId(id);
student.setName("xiaoDy");
student.setAge(23);
student.setAddress("河北石家庄");
return student;
} @Override
public Boolean insertStudent(Student student) {
System.out.println(student);
return true;
} }

3. 配置Spring和RPC服务,修改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. Spring配置文件: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" 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>

5. rpc 配置文件:spring-service.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-3.2.xsd"> <description>Spring Service Configuration</description> <bean id="/studentService" class="com.googlecode.jsonrpc4j.spring.JsonServiceExporter">
<property name="service">
<bean class="com.cvicse.ump.rpc.interfaceImp.StudentManager"></bean>
</property>
<property name="serviceInterface" value="com.cvicse.ump.rpc.interfaceDefine.StudentInterface" />
</bean> <bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> </beans>

6. 引入js文件和需要的jar包

7.新建jsp文件:index.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/studentService"); function selectStudent(){
var name = document.getElementById("num").value;
rpc.call('getStudentById',name, {
success : callback,
failure : errorcallback,
});
}
function callback(r){
var id=r.id;
var name=r.name;
var age = r.age;
var address = r.address;
document.getElementById("id").value = id;
document.getElementById("name").value = name;
document.getElementById("age").value = age;
document.getElementById("address").value = address;
}
function errorcallback(r){
alert(r);
} function insertStudent(){
var id=document.getElementById("id").value;
var name=document.getElementById("name").value;
var age = document.getElementById("age").value;
var address = document.getElementById("address").value; var student = {"id" : id, "name" : name, "age" : age, "address" : address}; rpc.call('insertStudent',student, {
success : insertCallBack,
failure : errorcallback,
}); } function insertCallBack(r){
if(r==true){
alert("插入成功");
}else{
alert("插入失败");
}
} </script>
</head>
<body> <h1>Spring RPC 测试</h1> 学号:<input type="text" id="id" size="17"><br>
姓名:<input type="text" id="name" size="17"><br>
年龄:<input type="text" id="age" size="17"><br>
地址:<input type="text" id="address" size="17"><br>
<br>
<input type="button" value="插入" onclick="insertStudent()">
<br><br>
查询学号:<input type="text" id="num" size="17"><br>
<input type="button" value="查询" onclick="selectStudent()">
</body>
</html>

运行结果:

源码下载地址:https://yunpan.cn/cPETcLkgHtAGN  访问密码 ad6e

Spring RPC 入门学习(3)-插入Student对象的更多相关文章

  1. Spring RPC 入门学习(3)-获取Student对象

    Spring RPC传递对象. 1. 新建RPC接口:StudentInterface.java package com.cvicse.ump.rpc.interfaceDefine; import ...

  2. Spring RPC 入门学习(2)-获取Map对象

    Spring RPC传递Map用例编写 1. 新建RPC接口类 package com.cvicse.ump.rpc.interfaceDefine; import java.util.Map; pu ...

  3. Spring RPC 入门学习(1)-HelloWorld入门

    Spring搭建RPC环境 第一,下载所需要的jar包,下载地址:https://yunpan.cn/cPErQeANrSMyB (提取码:63e5),见下图: 第二,新建动态WebProject,把 ...

  4. Spring Boot入门学习

    1. Spring Boot概述 1.1.什么是Spring Boot SpringBoot是一个可使用Java构建微服务的微框架.是Spring框架及其社区对"约定优先于配置"理 ...

  5. Spring Boot入门学习,解决复杂的spring配置文件及jar包

    转载:https://www.cnblogs.com/wmyskxz/p/9010832.html 总结 为何出了这样的框架? Spring Boot 是所有基于 Spring 开发的项目的起点.Sp ...

  6. Spring Boot入门学习必知道企业常用的Starter

    SpringBoot企业常用的 starter SpringBoot简介 SpringBoot运行 SpringBoot目录结构 整合JdbcTemplate @RestController 整合JS ...

  7. Spring.Net 入门学习笔记-----one

    一. 基本概念    Spring.Net是一个轻量级的控制反转(Ioc)和面向切面的(Aop)的容器框架: Ioc:控制反转:简单的说就是将创建对象的控制权转交给外部容器(IApplicationC ...

  8. Spring.Net 入门学习(一)实现控制器翻转与依赖注入

    Spring.net IOC:Invasion of Control,控制器翻转,名字由英文翻译过来就是这个意思了,其实用通俗的话来说就是:将创建对象的职责交给控制器来做,这个控制器就是spring了 ...

  9. Spring的入门学习笔记 (AOP概念及操作+AspectJ)

    AOP概念 1.aop:面向切面(方面)编程,扩展功能不通过源代码实现 2.采用横向抽取机制,取代了传统的纵向继承重复代码 AOP原理 假设现有 public class User{ //添加用户方法 ...

随机推荐

  1. [HDFS_4] HDFS 的 Java 应用开发

    0. 说明 在 IDEA下 进行 HDFS 的 Java 应用开发 通过编写代码实现对 HDFS 的增删改查操作 1. 流程 1.1 在项目下新建 Moudle 略 1.2 为 Moudle 添加 M ...

  2. 【第一章】zabbix3.4监控WindowsCPU使用率磁盘IO磁盘事件日志监控阈值邮件报警详细配置

    Windows安装zabbix-agent 监控Windows-CPU使用率 监控Windows-磁盘IO性能监控 监控Windows/Linux-磁盘触发器阈值更改 监控Windows-网卡自动发现 ...

  3. 基于LNMP(fastcgi协议)环境部署、原理介绍以及fastcgi_cache配置以及upstream模块负载均衡讲解

    ngx_http_proxy_module只能反向代理后端使用HTTP协议的主机.而ngx_http_fastcgi_module只能反向代理后端使用FPM或者使用FastCGI协议的客户端. 一.部 ...

  4. Mysql中use一个表出现警告:Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A

    今天使用mysql登录数据库,use一个表的时候出现警告信息,详细如下: 后来上网查了一下,出现问题的原因是: 进入mysql时,没有使用  -A  参数 平时我们习惯使用:mysql -hhostn ...

  5. LInux下(centos7.2)更新 python3.7

    进入超级管理员目录  su root 下载 wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz 找到下载的文件解压  tar - ...

  6. Android Studio入门问题汇总

    1.如何设置 AS 中的字体大小 2.如何切换 AS 的皮肤颜色,默认为黑色,修改为白色,改为 default 3.首次安装 Android Studio并打开时,如果创建了一个新工程并将工程保存在另 ...

  7. python 之 初识面向对象

    编程的两种范式 我们知道,程序 = 特定的语法+数据结构+算法 好像这个和我们熟知的小说有类似之处啊,小说 = 人物+背景+情节 写小说呢,都是有模板的,so,写程序也是一样,我们把这个“模板”叫做编 ...

  8. 阿里巴巴Web前端面试的一道JS题目,求解答!!!

    题目大概是这种: function outer(){ return inner; var inner = "a"; function inner(){}; inner = 9; } ...

  9. 让你提前认识软件开发(31):数据库脚本中的begin与end

    版权声明:本文为博主原创文章.对文章内容有不论什么意见或建议,欢迎与作者单独交流.作者QQ(微信):245924426. https://blog.csdn.net/zhouzxi/article/d ...

  10. 【Lucene4.8教程之中的一个】使用Lucene4.8进行索引及搜索的基本操作

    版权声明:本文为博主原创文章.转载请注明来自http://blog.csdn.net/jediael_lu/ https://blog.csdn.net/jediael_lu/article/deta ...