zoukankan      html  css  js  c++  java
  • 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

    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>
  • 相关阅读:
    Android随笔使用ViewPager实现简单地图片的左右滑动切换
    简单地Android中图片的三级缓存机制
    Java中怎么遍历map中value值 (转载)
    创建可执行的jar包
    写出优秀的java代码(转载)
    [JS脚本]ajax status 错误定义
    jsonlib 之jsonConfig详细使用(转载写的不错)
    Driver not found Error when sonar server is just not running
    使用Maven生成manifest
    ECMAScript 面向对象技术
  • 原文地址:https://www.cnblogs.com/dyh004/p/5456692.html
Copyright © 2011-2022 走看看