`
gwbasic
  • 浏览: 56240 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

试用GWT-1

    博客分类:
  • gwt
阅读更多
Google Web Toolkit终于完全开源了,开心!
写Javascript太烦了,早就想换了,这次gwt开源,正是一个好机会.
第一天试用,便测试了与Spring的整合,参考了http://gwt-widget.sourceforge.net/中的GWT Server Library (GWT-SL) 0.1.2 Released 中的 GWTSpringController.java
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class GWTSpringController extends RemoteServiceServlet implements ServletContextAware, Controller {

	private ServletContext servletContext;

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	@Override
	public ServletContext getServletContext() {
		return servletContext;
	}

	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		doPost(request, response);
		return null;
	}
}


客户端代码:
// File LoginForm.java
import com.google.gwt.user.client.rpc.IsSerializable;

public class LoginForm implements IsSerializable {
	private String usercode;
	private String pwd;
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getUsercode() {
		return usercode;
	}
	public void setUsercode(String usercode) {
		this.usercode = usercode;
	}
}

// File LoginSvr.java
import com.google.gwt.user.client.rpc.RemoteService;
import com.connstar.client.form.LoginForm;

public interface LoginSvr extends RemoteService {
	Integer queryUserPwd(LoginForm command) throws Exception;
}

// File LoginSvrAsync.java
import com.connstar.client.form.LoginForm;
import com.google.gwt.user.client.rpc.AsyncCallback;

public interface LoginSvrAsync {
	void queryUserPwd(LoginForm form, AsyncCallback callback);
}

// 远程调用代码
  Button rpcButton = new Button("RPC");
  rpcButton.addClickListener(new ClickListener() {
		public void onClick(Widget sender) {
			LoginSvrAsync loginService = (LoginSvrAsync) GWT.create(LoginSvr.class);
			ServiceDefTarget endpoint = (ServiceDefTarget) loginService;
			endpoint.setServiceEntryPoint("http://127.0.0.1:8080/test/gwt/login");
			AsyncCallback callback = new AsyncCallback() {
				public void onSuccess(Object result) {
					Window.alert("success:" + result.toString());
				}

				public void onFailure(Throwable caught) {
					Window.alert("failure:" + caught.toString());
				}
			};

			LoginForm form = new LoginForm();
			form.setUsercode("用户名");
			form.setPwd("密码");
			loginService.queryUserPwd(form, callback);
		}
	});            


试用成功.

其中注意的是在服务器端也要包含 com.google.gwt.user.client.rpc.core这个包
,是实例化Character, Byte, Short, Integer, Long, Boolean, Float, or Double,ArrayList,HashMap等类用的.

A type is serializable and can be used in a service interface if it
    * is primitive, such as char, byte, short, int, long, boolean, float, or double;
    * is String, Date, or a primitive wrapper such as Character, Byte, Short, Integer, Long, Boolean, Float, or Double;
    * is an array of serializable types (including other serializable arrays);
    * is a serializable user-defined class; or
    * has at least one serializable subclass

Java源文件建议使用utf-8编码,否则会乱码.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics