Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
GWT RPC Spring Scaffolding
package org.filippodeluca.gwt.examples.contacts;
import com.google.gwt.user.client.rpc.IsSerializable;
public class Contact implements IsSerializable {
private String name;
private String email;
public Contact(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
package org.filippodeluca.gwt.examples.contacts;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import java.util.ArrayList;
@RemoteServiceRelativePath("ContactService")
public interface ContactService extends RemoteService {
ArrayList<Contact> findByName(String name);
}
package org.filippodeluca.gwt.examples.contacts;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.ArrayList;
public interface ContactServiceAsync extends RemoteService {
void findByName(String name, AsyncCallback<ArrayList<Contact>> callback);
}
package org.filippodeluca.gwt.examples.contacts;
import org.filippodeluca.gwt.rpc.GWTService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
/**
* @author Filippo De Luca
*/
@Service("contactService")
public class ContactServiceImpl extends GWTService implements ContactService {
@Override
@Transactional(readOnly = true)
public ArrayList<Contact> findByName(String name) {
// Implementation calling the DB or whatever do you want
return new ArrayList<Contact>();
}
}
package org.filippodeluca.gwt.examples.contacts;
import org.filippodeluca.gwt.rpc.GWTServiceServlet;
import java.util.ArrayList;
/**
* @author Filippo De Luca
*/
public class ContactServiceServlet extends GWTServiceServlet implements ContactService {
private ContactService delegate;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
delegate = getRequiredWebApplicationContext(config.getServletContext()).getBean(ContactService.class);
}
@Override
public ArrayList<Contact> findByName(String name) {
// Call the delegate implementation
return delegate.findByName(name);
}
}
package org.filippodeluca.gwt.rpc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Base class to develop a GWT service.
* Exposes the Request and Response to the subclasses.
*
* @author Filippo De Luca
*/
public abstract class GWTService {
/**
* Return the current HTTP request.
* @return
*/
protected HttpServletRequest currentRequest() {
return GWTThreadLocals.instance().getRequest();
}
protected HttpServletResponse currentResponse() {
return GWTThreadLocals.instance().getResponse();
}
}
package org.filippodeluca.gwt.rpc;
/**
* It set the GWT thread locals to be available from the services.
*
* @author Filippo De Luca
*/
public abstract class GWTServiceServlet extends RemoteServiceServlet {
@Override
protected void onBeforeRequestDeserialized(String serializedRequest) {
super.onBeforeRequestDeserialized(serializedRequest);
setGwtThreadLocals();
}
protected void setGwtThreadLocals() {
GWTThreadLocals.instance().setRequest(getThreadLocalRequest());
GWTThreadLocals.instance().setResponse(getThreadLocalResponse());
}
}
package org.filippodeluca.gwt.rpc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Singleton class to pass Request and response from servlet to service.
*
* @author Filippo De Luca
*/
public final class GWTThreadLocals {
private static final GWTThreadLocals INSTANCE = new GWTThreadLocals();
protected ThreadLocal<HttpServletRequest> perThreadRequest;
protected ThreadLocal<HttpServletResponse> perThreadResponse;
private GWTThreadLocals() {
perThreadRequest = new ThreadLocal<HttpServletRequest>();
perThreadResponse = new ThreadLocal<HttpServletResponse>();
}
public static GWTThreadLocals instance() {
return INSTANCE;
}
public void setRequest(HttpServletRequest request) {
perThreadRequest.set(request);
}
public HttpServletRequest getRequest() {
return perThreadRequest.get();
}
public void setResponse(HttpServletResponse response) {
perThreadResponse.set(response);
}
public HttpServletResponse getResponse() {
return perThreadResponse.get();
}
}

OskyFJ commented Jul 11, 2016

ContactServiceAsync.java:

void findByName(String name, AsyncCallback"<"ArrayList callback); ??

"AsyncCallback<" (>?)

Owner

filosganga commented Dec 9, 2016

@OskyFJ Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment