2008-05-14

Gwt-Ext学习笔记之中级篇

关键字: gwt-ext google web toolkit cypal studio grid

 

Gwt-Ext学习笔记之中级篇

 

一、 配置 Gwt-Ext开发环境

a.       请参照 Gwt-Ext学习笔记之基础篇

b.      此教程是在 基础篇 进级篇 的基础上做的扩展,具体细节请参照前面教程。

二、 gwtext项目上创建客户端模型文件

a.       创建模型文件 InfoList.java,内容如下

 

public class InfoList implements EntryPoint {

	public void onModuleLoad() {

		ExtElement main = Ext.get("main");

		Panel mainPanel = new Panel() {
			{
				setTitle("测试");
				setHeight(300);
				setWidth(500);
				setFrame(true);
				setHtml("<p>如果看到这些信息,说明你的环境已经配置成功了!</p>");
				setStyle("margin: 10px 0px 0px 10px;");
			}
		};

		if (main != null) {
			mainPanel.setApplyTo(main.getDOM());
			mainPanel.render("");
		} else {
			RootPanel.get().add(mainPanel);
		}
	}

}
 

b.      修改 HTML宿主页面 InfoList.html文件

                                                               i.      InfoList.html文件中加入以下代码

 

<link href="js/resources/css/ext-all.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="js/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="js/ext-all.js"></script>
 

c.       InfoList.gwt.xml文件中加入以下代码

<inherits name="com.gwtext.GwtExt"/>
 

d.      测试环境是否配置成功 ,运行方式参考 Gwt-Ext学习笔记之基础篇 ,效果如下图

三、 定义服务

a.       gwtext项目上,创建名为 InfoListAction.java远程服务接口。

b.      PostgreSQL数据库的 JDBC postgresql-8.2-505.jdbc3.jar加入到项目中(其他数据库,加入相应的 JDBC包)。

c.       远程服务的实现类,在 InfoListActionImpl.java中加入如下代码:

/**
 * @author 七月天
 *
 */
public class InfoListActionImpl extends RemoteServiceServlet implements
		InfoListAction {

	public String[][] queryData() {
		Connection conn = null;
		String[][] allInfo = null;
		try {
			Class.forName("org.postgresql.Driver");
			String connString = "jdbc:postgresql://127.0.0.1:5432/gwtext";
conn = DriverManager.getConnection(connString, "julycn", "julycn");
String sqlQuery = "select username,password,email,phone from person";
			Statement stmt = conn.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			ResultSet rst = stmt.executeQuery(sqlQuery);

			// 行数
			int rows = 0;
			if (rst.last()) {
				rows = rst.getRow();
				rst.beforeFirst();
			}

			// 表的列数
			ResultSetMetaData rsm = rst.getMetaData();
			int columns = rsm.getColumnCount();

			// 初始化输组
			allInfo = new String[rows][columns];

			int i = 0;
			while (rst.next()) {
				for (int j = 0; j < columns; j++) {
					allInfo[i][j] = rst.getString(j + 1);
				}
				i++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		return allInfo;
	}

}
 

 

四、 绑定代码

a.       定义一个远程接口类 InfoListAction.java,代码如下:

 

/**
 * @author 七月天
 * 
 */
public interface InfoListAction extends RemoteService {

	public static final String SERVICE_URI = "/InfoListAction";

	public static class Util {

		public static InfoListActionAsync getInstance() {

			InfoListActionAsync instance = (InfoListActionAsync) GWT
					.create(InfoListAction.class);
			ServiceDefTarget target = (ServiceDefTarget) instance;
			target.setServiceEntryPoint(GWT.getModuleBaseURL() + SERVICE_URI);
			return instance;
		}
	}

	public String[][] queryData();
}
 

b.      定义远程异步接口类 InfoListActionAsync.java,代码如下:

/**
 * @author 七月天
 * 
 */
public interface InfoListActionAsync {

	public void queryData(AsyncCallback callback);
}
 

 

c.       注册服务器代码,将下面的一行加入到 InfoList.gwt.xml

<servlet class="com.gwtext.julycn.server.InfoListActionImpl" path="/InfoListAction" />
 

 

五、 执行客户端调用

a.       修改模型文件 InfoList.java,代码如下:

/**
 * @author 七月天
 *
 */
public class InfoList implements EntryPoint {

	public void onModuleLoad() {
		InfoListActionAsync action = InfoListAction.Util.getInstance();
		action.queryData(new AsyncCallback() {

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

			}

			public void onSuccess(Object result) {
				Panel panel = new Panel();
				panel.setBorder(false);
				panel.setPaddings(15);

				RecordDef recordDef = new RecordDef(new FieldDef[] {
						new StringFieldDef("username"),
						new StringFieldDef("password"),
						new StringFieldDef("email"),
						new StringFieldDef("phone") });

				final GridPanel grid = new GridPanel();

				String[][] data = (String[][]) result;

				MemoryProxy proxy = new MemoryProxy(data);

				ArrayReader reader = new ArrayReader(recordDef);
				Store store = new Store(proxy, reader);
				store.load();
				grid.setStore(store);

				ColumnConfig[] columns = new ColumnConfig[] {
new ColumnConfig("用户名", "username", 160, true, null,"username"),
						new ColumnConfig("密码", "password", 45),
						new ColumnConfig("邮箱", "email", 65),
						new ColumnConfig("电话", "phone", 65) };

				ColumnModel columnModel = new ColumnModel(columns);
				grid.setColumnModel(columnModel);

				grid.setFrame(true);
				grid.setStripeRows(true);
				grid.setAutoExpandColumn("username");

				grid.setHeight(350);
				grid.setWidth(600);
				grid.setTitle("用户信息");

				Toolbar bottomToolbar = new Toolbar();
				bottomToolbar.addFill();
				bottomToolbar.addButton(new ToolbarButton("清除排序",
						new ButtonListenerAdapter() {
public void onClick(Button button, EventObject e) {
								grid.clearSortState(true);
							}
						}));
				grid.setBottomToolbar(bottomToolbar);

				panel.add(grid);

				RootPanel.get().add(panel);

			}

		});
	}
}
 

 

b.      AsyncCallback 接口定义了两个方法: onSuccess(Object result)   onFailure(Throwable caught)。必须定义一个可以实现这两个方法的类。当执行远程调用时,模型类的实例并将其传递给异步服务方法。最后,服务器端资源完成,然后调用代码中两个方法之一。成功方法的参数是接口和实现中的调用的返回值。

六、 展示效果

a.       凌晨1点了,收工睡觉;先看看效果吧

 

评论
pindai 2008-07-24
我增加了一个搜索的.代码入下:
GridSearchPlugin gridSearch = new GridSearchPlugin(GridSearchPlugin.TOP);
gridSearch.init(grid);
gridSearch.setMode(GridSearchPlugin.LOCAL);
grid.addPlugin(gridSearch);
并且做了分页.现在只能搜索当前页.而不能搜索所有页.请问有无人知道怎么解决吗?
fxbird 2008-06-25
请问,gridpanel如何刷新数据,当已经加载完数据后,想重新加载一下,我又调了一次加载的程序,结果还是原来的。
ztkx 2008-06-03
请问一下,怎怎样通过命令行建立不同的模块在一个文件夹里,并且这样的话可以相互访问么,最后能够生成一个统一的发布版本么(我的意思是,在一个文件夹里,有所有的<module>.html)
wangtiantian5566 2008-05-31
很好的学习例子,我试过了,完全OK。。很好呀
ftmouse 2008-05-29
julycn 写道
ftmouse 写道
GWT-Ext 2.0.3 has been released. Grab the distribution from the Downloads area. Further details can be found in the release notes. See the entire release announcement here. Note that GWT-Ext only supports Ext 2.0.2.

不支持ext2.1?

支持ext2.1,这个demo就是用gwt和ext2.1做的


估计不支持2.1的新增的功能。
还是不是很清楚gwt-ext的机制。
我看最终的页面又用到了ext的js,和gxt不太一样。
但是gwt-ext的主创人员离开了。不知道日后会怎样了
julycn 2008-05-29
ftmouse 写道
GWT-Ext 2.0.3 has been released. Grab the distribution from the Downloads area. Further details can be found in the release notes. See the entire release announcement here. Note that GWT-Ext only supports Ext 2.0.2.

不支持ext2.1?

支持ext2.1,这个demo就是用gwt和ext2.1做的
ftmouse 2008-05-29
GWT-Ext 2.0.3 has been released. Grab the distribution from the Downloads area. Further details can be found in the release notes. See the entire release announcement here. Note that GWT-Ext only supports Ext 2.0.2.

不支持ext2.1?
julycn 2008-05-26
zingers 写道
我觉得增大eclipse启动参数的内存不能解决gwtcompiler报的outmemory错误
不知道所说的ant编译方法那里有下载完整的?


这个是我用的ant编译文件
<?xml version="1.0" encoding="utf-8" ?>
<project name="infolist" default="all" basedir="D:\WorkSpace\Eclipse\gwtext">
	<description>
		This is build file for GWT module 'org.gwtext.info.InfoList' deployment.
		GWT Designer generates it each time when you initiate deployment.
		
		You can use this file later for manual deployment, but keep in mind, that
		classpath is hardcoded in it, so after changes in classpath you should
		regenerate build file.
	</description>

	<property name="gwt.home" value="D:\Java\gwt-windows-1.4.62" />
	<property name="gwt.module.id" value="org.gwtext.info.InfoList" />
	<property name="war.name" value="InfoList.war" />

	<target name="jars" description="Package up the module project and required projects as jars">
		<delete dir="@dot" />
		<mkdir dir="@dot/${gwt.module.id}/WEB-INF/lib" />
		<!--=== gwtext ===-->
		<jar destfile="@dot/${gwt.module.id}/WEB-INF/lib/gwtext.jar">
			<fileset dir="D:/WorkSpace/Eclipse/gwtext/src">
				<include name="**"/>
			</fileset>
			<fileset dir="D:/WorkSpace/Eclipse/gwtext/bin">
				<include name="**/*.class"/>
			</fileset>
		</jar>
		<copy todir="@dot/${gwt.module.id}/WEB-INF/lib">
			<fileset file="D:\Java\gwtext-2.0.3\gwtext.jar"/>
			<fileset file="D:\Java\lib\postgresql-8.2-504.jdbc3.jar"/>
		</copy>		
	</target>

	<target name="gwt-compile" description="Compile to JavaScript">
		<java classname="com.google.gwt.dev.GWTCompiler" fork="yes" failonerror="true" maxmemory="128m">
			<classpath>
				<pathelement location="D:/WorkSpace/Eclipse/gwtext/src" />
				<pathelement location="D:/WorkSpace/Eclipse/gwtext/bin" />
				<pathelement location="D:/Java/gwt-windows-1.4.62/gwt-user.jar" />
				<pathelement location="D:/Java/gwtext-2.0.3/gwtext.jar" />
				<pathelement location="D:/Java/lib/postgresql-8.2-504.jdbc3.jar" />
				<pathelement location="D:/Java/gwt-windows-1.4.62/gwt-dev-windows.jar" />
			</classpath>
			<arg value="-style"/>
			<arg value="OBFUSCATED"/>
			<arg value="-out" />
			<arg file="@dot" />
			<arg value="${gwt.module.id}" />
		</java>
	</target>

	<target name="web-xml">
		<mkdir dir="@dot/${gwt.module.id}/WEB-INF" />
		<echo file="@dot/${gwt.module.id}/WEB-INF/web.xml"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<web-app>
	<servlet>
		<servlet-name>org.gwtext.info.server.InfoListActionImpl</servlet-name>
		<servlet-class>org.gwtext.info.server.InfoListActionImpl</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>org.gwtext.info.server.InfoListActionImpl</servlet-name>
		<url-pattern>/InfoListAction</url-pattern>
	</servlet-mapping>

</web-app>
]]></echo>
	</target>

	<target name="war" depends="jars, gwt-compile, web-xml">
		<delete file="${war.name}" />
		<copy file="${gwt.home}/gwt-servlet.jar" todir="@dot/${gwt.module.id}/WEB-INF/lib" />
		<jar destfile="${war.name}" basedir="@dot/${gwt.module.id}" duplicate="preserve" />
		<delete dir="@dot" />
	</target>
	
	<target name="deploy" depends="war">
		<move file="${war.name}" todir="D:/" />
	</target>

	<target name="all" depends="deploy" />

</project>

zingers 2008-05-25
我觉得增大eclipse启动参数的内存不能解决gwtcompiler报的outmemory错误
不知道所说的ant编译方法那里有下载完整的?
julycn 2008-05-23
kin_me 写道
如果是用户名在上面,下面是密码和5邮箱怎么做到呢?

可否具体一点,没有明白你的意思
lubezhang 2008-05-23
使用Ext加载页面的时候会很慢的。Ext的js库还是比较大的。
kin_me 2008-05-23
如果是用户名在上面,下面是密码和5邮箱怎么做到呢?
julycn 2008-05-21
So懒 写道
感觉gwt-ext性能慢的可以。。。

编译速度有些慢,项目部署后速度还可以,至少比ext要快的多。
So懒 2008-05-20
感觉gwt-ext性能慢的可以。。。
zfswff 2008-05-20
这两天耐着性子直接学习了EXTJS,感觉也不是很难啊,结合JSON以及JSP用起来非常方便,这个GWT-EXT,使用起来问题多多啊。毕竟最终还是转换成EXt,为何不直接用EXT呢
何况这个GWT也不能很好的结合JSp
zhuliyy1983 2008-05-20
确实和wicket非常的像
eivenchan 2008-05-16
如果你是用ant编译的话,
可以在build.xml里面找到这一段:
<target name="gwt-compile" description="Compile to JavaScript">
		<java classname="com.google.gwt.dev.GWTCompiler" fork="yes" failonerror="true" maxmemory="64m">
			<classpath>
				<pathelement location="E:/workspace/test/src" />
				<pathelement location="E:/workspace/test/bin" />
				<pathelement location="E:/my/lib/GWT/gwt-1.4.61/gwt-user.jar" />
				<pathelement location="E:/my/lib/GWT/gwt-1.4.61/gwt-dev-windows.jar" />
			</classpath>
			<arg value="-style"/>
			<arg value="OBFUSCATED"/>
			<arg value="-out" />
			<arg file="@dot" />
			<arg value="${gwt.module.id}" />
		</java>
	</target>


