Spring RPC 入门学习(3)-插入Student对象
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对象的更多相关文章
- Spring RPC 入门学习(3)-获取Student对象
Spring RPC传递对象. 1. 新建RPC接口:StudentInterface.java package com.cvicse.ump.rpc.interfaceDefine; import ...
- Spring RPC 入门学习(2)-获取Map对象
Spring RPC传递Map用例编写 1. 新建RPC接口类 package com.cvicse.ump.rpc.interfaceDefine; import java.util.Map; pu ...
- Spring RPC 入门学习(1)-HelloWorld入门
Spring搭建RPC环境 第一,下载所需要的jar包,下载地址:https://yunpan.cn/cPErQeANrSMyB (提取码:63e5),见下图: 第二,新建动态WebProject,把 ...
- Spring Boot入门学习
1. Spring Boot概述 1.1.什么是Spring Boot SpringBoot是一个可使用Java构建微服务的微框架.是Spring框架及其社区对"约定优先于配置"理 ...
- Spring Boot入门学习,解决复杂的spring配置文件及jar包
转载:https://www.cnblogs.com/wmyskxz/p/9010832.html 总结 为何出了这样的框架? Spring Boot 是所有基于 Spring 开发的项目的起点.Sp ...
- Spring Boot入门学习必知道企业常用的Starter
SpringBoot企业常用的 starter SpringBoot简介 SpringBoot运行 SpringBoot目录结构 整合JdbcTemplate @RestController 整合JS ...
- Spring.Net 入门学习笔记-----one
一. 基本概念 Spring.Net是一个轻量级的控制反转(Ioc)和面向切面的(Aop)的容器框架: Ioc:控制反转:简单的说就是将创建对象的控制权转交给外部容器(IApplicationC ...
- Spring.Net 入门学习(一)实现控制器翻转与依赖注入
Spring.net IOC:Invasion of Control,控制器翻转,名字由英文翻译过来就是这个意思了,其实用通俗的话来说就是:将创建对象的职责交给控制器来做,这个控制器就是spring了 ...
- Spring的入门学习笔记 (AOP概念及操作+AspectJ)
AOP概念 1.aop:面向切面(方面)编程,扩展功能不通过源代码实现 2.采用横向抽取机制,取代了传统的纵向继承重复代码 AOP原理 假设现有 public class User{ //添加用户方法 ...
随机推荐
- 第五章 绘图基础(LINEDEMO)
LINEDEMO程序绘制一个矩形.两条直线.一个椭圆和一个圆角矩形.该程序表明,定义了封闭矩形的这些函数确实对这些区域进行了填充,因为椭圆后面的线被隐藏了. /*------------------- ...
- Centos7系统详细的启动流程
熟悉系统启动流程对于我们学习Linux系统是非常有帮助的,虽然基础,但能帮助我们更加理解Linux系统的工作机制.以下将以CentOS发行版为例来介绍Linux系统的启动流程,因为在CentOS 5. ...
- Windows端部署zabbix-agent
一.windows客户端的配置关闭windows防火墙或者开通10050和10051端口(1).关闭防火墙(不推荐直接关闭,测试可以这样做,尤其是最近勒索病毒猛烈)开始—控制面板—windows防火墙 ...
- VRS生成的虚拟观测值存在的问题
目前生成的虚拟观测值. 天津的版本,如果有数据库中有天线类型,那么会对天线类型改正了两次. 解决方法:在生成虚拟观测值编码的部分,注释掉天线改正的部分. 对结果的影响:错误版本生成的虚拟观测值,移动站 ...
- Beta冲刺! Day1 - 磨刀
Beta冲刺! Day1 - 磨刀 今日已完成 晨瑶:罗列Beta计划.和新人交接.任务安排 昭锡:无 永盛:服务器出现一些 mysql 的问题,伟鹏的爬取脚本没办法远程链接到服务器,在修 立强:学习 ...
- ABAP CDS 替换对象(Replacement Objects)引起的数据错误
最近遇到了一个诡异的问题:从CDS视图中取得的数据,和从透明表中取得的数据,会有不同的值.在这里记录下问题的表现和解决方案,以供参考. 系统版本:S/4HANA OP1610 涉及表:MCHB 本文链 ...
- JS中=>,>>>是什么意思
最近经常看到 JS中=>,符号,于是查了一下别人的博客 =>是es6语法中的arrow function 举例:(x) => x + 6 相当于 function(x){ ret ...
- BSOJ 3899 -- 【CQOI2014】 数三角形
Description 给定一个n*m的网格,请计算三个点都在格点上的三角形共有多少个.下图为4*4的网格上的一个三角形. 注意三角形的三点不能共线. Input 输入一行,包含两个空格分隔的正整数 ...
- Electron 发生错误 "Cannot find module app"的解决方案
运行一个electron小demo出现的一个错误信息:Cannot find module app 原代码如下所示: var app = require('app'); var BrowserWind ...
- PAT A1018 Public Bike Management (30 分)——最小路径,溯源,二标尺,DFS
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...