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点了,收工睡觉;先看看效果吧
评论
GridSearchPlugin gridSearch = new GridSearchPlugin(GridSearchPlugin.TOP);
gridSearch.init(grid);
gridSearch.setMode(GridSearchPlugin.LOCAL);
grid.addPlugin(gridSearch);
并且做了分页.现在只能搜索当前页.而不能搜索所有页.请问有无人知道怎么解决吗?
不支持ext2.1?
支持ext2.1,这个demo就是用gwt和ext2.1做的
估计不支持2.1的新增的功能。
还是不是很清楚gwt-ext的机制。
我看最终的页面又用到了ext的js,和gxt不太一样。
但是gwt-ext的主创人员离开了。不知道日后会怎样了
不支持ext2.1?
支持ext2.1,这个demo就是用gwt和ext2.1做的
不支持ext2.1?
不知道所说的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>
不知道所说的ant编译方法那里有下载完整的?
可否具体一点,没有明白你的意思
编译速度有些慢,项目部署后速度还可以,至少比ext要快的多。
何况这个GWT也不能很好的结合JSp
可以在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模式还是运行编译都不会出现内存溢出的情况.
编译提示也是GWT在提示内存溢出,解决Eclipse能解决问题吗?我的gwtoutput内容
总是没有相关的HTML文件生成,真是郁闷!
多谢啊!!!
所以每次都要把所有文件编译一次.
不过GWT默认是每个浏览器生成一个js文件的,用于解决跨浏览器的问题.
所以在开发的时候最好指定只生成一个浏览器的js,
这样可以节省一点点时间:
在 XXX.gwt.xml里面加上一句:
<set-property name="user.agent" value="gecko"/>
value的值可以是各浏览器,例如:ie6,safari,opera,gecko
gecko是firefox的内核
至于内存溢出的问题可以加大eclipse的内存使用上限.
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 49484 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
我的相册
共 12 张
最近加入圈子
最新评论
-
Gwt-Ext学习笔记之基础篇
请问:为什么我的没有这项出来呢?在 Configurations中选择 Cypa ...
-- by bh_nesta -
Gwt-Ext学习笔记之中级篇
我增加了一个搜索的.代码入下: GridSearc ...
-- by pindai -
Birt报表金额大小写转换
hmm,推荐把这样的代码作为一个函数保存在一个js文件里。见BIRT英文论坛ht ...
-- by RogerTu -
请大家给我估价,上海地区 ...
chinata 写道 julycn 写道: 有两三家企业给了offer,(o ...
-- by vwpolo -
请大家给我估价,上海地区 ...
你太精通了,全被你研究透了,是的无价!
-- by soleghost






评论排行榜