GWT1.7学习之后台传送pojo到前台页面.RPC调用

简介: 1,在GWT中可以直接将一个pojo对象由server传送到client.   比如: 一个序列化的User对象.是JDO保存到数据库的POJO. 注意:这个POJO必须放到client包下面.否则会有问题.   package com.i.web.desktop.client; import javax.jdo.annotations.IdGeneratorStrate

1,在GWT中可以直接将一个pojo对象由server传送到client.

 

比如:

一个序列化的User对象.是JDO保存到数据库的POJO.

注意:这个POJO必须放到client包下面.否则会有问题.

 

package com.i.web.desktop.client;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.gwt.user.client.rpc.IsSerializable;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class User implements IsSerializable

 {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;/* 用户主键 */

	@Persistent
	private String userName;/* 用户名 */

	@Persistent
	private String loginName;/* 登录名 */

	@Persistent
	private String passwd;/* 密码. */

	@Persistent
	private String email;/* 电子邮件. */

	public User(Long id, String userName, String loginName, String passwd,
			String email) {

		super();
		this.id = id;
		this.userName = userName;
		this.loginName = loginName;
		this.passwd = passwd;
		this.email = email;
	}
	public User() {
	}

        /*省略getset方法.*/
}
 

 

 这里要有默认的构造函数.

在client包下面.

并且要实现接口implements IsSerializable

这样才可以将这个序列化的对象传送到客户端.

 

2,这里可以传送的参数只有基本类型,基本类型的分装类.和实现序列化的类.

 

所以这里可以传入的参数没有List,Set之类的.虽然可以使用

/**
     * @gwt.typeArgs <com.i.web.desktop.client.User>
     */

注释将返回的list类型设置下.但貌似还有点问题.调试成功.

所以就用了User[] 的数组进行list的传递.

 

代码也是要改下.

 

public User[] getAllUser() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			javax.jdo.Query query = pm.newQuery(User.class);
			List<User> results = (List<User>) query.execute("Smith");
			User[] users = new User[results.size()];
			results.toArray(users);
			return users;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			pm.close();
		}
	}
 

这里直接使用了一个List下面的方法results.toArray(users);将list转换成User[] 数组了.

 

这样测试是可以实验成功的.

 

final UserActionAsync userAction = GWT.create(UserAction.class);

userAction.getAllUser(new AsyncCallback<User[]>() {

			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			public void onSuccess(User[] result) {
				// TODO Auto-generated method stub
				System.out.println("get size:"+result.length);
				for (int j = 0; j < result.length; j++) {
					System.out.print(result[j].getUserName());
					System.out.print(result[j].getId()+"");
					System.out.println(result[j].getPasswd());
				}
			}
		});

 在client进行调用.可以显示User[] 数组.

 

总结:

1,可以把一个JDO对象当作一个参数传从server传到client.

前提是这JDO对象在client包下.实现了IsSerializable 接口,有自己的构造函数.

2,没有能将List直接传到client.而是用List.toArray()转换之后在进行操作.

 

2在Client进行分层设计.

可以将页面变成一个一个小的面板.每一个面板里面都可以做为一个单独的类.

 

在这个单独的类里面进行操作.

比如在初始化页面:

调用登录面板:

 

