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

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

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.Sping的配置文件: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. 编写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 ceshi(){
var name = document.getElementById("name").value;
rpc.call('getStudentById',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>

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

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

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

  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应用中创建全局获取ApplicationContext对象

    在Spring应用中创建全局获取ApplicationContext对象 1.需要创建一个类,实现接口ApplicationContextAware的setApplicationContext方法. ...

  5. Spring Boot入门学习

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

  6. 将Spring容器跟随系统启动并获取容器对象

    将Spring容器随系统启动的方法: 在web.xml中配置监听器,监听的对象为ContextLoaderListener <listener> <listener-class> ...

  7. spring mvc中几种获取request对象的方式

    在使用spring进行web开发的时候,优势会用到request对象,用来获取访问ip.请求头信息等 这里收集几种获取request对象的方式 方法一:在controller里面的加参数 public ...

  8. Java反射学习-2 - 获取Class对象的三种方式

    package cn.tx.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import ...

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

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

随机推荐

  1. U盘内容被病毒隐藏的解决办法(亲测可用)

    前几天用U盘的时候不小心感染上了病毒,用自己的电脑打开后里面只剩下一个U盘的快捷方式,选中显示隐藏文件之后依然没有任何显示,但是查看U盘的属性的时候可以看到,U盘已经使用了300多M,所以就上网查了一 ...

  2. 5.1Python函数(一)

    目录 目录 前言 (一)函数的基本知识 (二)函数的基本使用 ==1.函数的简单定义== ==2.传值函数== (3)输出效果 ==3.不定长函数== ==4.缺省函数== ==5.函数的传值过程== ...

  3. Python3编写网络爬虫12-数据存储方式五-非关系型数据库存储

    非关系型数据库存储 NoSQL 全称 Not Only SQL 意为非SQL 泛指非关系型数据库.基于键值对 不需要经过SQL层解析 数据之间没有耦合性 性能非常高. 非关系型数据库可细分如下: 键值 ...

  4. Sublime Text3如何快速预览html文件

    Sublime Text3 步骤1:选择 Tools----> Build System ----> New Build System... 步骤2:输入以下内容 "cmd&qu ...

  5. ug nx7.5安装方法(图文详解)

           UG7.5,也称NX7.5,自卑西门子收购,软件名字已经改为SIEMENS NX了,ug7.5是一套集成了CAD.CAE 和CAM解决方案,能为设计师们提供最功能齐全的设计环境,能够大大 ...

  6. Memcached服务加固方案

    配置访问控制.建议用户不要将服务发布到互联网上而被黑客利用,可以通过ECS安全组规则或IPtables配置访问控制规则.例如,在Linux环境中运行命令,在IPtables中添加此规则只允许192.1 ...

  7. Arduino IDE for ESP8266 ()组网

    多个esp8266连接在同一个 WIFI上,在局域网内部,相互传数据 #include <ESP8266WiFi.h> #define led 2 //发光二极管连接在8266的GPIO2 ...

  8. PAT A1098 Insertion or Heap Sort (25 分)——堆排序和插入排序,未完待续。。

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  9. python基础学习第三天

    #变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间#基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中#变量可以指定不同的数据类型,这些变量可以存储整数.小数 ...

  10. C++ 函数模板&类模板

    函数模板 #include <iostream> #include <string> using namespace std; template <typename T& ...