DWR使用手记(二)
2006-12-16 22:49
351 查看
还是前段时间写的《DWR使用手记》那是在第一次使用DWR时写的一些总结,对DWR做了一个大体的结构性的介绍,主是代码结构。文章链接在这:http://blog.csdn.net/fly29/archive/2006/03/01/613289.aspx
在这中间,经历了换公司,然后被公司发配出差到现场维护,开发一些新需求,所以没能把中间一些开发经验总结予以分享,明天开始再一一总结吧,也算是对前段时间工作的一个总结吧。
目前DWR已经升到2.0,也没能及时了解其最新特性。DWR总的来讲,是一个很好的框架,可以把js---java连接起来,方便在页面中直接调用java类中方法。这其中起最大作用的当然是bsf框架,也是apache的一个开源框架。通过在dwr.xml中进行配置也可以把javascript中的对象与java类中的model(或者叫domain对象进行转换),前几天在写代码时用到了这点,写个例子放在这。
/* 声明一个javascript对象 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
function book(name,author)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name=name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.author=author;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//写个类供页面中js调用BookTool
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class BookUtils...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void addBook(Book book) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//invoke business layer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//domain
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Book ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String author;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//getter & setter here
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
这样在页面中,声明了一个book变量,直接调用BookTool的addBook(Book b)方法即可完成操作。例:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script language="javascript">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
function add() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var b = new book("think in java","bruce");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BookUtils.addBook(b);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
当然在页面顶部要加上:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script type='text/javascript' src='dwr/interface/BookUtils.js'></script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
也要在dwr.xml中加上
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<create creator="new" javascript="BookUtils">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param name="class" value="com.test.BookTool" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</create>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
其实从上面我们可以看出,每次调用类BookTool是new一个新的对象,因此完全可以在class BookTool中把Book的所有属性加进去,即可以是这样:
public class BookUtils {
private String name;
private String author;
//getter & setter
public void addBook(Book b) {
// do sth
}
}
这样可以在页面中直接修改BookUtils的属性,然后调用其方法即可。不过为了更好使用OO,所以还是建议把域对象独立出来。
以上纯属个人拙见,期待与您交流。
QQ:236306746
在这中间,经历了换公司,然后被公司发配出差到现场维护,开发一些新需求,所以没能把中间一些开发经验总结予以分享,明天开始再一一总结吧,也算是对前段时间工作的一个总结吧。
目前DWR已经升到2.0,也没能及时了解其最新特性。DWR总的来讲,是一个很好的框架,可以把js---java连接起来,方便在页面中直接调用java类中方法。这其中起最大作用的当然是bsf框架,也是apache的一个开源框架。通过在dwr.xml中进行配置也可以把javascript中的对象与java类中的model(或者叫domain对象进行转换),前几天在写代码时用到了这点,写个例子放在这。
/* 声明一个javascript对象 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
function book(name,author)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name=name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.author=author;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//写个类供页面中js调用BookTool
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class BookUtils...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void addBook(Book book) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//invoke business layer
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
//domain
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Book ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String author;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//getter & setter here
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
这样在页面中,声明了一个book变量,直接调用BookTool的addBook(Book b)方法即可完成操作。例:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script language="javascript">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
function add() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var b = new book("think in java","bruce");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
BookUtils.addBook(b);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
当然在页面顶部要加上:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script type='text/javascript' src='dwr/interface/BookUtils.js'></script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
也要在dwr.xml中加上
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<create creator="new" javascript="BookUtils">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param name="class" value="com.test.BookTool" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</create>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
其实从上面我们可以看出,每次调用类BookTool是new一个新的对象,因此完全可以在class BookTool中把Book的所有属性加进去,即可以是这样:
public class BookUtils {
private String name;
private String author;
//getter & setter
public void addBook(Book b) {
// do sth
}
}
这样可以在页面中直接修改BookUtils的属性,然后调用其方法即可。不过为了更好使用OO,所以还是建议把域对象独立出来。
以上纯属个人拙见,期待与您交流。
QQ:236306746
相关文章推荐
- DWR使用手记
- Atlas学习手记(5):使用服务端定时控件TimerControl
- Atlas学习手记(24):使用行为增强用户界面(四):Popup Behavior
- Atlas学习手记(12):使用CascadingDropDown控件
- 【OpenStack】vmware workstation使用devstack安装openstack手记
- Apache Mina使用手记(四)
- Makefile 使用手记(1)
- 在EXT中直接使用DWR
- 转个不错的 Reflector使用手记
- JavaWeb常用开发技术总结-js、spring、javabean、mysql数据库、dwr的使用等
- NetAdvantage使用手记(一)
- qooxdoo.js使用手记
- 【Discuz 7.2】使用手记(头像上传出错、不显示头像等)
- DWR框架搭建以及使用
- HA_Xenu1.3.6 使用手记
- Web端服务器推送技术原理分析及dwr框架简单的使用
- zookeeper客户端curator使用手记
- 反向ajax与dwr区别和反向ajax使用
- access2007 使用手记:Access用OleDbParameter无法更新数据
- VMWare GSX Server for Linux使用手记