public void onModuleLoad() {
		System.out.println("系统加载的时候显示....");
		LoginPanel loginPanel = new LoginPanel();
		loginPanel.show();
		loginPanel.center();
		/*在ModuleLoad()方法调用的时候,初始化登录按钮.*/

 登录面板是另一个java类.

 

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

public class LoginPanel extends DialogBox {

	private final UserActionAsync userAction = GWT.create(UserAction.class);
	public LoginPanel() {
		setText("用户登录");

		final Grid grid = new Grid(3, 3);
		grid.setWidget(0, 0, new Label("登录名:"));
		final TextBox loginName = new TextBox();
		grid.setWidget(0, 1, loginName);
		grid.setWidget(1, 0, new Label("密码:"));
		final PasswordTextBox passwd = new PasswordTextBox();
		grid.setWidget(1, 1, passwd);

		final Button loginButton = new Button("登录", new ClickHandler() {
			public void onClick(ClickEvent event) {
				// hide();
				userAction.userLogin(loginName.getValue(), passwd.getValue(), new AsyncCallback<String>() {
					public void onSuccess(String result) {
						System.out.println("sssssssss" +result);
						if("".equals(result)){
							Window.alert("用户名密码错误!");
						}else{
							hide();
							RootPanel.get().add(new Label("登录成功."));
							ForumsPanel forumsPanel = new ForumsPanel();
							RootPanel.get().add(forumsPanel);
						}
					}
					public void onFailure(Throwable caught) {
						
					}
				});
			}
		});
		grid.setWidget(2, 0, loginButton);
		final Button RegisterButton = new Button("注册", new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
				RegisterUserPanel registerUserPanel = new RegisterUserPanel();
				registerUserPanel.show();
				registerUserPanel.center();
			}
		});
		grid.setWidget(2, 1, RegisterButton);
		setWidget(grid);
	}
}

 在登录面板下面还有一个注册面板.

 

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

public class RegisterUserPanel extends DialogBox {

	private final UserActionAsync userAction = GWT.create(UserAction.class);

	public RegisterUserPanel() {
		setText("用户注册");

		final Grid grid = new Grid(5, 5);
		grid.setWidget(0, 0, new Label("用户名:"));
		final TextBox userName = new TextBox();
		grid.setWidget(0, 1, userName);
		grid.setWidget(1, 0, new Label("登录名:"));
		final TextBox loginName = new TextBox();
		grid.setWidget(1, 1, loginName);
		grid.setWidget(2, 0, new Label("密码:"));
		final PasswordTextBox passwd = new PasswordTextBox();
		grid.setWidget(2, 1, passwd);
		grid.setWidget(3, 0, new Label("email:"));
		final TextBox email = new TextBox();
		grid.setWidget(3, 1, email);

		final Button RegisterButton = new Button("返回", new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
				LoginPanel loginPanel = new LoginPanel();
				loginPanel.show();
				loginPanel.center();
			}
		});

		final Button loginButton = new Button("注册", new ClickHandler() {
			public void onClick(ClickEvent event) {
				// hide();
				String errorMsg = "";
				if ("".equals(userName.getValue())) {
					errorMsg += "用户名,";
				}
				if ("".equals(loginName.getValue())) {
					errorMsg += "登录名,";
				}
				if ("".equals(passwd.getValue())) {
					errorMsg += "密码,";
				}
				if ("".equals(email.getValue())) {
					errorMsg += "email,";
				}
				if ("".equals(errorMsg)) {
					userAction.saveUser(userName.getValue(), loginName
							.getValue(), passwd.getValue(), email.getValue(),
							new AsyncCallback<Void>() {
								public void onSuccess(Void result) {
									Window.alert("注册成功,请重新登录.");
									hide();
									LoginPanel loginPanel = new LoginPanel();
									loginPanel.show();
									loginPanel.center();
								}

								public void onFailure(Throwable caught) {
								}
							});
				} else {
					Window.alert(errorMsg + "不能为空.");
				}
			}

		});
		grid.setWidget(4, 0, loginButton);
		grid.setWidget(4, 1, RegisterButton);
		setWidget(grid);
	}
}
 

注册成功之后将信息保存到数据库.然后再跳回到登录页面.

 

这样就用GWT实现了两个页面的切换.

 



 

显示最简单的两个登录注册面板.

 

代码还在整理当中.还想添加些其他的功能.

 

3.关于GoogleApps

登录:http://appengine.google.com/

 

登录之后你可以注册 10 个 应用的名称:

 



 然后在上传apps的代码的时候指定 名称的名字:

 

 

这样你就可以在一个帐号下面同时管理 10 个应用:

说明:这个名称不一定是你自己的邮件的名字.当然可以是相同的.也可以是不同的.

先申请了先得到.

 

不用也先占着几个好名字吧.做自己的网站的名字.

 


