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. MySQL 复制夯住一例排查以及原理探讨

    目录 目录 一 引子 二 故障分析 三 故障解决 四 原理探讨 五 小结 文/温国兵 一 引子 研发反应,有台从库和主库不同步.由于业务读操作是针对从库的,数据不同步必定会带来数据的不一致,业务获取的 ...

  2. WTL汉化版

    基于 WTL90_4060 仅汉化了Windows部分,CE和Mobile未汉化 AppWizard和rc文件已全部汉化 如果不需要汉化则将所有的2052目录删除即可 如有问题可以给我留言 点我下载

  3. SonarQube 配置 LDAP(AD域)

    安装插件 1.下载 LDAP Plugin 插件,地址:https://docs.sonarqube.org/display/SONARQUBE67/LDAP+Plugin2.将下载的插件,放到 SO ...

  4. Python基础知识:while循环

    1.在循环中使用continue输出1-10之间的奇数 num=0 while num <10: num += 1 if num %2 == 0: #--%--运算符,相除返回余数 contin ...

  5. 【hexo】01安装

    什么是 Hexo? Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页. 安装前提 安装 Hexo 相当简 ...

  6. MySQL之慢查询日志分析

    在MySQL命令行中查看慢查询日志是否打开了: mysql> show variables like '%slow_query%'; +---------------------------+- ...

  7. 05LaTeX学习系列之---TeX的命令行操作

    目录 目录 前言 (一)查看版本号 1.查看TeX的版本号 2.查看LaTeX的版本号 3.查看XeLeTeX的版本号 (二)更行版本 (三)用命令行来编译.tex文件 1.用LaTeX编译 2.用X ...

  8. Beta阶段总结博客(麻瓜制造者)

    Beta冲刺过程中各个成员的贡献百分比: 成员 贡献值 邓弘立 15% 符天愉 14% 江郑 14% 刘双玉 14% 肖小强 13% 李佳铭 11% 汪志彬 11% 伍杰麟 8% 项目的发布说明 本版 ...

  9. mysql删除数据左右空格

    select trim(字段) from 表 删除左右空格 select ltrim(字段) from 表 删除左空格 select rtrim(字段) from 表 删除右空格

  10. centos7下安装docker(8.3容器的常用操作)

    yu我们之前已经学习了如何运行容器docker run,也学习了如何进入容器docker attach和docker exec,下面我们来学习容器的其他操作: stop/start/restart 1 ...