Spring RPC Primer Learning (3)-Get Student Object

Source: Internet
Author: User

Spring RPC passes the object.

1. New RPC Interface: Studentinterface.java

 Package Com.cvicse.ump.rpc.interfaceDefine; Import com.cvicse.ump.student.Student;  Public Interface Studentinterface {    public  Student Getstudentbyid (String ID);}

2. Create a new implementation class for the RPC interface, Studentmanager.java

 Packagecom.cvicse.ump.rpc.interfaceImp;ImportCom.cvicse.ump.rpc.interfaceDefine.StudentInterface;Importcom.cvicse.ump.student.Student; Public classStudentmanagerImplementsStudentinterface {@Override PublicStudent Getstudentbyid (String id) {Student Student=NewStudent ();        Student.setid (ID); Student.setname ("Xiaody"); Student.setage (23); Student.setaddress ("Shijiazhuang, Hebei"); returnstudent; }}

3. Configure Spring and Rpc,web.xml

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns: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 Related configurations -  <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-related configuration -  <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>

Configuration file for 4.Sping: Applicationcontext.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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 Configuration: Spring-service.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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>    <BeanID= "/studentservice"class= "Com.googlecode.jsonrpc4j.spring.JsonServiceExporter">        < Propertyname= "Service">            <Beanclass= "Com.cvicse.ump.rpc.interfaceImp.StudentManager"></Bean>        </ Property>        < Propertyname= "Serviceinterface"value= "Com.cvicse.ump.rpc.interfaceDefine.StudentInterface" />    </Bean>     <Beanclass= "Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /></Beans>

6. Writing a JSP

<%@ Pagelanguage= "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><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Spring RPC TEST</title>    <Scriptsrc= "Js/jsonrpcjs-0.1.8.min.js"></Script>    <Scripttype= "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 Test</H1>Please enter your name:<inputtype= "text"ID= "Name"size= "+"><inputtype= "button"value= "Hello"ID= "BUT1"onclick= "Ceshi ()"></Body></HTML>

Spring RPC Primer Learning (3)-Get student objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.