google的apps还是很吸引我的想做点东西.从最简单的开始.从实践开始.

 

同时希望能够和大家一起学习进步.有不对的地方还请多指教.

目录
相关文章
|
3月前
|
XML 存储 JSON
从零开始学习 RPC 与 Protobuf
在数据密集型应用领域,Google 开发的 Protobuf 作为一种高效数据编码方式而广受欢迎。它胜任于 JSON 及 XML 对比,不仅在体积和速度上表现出色,而且其结构化方式优化了网络传输中的性能。简而言之,Protobuf 是将复杂数据结构编码成二进制流的手段,并能够轻松将这些流再还原回原始数据格式。
|
6月前
|
中间件 Go 数据处理
Go语言学习 - RPC篇:gRPC-Gateway定制mux选项
通过上一讲,我们对gRPC的拦截器有了一定的认识,也能定制出很多通用的中间件。 但在大部分的业务系统中,我们面向的还是HTTP协议。那么,今天我们就从gRPC-Gateway的mux选项出发,一起来看看一些很实用的特性。
92 0
|
6月前
|
编解码 中间件 Go
Go语言学习 - RPC篇:gRPC拦截器剖析
我们在前几讲提到过,优秀的RPC框架都提供了`middleware`的能力,可以减少很多重复代码的编写。在gRPC-Gateway的方案里,包括了两块中间件的能力: 1. gRPC中的`ServerOption`,是所有gRPC+HTTP都会被处理 2. gRPC-Gateway中的`ServeMuxOption`,只有HTTP协议会被处理 今天,我们先关注共同部分的`ServerOption`,它提供的能力最为全面,让我们一起了解下。
45 0
|
6月前
|
存储 JSON Go
Go语言学习 - RPC篇:深入gRPC-Gateway-探索常用数据类型
今天,我们先迈出第一步:探索RPC服务中的数据类型。掌握常见的数据类型,灵活地运用到接口设计中,能帮助我们快速地提供优雅的接口类服务。
45 0
|
6月前
|
Go API 开发者
Go语言学习 - RPC篇:gRPC-Gateway示例代码概览
gRPC-Gateway是gRPC生态的一环,用于对HTTP协议的扩展,是一套高性能、高扩展的开源RPC框架。 因此,要掌握gRPC-Gateway,必须要对gRPC有一定的基础,才能明白它的定位与价值。
44 0
|
6月前
|
JSON 中间件 Go
Go语言学习 - RPC篇:gin框架的基础能力剖析
gin是非常流行的一款HTTP框架。相较于原生的HTTP server,gin有很多改进点,主要在于3点: 1. 上手简单,开发思路与原生HTTP基本一致 2. 引入多个工具库,提高了开发效率 3. 生态丰富,有许多开源的组件 围绕着gin框架,我们将展开今天的话题。
105 2
Go语言学习 - RPC篇:gin框架的基础能力剖析
|
6月前
|
JSON Go 开发工具
Go语言学习 - RPC篇:理解标准库HTTP的hander实现逻辑
在Go语言中,常见的RPC包括HTTP/gRPC/Thrift等,但绝大多数的开发场景仍是基于HTTP。本文对RPC的讨论,主要是基于HTTP的场景。
40 0
|
Dubbo 应用服务中间件
Netty实现简单RPC调用
我们知道Dubbo是一个RPC框架,那RPC框架需要实现什么?需要实现的是调用远程服务和本地服务一样方便,同时提高调用远程服务的性能。而服务端和客户端之间的关系,其实就是一个生产和消费的关系。
92 0
Netty实现简单RPC调用
|
监控 前端开发 Java
Rpc 调用监控 | 学习笔记
快速学习 Rpc 调用监控
332 0
Rpc 调用监控 | 学习笔记
|
消息中间件 Dubbo 网络协议
rabbitmq高并发RPC调用,你Get到了吗?
rabbitmq高并发RPC调用,你Get到了吗?
710 0
rabbitmq高并发RPC调用,你Get到了吗?