Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

SendingEventsFromTheClientToTheServerUsingRPC.asciidoc 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ---
  2. title: Sending Events From The Client To The Server Using RPC
  3. order: 37
  4. layout: page
  5. ---
  6. [[sending-events-from-the-client-to-the-server-using-RPC]]
  7. Sending events from the client to the server using RPC
  8. ------------------------------------------------------
  9. An RPC mechanism can be used to communicate from the client to the
  10. server. In effect, the client can call methods that are executed by the
  11. server component. The server component can then take appropriate action
  12. - e.g updating the shared state or calling event listeners.
  13. To set up client-server RPC we need to create one interface defining the
  14. RPC methods, and then make use of that interface on both the client and
  15. the server. Place the `MyComponentServerRpc` interface in the client
  16. package:
  17. [source,java]
  18. ....
  19. package com.example.mycomponent.client;
  20. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  21. import com.vaadin.terminal.gwt.client.communication.ServerRpc;
  22. public interface MyComponentServerRpc extends ServerRpc {
  23. public void clicked(MouseEventDetails mouseDetails);
  24. }
  25. ....
  26. Note that the RPC methods can not have return values. In this example,
  27. we pass `MouseEventDetails` to get a more complete example, but you
  28. could pass almost any (or no) parameters.
  29. In the server side `MyComponent` we need to implement the interface, and
  30. register it for use:
  31. [source,java]
  32. ....
  33. package com.example.mycomponent;
  34. import com.example.mycomponent.client.MyComponentServerRpc;
  35. import com.example.mycomponent.client.MyComponentState;
  36. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  37. import com.vaadin.ui.AbstractComponent;
  38. public class MyComponent extends AbstractComponent {
  39. private int clickCount = 0;
  40. private MyComponentServerRpc rpc = new MyComponentServerRpc() {
  41. public void clicked(MouseEventDetails mouseDetails) {
  42. clickCount++;
  43. setText("You have clicked " + clickCount + " times");
  44. }
  45. };
  46. public MyComponent() {
  47. registerRpc(rpc);
  48. }
  49. /* Previous code commented out for clarity:
  50. @Override
  51. public MyComponentState getState() {
  52. return (MyComponentState) super.getState();
  53. }
  54. public void setText(String text) {
  55. getState().text = text;
  56. }
  57. public String getText() {
  58. return getState().text;
  59. }
  60. */
  61. }
  62. ....
  63. Here we react to the RPC call by incrementing a counter. We do not make
  64. use of the `MouseEventDetails` (yet). Notice the *important call to
  65. `registerRpc()`* in the added constructor.
  66. In the client side `MyComponentConnector`, we use `RpcProxy` to get an
  67. implementation of the RPC interface, and call the `clicked()` method
  68. when the widget is clicked:
  69. [source,java]
  70. ....
  71. package com.example.mycomponent.client;
  72. // imports removed for clarity
  73. import com.vaadin.terminal.gwt.client.communication.RpcProxy;
  74. @Connect(MyComponent.class)
  75. public class MyComponentConnector extends AbstractComponentConnector {
  76. MyComponentServerRpc rpc = RpcProxy
  77. .create(MyComponentServerRpc.class, this);
  78. public MyComponentConnector() {
  79. getWidget().addClickHandler(new ClickHandler() {
  80. public void onClick(ClickEvent event) {
  81. final MouseEventDetails mouseDetails = MouseEventDetailsBuilder
  82. .buildMouseEventDetails(event.getNativeEvent(),
  83. getWidget().getElement());
  84. rpc.clicked(mouseDetails);
  85. }
  86. });
  87. }
  88. /* Previous code commented out for clarity:
  89. @Override
  90. protected Widget createWidget() {
  91. return GWT.create(MyComponentWidget.class);
  92. }
  93. @Override
  94. public MyComponentWidget getWidget() {
  95. return (MyComponentWidget) super.getWidget();
  96. }
  97. @Override
  98. public MyComponentState getState() {
  99. return (MyComponentState) super.getState();
  100. }
  101. @OnStateChange("text")
  102. void updateText() {
  103. getWidget().setText(getState().text);
  104. }
  105. */
  106. }
  107. ....
  108. Notice that most of the code is for attaching the click handler and
  109. creating the `MouseEventDetails`, the code for the actual RPC is quite
  110. minimal.
  111. Compile the widgetset, and the label text should be updated with the
  112. click count whenever you click it (remember that the counting is done on
  113. the server-side).
  114. Finally, note that you can use multiple RPC interfaces in one component,
  115. allowing for better code separation and reuse.
  116. You can do the same thing in the other direction, see the article about
  117. server to client RPC for details.