You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gwt-rpc.asciidoc 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ---
  2. title: RPC Calls Between Client- and Server-Side
  3. order: 6
  4. layout: page
  5. ---
  6. [[gwt.rpc]]
  7. = RPC Calls Between Client- and Server-Side
  8. Vaadin supports making Remote Procedure Calls (RPC) between a server-side
  9. component and its client-side widget counterpart. RPC calls are normally used
  10. for communicating stateless events, such as button clicks or other user
  11. interaction, in contrast to changing the shared state. Either party can make an
  12. RPC call to the other side. When a client-side widget makes a call, a server
  13. request is made. Calls made from the server-side to the client-side are
  14. communicated in the response of the server request during which the call was
  15. made.
  16. If you use Eclipse and enable the "Full-Fledged" widget in the New Vaadin Widget
  17. wizard, it automatically creates a component with an RPC stub.
  18. [[gwt.rpc.server-side]]
  19. == RPC Calls to the Server-Side
  20. RPC calls from the client-side to the server-side are made through an RPC
  21. interface that extends the [interfacename]#ServerRpc# interface. A server RPC
  22. interface simply defines any methods that can be called through the interface.
  23. For example:
  24. [source,java]
  25. ----
  26. public interface MyComponentServerRpc extends ServerRpc {
  27. public void clicked(String buttonName);
  28. }
  29. ----
  30. The above example defines a single [methodname]#clicked()# RPC call, which takes
  31. a [classname]#String# object as the parameter.
  32. You can pass the most common standard Java types, such as primitive and boxed
  33. primitive types, [classname]#String#, and arrays and some collections (
  34. [classname]#List#, [classname]#Set#, and [classname]#Map#) of the supported
  35. types. Also the Vaadin [classname]#Connector# and some special internal types
  36. can be passed.
  37. An RPC method must return void - the widget set compiler should complain if it
  38. doesn't.
  39. [[gwt.rpc.server-side.calling]]
  40. === Making a Call
  41. Before making a call, you need to instantiate the server RPC object with
  42. [methodname]#RpcProxy.create()#. This is usually done transparently by using [methodname]#getRpcProxy()#. After that, you can make calls through the
  43. server RPC interface that you defined, for example as follows:
  44. [source,java]
  45. ----
  46. @Connect(MyComponent.class)
  47. public class MyComponentConnector
  48. extends AbstractComponentConnector {
  49. public MyComponentConnector() {
  50. getWidget().addClickHandler(new ClickHandler() {
  51. public void onClick(ClickEvent event) {
  52. final MouseEventDetails mouseDetails =
  53. MouseEventDetailsBuilder
  54. .buildMouseEventDetails(
  55. event.getNativeEvent(),
  56. getWidget().getElement());
  57. MyComponentServerRpc rpc =
  58. getRpcProxy(MyComponentServerRpc.class);
  59. // Make the call
  60. rpc.clicked(mouseDetails.getButtonName());
  61. }
  62. });
  63. }
  64. }
  65. ----
  66. [[gwt.rpc.server-side.handling]]
  67. === Handling a Call
  68. RPC calls are handled in a server-side implementation of the server RPC
  69. interface. The call and its parameters are serialized and passed to the server
  70. in an RPC request transparently.
  71. [source,java]
  72. ----
  73. public class MyComponent extends AbstractComponent {
  74. private MyComponentServerRpc rpc =
  75. new MyComponentServerRpc() {
  76. private int clickCount = 0;
  77. public void clicked(String buttonName) {
  78. Notification.show("Clicked " + buttonName);
  79. }
  80. };
  81. public MyComponent() {
  82. ...
  83. registerRpc(rpc);
  84. }
  85. }
  86. ----