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

时间: 2024-10-24 10:25:53

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

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); } 2.新建RPC接口的实现类,StudentManager.java pack

Spring RPC 入门学习(1)

Spring搭建RPC环境 第一,下载所需要的jar包,下载地址:https://yunpan.cn/cPErQeANrSMyB (提取码:63e5),见下图: 第二,新建动态WebProject,把所下载的jar包,放入lib目录下: 第三,新建远程调用接口: package com.cvicse.ump.rpc; public interface HelloWorld { public String sayHello(String name); } 第四,添加接口的实现: package c

简单的Spring IoC入门学习

IoC控制反转,由spring统一管理类的创建,不再由自己创建. 一.导入所需jar core.context.beans.expression.common-logging 二.目标类 package com.claudxyz.IoC; public interface UserService { public abstract void addUser(); } package com.claudxyz.IoC; public class UserServiceImpl implements

Spring.Net 入门学习笔记

一. 基本概念    Spring.Net是一个轻量级的控制反转(Ioc)和面向切面的(Aop)的容器框架: Ioc:控制反转:简单的说就是将创建对象的控制权转交给外部容器(IApplicationContext); DI:依赖注入:其实就是将通过容器创建对象的时候,给属性.构造函数的参数注入默认值: Spring.Net不是一门技术,而是一种思想,其目的就是实现松耦合: 二.使用容器创建对象

spring boot入门学习---1

1.maven配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM

Spring入门学习(一)

今天开始正式学习Spring框架,首先讲安装,spring 的版本发布中,有的版本有同步发布依赖包的下载,有的却没有.3.0.2的版本有, 而3.2.3 的版本却没有(可能是因为依赖库没有改变的缘故). 1.下载Spring 这里只是为了学习,下载3.0.2版本的:http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.0.2.RELEASE-dependencies.zip 2.配置Spri

Struts2+Spring+Ibatis用户注册、登录、管理入门学习实例源码下载

原文:Struts2+Spring+Ibatis用户注册.登录.管理入门学习实例源码下载 源代码下载地址:http://www.zuidaima.com/share/1550463735532544.htm Struts2+Spring+Ibatis用户注册.登录.管理入门学习实例源码下载 项目截图: jar包和sql文件都在源码包中. 运行截图: 用户注册页面: 用户管理页面:

[Spring Data MongoDB]学习笔记--MongoTemplate插入修改操作

插入操作: 直接给个例子 import static org.springframework.data.mongodb.core.query.Criteria.where; import static org.springframework.data.mongodb.core.query.Criteria.query; … Person p = new Person("Bob", 33); mongoTemplate.insert(p);//还可以多加一个参数,来提供collectio

JavaSE入门学习21:Java面向对象之接口(interface)(二)

一接口实现的多态 在上一篇博文:JavaSE入门学习20:Java面向对象之接口(interface)(一)中提到了接口的实现存在多态性,那么 这一篇主要就要分析接口实现的多态. 实例一 Test.java源文件代码: public class Test{ public static void main(String[] args){ //实现接口Singer Singer s1 = new Student("Amy"); s1.sing(); s1.sleep(); s1.study