How to make an GWT server call(GWT RPC?) [closed]

吃可爱长大的小学妹 提交于 2019-11-27 06:32:57

问题


Hi I am new to gwt(java based gui). Here i am trying to make a GWT server call.

I want to save my MyBean into my Database.

Later on i have to update,delete etc..

What are the possibilities and how can I achieve this ??


回答1:


There are couple of possibilities to hit the database with GWT like RequestFactory,RPC.

Before getting started with server calls please go through,

  • GWT RPC(Which makes server calls Asynchronesly)

  • RequestFactory( Alternative to GWT-RPC for creating data-oriented services.)

After gone through the links here is a Example of How to make an RPC.

Coming to your MyBean CRUD operations,in short the simple RPC structure as below :

GWT Code <===> InterfaceAsync <===> Interface (Synchronous)<===> Server Code 

I am trying to explain with you elements it self .

The Synchronous Interface(central to the whole RPC):

import com.google.gwt.user.client.rpc.RemoteService;
public interface BeanProcessRPCInterface extends RemoteService
{
    public Mybean processMybeanRPC(Mybean  bean);
}

The ASynchronous Interface(Key part on Client):

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface BeanProcessRPCInterfaceAsync
{
    public void processMybeanRPC (Mybean bean, AsyncCallback callback);
}

Here you go with the Service(Equals to servlet) which implements "BeanProcessRPCInterface"

public class BeanProcessRPCImpl extends 
                   RemoteServiceServlet implements BeanProcessRPCInterface
{
    private static final long serialVersionUID = 1L;

    public Mybean processMybeanRPC(Mybean  bean)
    {
       //Process your widget  here (CRUD operations)

    }

**you can override doget,doPost,doDelete....etc along with your methods 
}

Map the above class in your web.xml;

     <servlet>
    <servlet-name>beanrpc</servlet-name>
    <servlet-class>com.server.BeanProcessRPCImpl</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>beanrpc</servlet-name>
    <url-pattern>/beanrpc</url-pattern>
  </servlet-mapping>

Finally in your GWT Code .Use service as below

Using in Code:

//register the service .

   private final BeanProcessRPCInterfaceAsync beanService = 
        GWT.create(BeanProcessRPCInterface.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        endpoint.setServiceEntryPoint('beanrpc');

requesting server with callback

beanService.processMybeanRPC(mybean, callback);
 AsyncCallback callback = new AsyncCallback()
    {
        public void onFailure(Throwable caught)
        {
           //Do on fail
        }

        public void onSuccess(Object result)
        {
           //Process successfully done with result (result is which you
           // returned in impl class) .
        }
    };

P.S .Beware of package structures:

BeanProcessRPCInterfaceAsync ,BeanProcessRPCInterface should be in client* package

MyBean class should be in shared* package

BeanProcessRPCImpl should be in server* package

Good Luck.



来源:https://stackoverflow.com/questions/15717089/how-to-make-an-gwt-server-callgwt-rpc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!