<? xml version ="1.0" encoding ="UTF-8" ? > < module rename-to ='rpc_command' > < inherits name ='com.google.gwt.user.User' /> < inherits name ="com.google.gwt.inject.Inject" /> < inherits name ="net.customware.gwt.dispatch.Dispatch" /> < entry-point class ='net.pimgwt.client.RpcCommandEntryPoint' /> < source path ='client' /> </ module > * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > < module rename-to ='rpc_command' > < inherits name ='com.google.gwt.user.User' /> < inherits name ="com.google.gwt.inject.Inject" /> < inherits name ="net.customware.gwt.dispatch.Dispatch" /> < entry-point class ='net.pimgwt.client.RpcCommandEntryPoint' /> < source path ='client' /> </ module > * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > < module rename-to ='rpc_command' > < inherits name ='com.google.gwt.user.User' /> < inherits name ="com.google.gwt.inject.Inject" /> < inherits name ="net.customware.gwt.dispatch.Dispatch" /> < entry-point class ='net.pimgwt.client.RpcCommandEntryPoint' /> < source path ='client' /> </ module > * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > < module rename-to ='rpc_command' > < inherits name ='com.google.gwt.user.User' /> < inherits name ="com.google.gwt.inject.Inject" /> < inherits name ="net.customware.gwt.dispatch.Dispatch" /> < entry-point class ='net.pimgwt.client.RpcCommandEntryPoint' /> < source path ='client' /> </ module > * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > < module rename-to ='rpc_command' > < inherits name ='com.google.gwt.user.User' /> < inherits name ="com.google.gwt.inject.Inject" /> < inherits name ="net.customware.gwt.dispatch.Dispatch" /> < entry-point class ='net.pimgwt.client.RpcCommandEntryPoint' /> < source path ='client' /> </ module > * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > < module rename-to ='rpc_command' > < inherits name ='com.google.gwt.user.User' /> < inherits name ="com.google.gwt.inject.Inject" /> < inherits name ="net.customware.gwt.dispatch.Dispatch" /> < entry-point class ='net.pimgwt.client.RpcCommandEntryPoint' /> < source path ='client' /> </ module > * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > < module rename-to ='rpc_command' > < inherits name ='com.google.gwt.user.User' /> < inherits name ="com.google.gwt.inject.Inject" /> < inherits name ="net.customware.gwt.dispatch.Dispatch" /> < entry-point class ='net.pimgwt.client.RpcCommandEntryPoint' /> < source path ='client' /> </ module > * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > < module rename-to ='rpc_command' > < inherits name ='com.google.gwt.user.User' /> < inherits name ="com.google.gwt.inject.Inject" /> < inherits name ="net.customware.gwt.dispatch.Dispatch" /> < entry-point class ='net.pimgwt.client.RpcCommandEntryPoint' /> < source path ='client' /> </ module > * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > < module rename-to ='rpc_command' > < inherits name ='com.google.gwt.user.User' /> < inherits name ="com.google.gwt.inject.Inject" /> < inherits name ="net.customware.gwt.dispatch.Dispatch" /> < entry-point class ='net.pimgwt.client.RpcCommandEntryPoint' /> < source path ='client' /> </ module > * This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" ? > < module rename-to ='rpc_command' > < inherits name ='com.google.gwt.user.User' /> < inherits name ="com.google.gwt.inject.Inject" /> < inherits name ="net.customware.gwt.dispatch.Dispatch" /> < entry-point class ='net.pimgwt.client.RpcCommandEntryPoint' /> < source path ='client' /> </ module > * This source code was highlighted with Source Code Highlighter .
* This source code was highlighted with Source Code Highlighter .
- package net.pimgwt.client.rpc;
- import net.customware.gwt.dispatch.shared.Action;
- @SuppressWarnings ( "serial" )
- public class SingleRequestAction implements Action <SingleRequestResult> {
- private String param;
- public SingleRequestAction () {
- }
- public SingleRequestAction (String param) {
- this .param = param;
- }
- public String getParam () {
- return this .param;
- }
- }
* This source code was highlighted with Source Code Highlighter .
- package net.pimgwt.client.rpc;
- import net.customware.gwt.dispatch.shared.Result;
- @SuppressWarnings ( "serial" )
- public class SingleRequestResult implements Result {
- private String resultMessage;
- public SingleRequestResult () {}
- public SingleRequestResult (String resultMessage) {
- this .resultMessage = resultMessage;
- }
- public String getResultMessage () {
- return this .resultMessage;
- }
- }
* This source code was highlighted with Source Code Highlighter .
- package net.pimgwt.server;
- import net.customware.gwt.dispatch.server.service.DispatchServiceServlet;
- import com.google.inject.servlet.ServletModule;
- public class DispatcherServletModule extends ServletModule {
- @Override
- protected void configureServlets () {
- serve ( "/ rpc_command / dispatch" ) .with (DispatchServiceServlet. class );
- }
- }
DispatcherServletModule
class is ServletModule
. It rewrites the parent method configureServlets()
, which matches the RPC-URL of the servlet manager implementation that is provided by GWT-Dispatch. By default, the URL that will be listened to by the dispatcher is based on the application_name / dispatch scheme.SingleRequestAction
command):* This source code was highlighted with Source Code Highlighter .
- package net.pimgwt.server;
- import net.customware.gwt.dispatch.server.ActionHandler;
- import net.customware.gwt.dispatch.server.ExecutionContext;
- import net.customware.gwt.dispatch.shared.ActionException;
- import net.pimgwt.client.rpc.SingleRequestAction;
- import net.pimgwt.client.rpc.SingleRequestResult;
- public class SingleRequestHandler implements ActionHandler <SingleRequestAction, SingleRequestResult> {
- @Override
- public SingleRequestResult execute (SingleRequestAction action, ExecutionContext
- context) throws ActionException {
- return new SingleRequestResult ( "You are entered:" + action.getParam ());
- }
- @Override
- public Class <SingleRequestAction> getActionType () {
- return SingleRequestAction. class ;
- }
- @Override
- public void rollback (SingleRequestAction action, SingleRequestResult result,
- ExecutionContext context) throws ActionException {}
- }
SingleRequestAction
and SingleRequestResult
respectively. The ActionHandler
interface also ActionHandler
the implementation class to provide the execute(), getActionType()
and rollback()
methods, whose names speak for themselves. In the code above, for such a simple command as the SingleRequestAction
the rollback action in case of failure is simply left blank. Nothing to roll back.execute()
method execution is a SingleRequestResult
object, to which we simply write the text of the response that will be passed to the caller (client) side.getActionType()
method should return a reference to the class of the command to which the handler is attached. This is necessary so that the dispatcher can correctly call the necessary handler, and not some other one.* This source code was highlighted with Source Code Highlighter .
- package net.pimgwt.server;
- import net.customware.gwt.dispatch.server.guice.ActionHandlerModule;
- public class RpcCommandHandlerModule extends ActionHandlerModule {
- @Override
- protected void configureHandlers () {
- bindHandler (SingleRequestHandler. class );
- //. . .
- }
- }
GuiceServletContextListener
class, which will “listen” to what is happening from the outside and react when the / rpc_command / dispatch request comes from the client and start the handler of the corresponding command:* This source code was highlighted with Source Code Highlighter .
- package net.pimgwt.server;
- import com.google.inject.Guice;
- import com.google.inject.Injector;
- import com.google.inject.servlet.GuiceServletContextListener;
- public class RpcCommandGuiceConfig extends GuiceServletContextListener {
- @Override
- protected Injector getInjector () {
- return Guice.createInjector ( new RpcCommandHandlerModule (), new DispatcherServletModule ());
- }
- }
GuiceServletContextListener
class GuiceServletContextListener
provided by the Guice framework as a means of integrating it with Java Servlets. The code above will perform all the necessary injections (injects) in the right places. Thus, we have the GWT-Dispatch and Guice integration chain with Servlets closed.* This source code was highlighted with Source Code Highlighter .
- <? xml version = "1.0" encoding = "UTF-8" ? >
- <! Doctype web-app
- PUBLIC "- // Sun Microsystems, Inc.//DTD Web Application 2.3 // EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd" >
- < web-app >
- < filter >
- < filter-name > guiceFilter </ filter-name >
- < filter-class > com.google.inject.servlet.GuiceFilter </ filter-class >
- </ filter >
- < filter-mapping >
- < filter-name > guiceFilter </ filter-name >
- < url-pattern > / * </ url-pattern >
- </ filter-mapping >
- < listener >
- < listener-class > net.pimgwt.server.RpcCommandGuiceConfig </ listener-class >
- </ listener >
- < welcome-file-list >
- < welcome-file > index.html </ welcome-file >
- </ welcome-file-list >
- </ web-app >
GuiceFilter
configured to filter all requests that fall on the server side of the client. Naturally, in the url-param-instructions, you can specify your URL pattern to listen. How to do this I will not say, these are obvious things and they are not related to the subject matter.DispatchAsync
. You can implement this interface as you wish, for example, as a dispatcher who can cache the results obtained earlier. For the demo project, I chose the “boxed” implementation of DefaultDispatchAsync
again from the GWT-Dispatch package.* This source code was highlighted with Source Code Highlighter .
- //. . .
- private DispatchAsync rpcDispatcher = new DefaultDispatchAsync ();
- //. . .
- @UiField Button singleValueTestButton;
- //. . .
- @UiHandler ( "singleValueTestButton" )
- public void singleValueButtonClicked (ClickEvent event ) {
- responseLable.setText ( "" );
- rpcDispatcher.execute ( new SingleRequestAction (paramTextbox.getText ()), new
- AsyncCallback <SingleRequestResult> () {
- @Override
- public void onFailure (Throwable caught) {
- responseLable.setText ( "Error occured:" +
- caught.getMessage ());
- }
- @Override
- public void onSuccess (SingleRequestResult result) {
- responseLable.setText (result.getResultMessage ());
- }
- });
- }
responseLable.setText(result.getResultMessage());
MultiRequestAction
, is implemented in the project. The result of its execution is a MultiRequestResult
, which in turn contains a list of DummyDTO
objects, which is filled in a loop in the server handler of this command.Source: https://habr.com/ru/post/94844/
All Articles