可以修改第二行中的maxmemory="64m"的值,我一般是设置为768m
无论是运行host model模式还是运行编译都不会出现内存溢出的情况.
zfswff 2008-05-15
开始我也想到了提高eclipse内存使用上限,但是发现编译以及执行的都是GWT
编译提示也是GWT在提示内存溢出,解决Eclipse能解决问题吗?我的gwtoutput内容
总是没有相关的HTML文件生成,真是郁闷!
zfswff 2008-05-15
eivenchan

多谢啊!!!
eivenchan 2008-05-15
编译太慢是GWT的硬伤,因为js的原因,GWT不能实现单文件编译,
所以每次都要把所有文件编译一次.
不过GWT默认是每个浏览器生成一个js文件的,用于解决跨浏览器的问题.
所以在开发的时候最好指定只生成一个浏览器的js,
这样可以节省一点点时间:
在 XXX.gwt.xml里面加上一句:
<set-property name="user.agent" value="gecko"/>
value的值可以是各浏览器,例如:ie6,safari,opera,gecko
gecko是firefox的内核

至于内存溢出的问题可以加大eclipse的内存使用上限.
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

julycn
搜索本博客
我的相册
0a46256d-c842-3a98-84ca-5ee90d4e18ae-thumb
untitled.JPG
共 12 张
存档
最新评论