<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
  <channel>
    <title>笨鸟先飞</title>
    <description></description>
    <link>http://zhuxue-feng.javaeye.com</link>
    <language>UTF-8</language>
    <copyright>Copyright 2003-2008, JavaEye.com</copyright>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>JavaEye - 做最棒的软件开发交流社区</generator>
      <item>
        <title>理解jQuery的两对小括号()()</title>
        <author>笨鸟先飞</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://zhuxue-feng.javaeye.com">笨鸟先飞</a>&nbsp;
          链接：<a href="http://zhuxue-feng.javaeye.com/blog/184859" style="color:red;">http://zhuxue-feng.javaeye.com/blog/184859</a>&nbsp;
          发表时间: 2008年04月21日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p><span style="font-size: small;">如果大家看jquery的sourcecode就知道一开始就有两对小括号,</span>
</p>
<p><span style="font-size: small;">是什么意思呢! 真的是比较困惑, 这么神奇的代码,什么意思啊? </span>
</p>
<p><span style="font-size: small;">我们先看看jQuery一开始是怎么写的:</span>
</p>
<p><span style="color: #0000ff; font-size: small;"> 
(function(){ 
    </span>
</p>
<p><span style="color: #0000ff; font-size: small;">// jquery 代码 </span>
</p>
<p><span style="font-size: small;"><span style="color: #0000ff;">})();</span>
 </span>
</p>
<p><span style="font-size: small;">它是由什么组成呢? 有一个匿名函数(函数没有名字),</span>
</p>
<p><span style="font-size: small;"> 此匿名函数被一对小括号包裹着,这对小括号右边还有一对小括号.</span>
</p>
<p><span style="font-size: small;"> 我们再看常用一般函数定义方式. </span>
</p>
<p><span style="color: #0000ff; font-size: small;">//函数定义 </span>
</p>
<p><span style="color: #0000ff; font-size: small;">function func1(){ </span>
</p>
<p><span style="color: #0000ff; font-size: small;">alert(&quot;hello,world&quot;);</span>
</p>
<p><span style="color: #0000ff; font-size: small;"> } </span>
</p>
<p><span style="font-size: small;"><span style="color: #0000ff;">//调用函数 </span>
</span>
</p>
<p><span style="font-size: small;"><span style="color: #0000ff;">func1();</span>
 </span>
</p>
<p><span style="font-size: small;">那么这两句话目的很明显就是要弹出一个&quot;helloworld&quot;的对话框,</span>
</p>
<p><span style="font-size: small;">那么我们可不可以合并成一句话, 也同样能达到这样的效果 </span>
</p>
<p><span style="font-size: small;">当然是可以的.</span>
</p>
<p><span style="color: #0000ff; font-size: small;"> (function (){ </span>
</p>
<p><span style="color: #0000ff; font-size: small;">alert(&quot;hello,world&quot;</span>
</p>
<p><span style="font-size: small;"><span style="color: #0000ff;">); })();</span>
 </span>
</p>
<p><span style="font-size: small;">函数名称func1都去掉了, 在函数外面加了一对小括号.</span>
</p>
<p><span style="font-size: small;">这和jquery是一样的啊! 可以看出最后面一对小括号语义就是&quot;调用此匿名函数&quot;,</span>
</p>
<p><span style="font-size: small;">那么包裹在匿名函数外面的一对小括号可以去掉吗? </span>
</p>
<p><span style="font-size: small;">变成如下的样子 </span>
</p>
<p><span style="color: #0000ff; font-size: small;">function (){</span>
</p>
<p><span style="color: #0000ff; font-size: small;">alert(&quot;hello,world&quot;); </span>
</p>
<p><span style="color: #0000ff; font-size: small;">}();</span>
</p>
<p>&nbsp;</p>
<p><span style="font-size: small;">要回答这个问题,先看看javascript对表达式的评估.</span>
</p>
<p><span style="font-size: small;">dd= aa+ cc*bb javascript会先评估cc*bb,</span>
</p>
<p><span style="font-size: small;">然后才会评估aa+,这是因为地球人都知道的事情,</span>
</p>
<p><span style="font-size: small;">乘运算(*)符优先级高于加(+) 同样如果去掉这对小括号的话, </span>
</p>
<p><span style="font-size: small;">我们可以假想一下其表达式的评估过程, </span>
</p>
<p><span style="font-size: small;">javascript会先评估最后面小括号,而不是先评估匿名函数了,</span>
</p>
<p><span style="font-size: small;">那么最后面的小括号是什么语义呢! 仅当作一般运算符去处理,</span>
</p>
<p><span style="font-size: small;">并不是&quot;此处存在函数调用&quot;的这样一个语义, </span>
</p>
<p><span style="font-size: small;">因为在这之前并不知道有匿名函数存在(还没有评估呢),</span>
</p>
<p><span style="font-size: small;">所以在匿名函数外面加上一对小括号的话,javascript对其评估的顺序就变了, </span>
</p>
<p><span style="font-size: small;">所以答案很明显,不可以去掉匿名函数外面的一对小括号,否则其评估就会失败,</span>
</p>
<p><span style="font-size: small;">导致语法错误. 看到这里是不是豁然开朗了,呵呵! 如果讲的有错误,还请大侠们给指出来,在此谢谢了.</span>
</p>
          <br/>
          <span style="color:red;">
            <a href="http://zhuxue-feng.javaeye.com/blog/184859#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 21 Apr 2008 11:47:08 +0800</pubDate>
        <link>http://zhuxue-feng.javaeye.com/blog/184859</link>
        <guid>http://zhuxue-feng.javaeye.com/blog/184859</guid>
      </item>
      <item>
        <title>理解javascript  closures 闭包(读书笔记)</title>
        <author>笨鸟先飞</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://zhuxue-feng.javaeye.com">笨鸟先飞</a>&nbsp;
          链接：<a href="http://zhuxue-feng.javaeye.com/blog/180252" style="color:red;">http://zhuxue-feng.javaeye.com/blog/180252</a>&nbsp;
          发表时间: 2008年04月07日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <br /><span style="font-size: small; color: #3366ff">什么是闭包(closures):</span><br />&nbsp;闭包就是内部函数通过某种方式使其可见范围超出了其定义的范围,这就产生了一个在其定义范围内的闭包.<br />&nbsp;<br />&nbsp;这是我理解后的一个定义,呵呵!很晦涩吧!不过没关系,可以先看看下面的讲解.<br />&nbsp;<br />&nbsp;<br /><span style="font-size: small; color: #3366ff">一 内部函数(inner function)</span><br /><br />javascript是支持内部函数申明(inner function declaration)的编程语言, <br /><br />内部函数就是在另一个函数的内部定义,如<br />function outerFun(){<br />&nbsp; function innerFun(){<br />&nbsp;&nbsp;&nbsp; alert('hello');<br />&nbsp; }<br />}<br /><br />函数innerFun就是内部函数, 它在函数outerFun范围内是可见的,<br />也就是说innerFun函数的命名空间(namespace)是在outerFun范围之内.<br /><br />正确调用:<br />function outerFun(){<br />&nbsp; function innerFun(){<br />&nbsp;&nbsp;&nbsp; alert('hello');<br />&nbsp; }<br />&nbsp; innerFun();<br />}<br />outerFun(); //alerts &quot;hello&quot;<br />错误调用(error):<br />function outerFun(){<br />&nbsp; function innerFun(){<br />&nbsp;&nbsp;&nbsp; alert('hello');<br />&nbsp; }<br />}<br />innerFun();<br /><br />那么如果我想在函数outerFun外面调用函数innerFun,我该如何做呢?<br /><span style="color: #ff9900">做法1:</span><br />var globVar;<br />function outerFun() {<br />&nbsp; function innerFun() {<br />&nbsp;&nbsp; alert('hello'); <br />&nbsp; } <br />&nbsp; globVar = innerFun;<br />}<br />outerFun();<br />globVar();<br /><span style="color: #ff9900">做法2:</span><br />function outerFun() { <br />&nbsp; function innerFun() { <br />&nbsp;&nbsp;&nbsp; alert('hello'); <br />&nbsp; } <br />&nbsp; return innerFun ;<br />}<br />var globVar = outerFun();<br />globVar();<br /><br /><span style="color: #ff9900">做法3:</span><br />function outerFun() { <br />&nbsp; function innerFun() { <br />&nbsp;&nbsp;&nbsp; alert('hello'); <br />&nbsp; } <br />&nbsp; return {'innerFun':innerFun} ;<br />}<br />var globVar = outerFun();<br />globVar.innerFun();<br /><br />以上三种做法把内部函数的可见范围扩大了, 其中最后一种做法是把内部函数当做匿名对象{'innerFun':innerFun}的属性,并随之一起返回.<br /><br />从中看到在javascript里面,函数名称可以当作是一种引用变量,类似于c里面指针的概念,在这里,随着程序的执行<br />会产生两个引用变量指向内部函数innerFun,一个是globVar(第三种做法是globVar.innerFun),另一个是其函数自身innerFun,<br />只不过这两个变量的可见范围不一样,即命名空间不一样. <br />javascript垃圾回收器会在函数最后一个引用变量被废弃后,释放其所占用的内存.<br /><br /><br /><span style="font-size: small; color: #0000ff">二 变量范围</span><br />例1 内部函数变量<br />在内部函数内申明的变量其可见范围就在其函数内<br />function outerFun() { <br />&nbsp;&nbsp; function innerFun() { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var innerVar = 0; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; innerVar++; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(innerVar); <br />&nbsp;&nbsp; } <br />&nbsp;&nbsp; return innerFun;<br />}<br />var globVar = outerFun();<br />globVar(); // Alerts &quot;1&quot;<br />globVar(); // Alerts &quot;1&quot;<br />var innerVar2 = outerFun();<br />innerVar2(); // Alerts &quot;1&quot;<br />innerVar2(); // Alerts &quot;1&quot;<br /><br />每一次内部函数调用,一个新的innerVar变量都被创建,所以结果显示都是1<br /><br /><br />例2 内部函数引用全局变量(global variables)<br /><br />var globVar = 0;<br />function outerFun() { <br />&nbsp; function innerFun() { <br />&nbsp;&nbsp;&nbsp; globVar++; <br />&nbsp;&nbsp;&nbsp; alert(globVar); <br />&nbsp; } <br />&nbsp; return innerFun;<br />}<br />var globVar = outerFun();<br />globVar(); // Alerts &quot;1&quot;<br />globVar(); // Alerts &quot;2&quot;<br />var globVar2 = outerFun();<br />globVar2(); // Alerts &quot;3&quot;<br />globVar2(); // Alerts &quot;4&quot;<br />每一次内部函数的调用,全局变量都增加1,所以显示结果都是依次递增.<br /><br />例3 内部函数引用外部函数变量<br />&nbsp; <br />&nbsp; function outerFun() { <br />&nbsp;&nbsp;&nbsp;&nbsp; var outerVar = 0; <br />&nbsp;&nbsp;&nbsp;&nbsp; function innerFun() { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; outerVar++; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(outerVar); <br />&nbsp;&nbsp;&nbsp;&nbsp; } <br />&nbsp;&nbsp;&nbsp;&nbsp; return innerFun;<br />&nbsp; }<br />&nbsp; <br />&nbsp; var globVar = outerFun();<br />&nbsp; globVar(); // Alerts &quot;1&quot;<br />&nbsp; globVar(); // Alerts &quot;2&quot;<br />&nbsp; var globVar2 = outerFun();<br />&nbsp; globVar2(); // Alerts &quot;1&quot;<br />&nbsp; globVar2(); // Alerts &quot;2&quot;<br />&nbsp; 注意在第2次调用outerFun()的时候重新创建了一个新的变量outerVar,所以显示结果是&quot;1&quot;,&quot;2&quot;,&quot;1&quot;,&quot;2&quot;<br />&nbsp;<br />什么是闭包(closures)呢, 就是内部函数通过某种方式使其可见范围超出了其定义的范围, 如上例3,globVar 就是函数outerFun()的一个闭包,<br />而闭包产生的时机就是这句代码var globVar = outerFun(); 再看上例3,闭包globVar第一次调用之后,变量outerVar 值 &quot;1&quot; 还是在内存<br />中,没有回收,因为闭包globVar第二次调用的时候其值已经递增为&quot;2&quot; ,只要globVar没有被废弃掉,则outerVar的值就会一直存在. 像outerVar<br />这样的变量称其为自由变量<br /><br /><span style="font-size: small; color: #0000ff">三 闭包与面向对象编程之间的联系</span><br />&nbsp;&nbsp; 看看下面的例子:<br />&nbsp;&nbsp; function outerFun() {<br />&nbsp;&nbsp;&nbsp;&nbsp; var outerVar = 0; <br />&nbsp;&nbsp;&nbsp;&nbsp; function innerFun() { <br />&nbsp;&nbsp;&nbsp;&nbsp; outerVar++; <br />&nbsp;&nbsp;&nbsp;&nbsp; alert(outerVar); <br />&nbsp;&nbsp;&nbsp;&nbsp; } <br />&nbsp;&nbsp;&nbsp;&nbsp; function innerFun2() { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; outerVar = outerVar + 2; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; alert(globVar); <br />&nbsp;&nbsp;&nbsp;&nbsp; } <br />&nbsp;&nbsp;&nbsp;&nbsp; return {'innerFun': innerFun, 'outerFun2': outerFun2};<br />&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp; var globVar = outerFun();<br />&nbsp;&nbsp; globVar.innerFun(); // Alerts &quot;1&quot;<br />&nbsp;&nbsp; globVar.innerFun2(); // Alerts &quot;3&quot;<br />&nbsp;&nbsp; globVar.innerFun(); // Alerts &quot;4&quot;<br />&nbsp;&nbsp; var globVar2 = outerFun();<br />&nbsp;&nbsp; globVar2.innerFun(); // Alerts &quot;1&quot;<br />&nbsp;&nbsp; globVar2.innerFun2(); // Alerts &quot;3&quot;<br />&nbsp;&nbsp; globVar2.innerFun(); // Alerts &quot;4&quot;<br />&nbsp; &nbsp;<br />&nbsp;&nbsp; outerFun()可以看成是类,globVar,globVar2可以看成是两个实例,实例变量就是outerVar,并且是private. 函数innerFun(),<br />&nbsp;&nbsp; innerFun2()就是实例方法. &nbsp;<br />&nbsp; &nbsp;<br />&nbsp;&nbsp; 可能这还不是很清楚,在看下面的例子:<br />&nbsp;&nbsp; function Boy(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var name;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function setName(vName){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name = vName;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function getName(){<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return name;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return {'setName': setName, 'getName': getName};<br />&nbsp;&nbsp; }<br />&nbsp;&nbsp; var boy1 = Boy();<br />&nbsp;&nbsp; boy1.setName(&quot;zhuzhenhua&quot;);<br />&nbsp;&nbsp; alert(boy1.getName());<br />&nbsp; &nbsp;<br />&nbsp; &nbsp;<br />&nbsp;&nbsp; 写到这里,仍然对闭包这个概念所对应的物理结构有些模糊,所以还请过路的侠士指点一二.<br />&nbsp; &nbsp;<br />&nbsp; &nbsp;<br />&nbsp; &nbsp;<br />&nbsp; &nbsp;<br /><br /><br /><br /><br /><br />&nbsp; <br /><br />&nbsp; <br /><br />&nbsp;<br /><br /><br /><br /><br />
          <br/>
          <span style="color:red;">
            <a href="http://zhuxue-feng.javaeye.com/blog/180252#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 07 Apr 2008 19:43:00 +0800</pubDate>
        <link>http://zhuxue-feng.javaeye.com/blog/180252</link>
        <guid>http://zhuxue-feng.javaeye.com/blog/180252</guid>
      </item>
      <item>
        <title>基于Spring的测试类进行DAO层集成测试实践(1)</title>
        <author>笨鸟先飞</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://zhuxue-feng.javaeye.com">笨鸟先飞</a>&nbsp;
          链接：<a href="http://zhuxue-feng.javaeye.com/blog/179648" style="color:red;">http://zhuxue-feng.javaeye.com/blog/179648</a>&nbsp;
          发表时间: 2008年04月05日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>资源准备:</p><p>mysql5.0</p><p>spring-2.5</p><p>hibernate-3.2.5</p><p>junit-4.4.jar</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>创建表</p><p>&nbsp;</p><p>DROP TABLE IF EXISTS `myproject`.`boys`;<br />CREATE TABLE&nbsp; `myproject`.`boys` (<br />&nbsp; `id` bigint(20) NOT NULL auto_increment,<br />&nbsp; `phone` varchar(20) default NULL,<br />&nbsp; `sex` varchar(2) default NULL,<br />&nbsp; `address` varchar(255) default NULL,<br />&nbsp; PRIMARY KEY&nbsp; (`id`)<br />) ENGINE=InnoDB DEFAULT CHARSET=utf8;</p><p>&nbsp;</p><p>创建相关配置文件:</p><p>&nbsp;</p><p>(1)applicationContext-resources.xml</p><p>&nbsp;</p><p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</p><p>&lt;beans xmlns=&quot;<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>&quot;<br />&nbsp;xmlns:xsi=&quot;<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>&quot;<br />&nbsp;xmlns:p=&quot;<a href="http://www.springframework.org/schema/p">http://www.springframework.org/schema/p</a>&quot;<br />&nbsp;xmlns:aop=&quot;<a href="http://www.springframework.org/schema/aop">http://www.springframework.org/schema/aop</a>&quot;<br />&nbsp;xmlns:context=&quot;<a href="http://www.springframework.org/schema/context">http://www.springframework.org/schema/context</a>&quot;<br />&nbsp;xmlns:jee=&quot;<a href="http://www.springframework.org/schema/jee">http://www.springframework.org/schema/jee</a>&quot;<br />&nbsp;xmlns:tx=&quot;<a href="http://www.springframework.org/schema/tx">http://www.springframework.org/schema/tx</a>&quot;<br />&nbsp;xsi:schemaLocation=&quot;<br />&nbsp;&nbsp;&nbsp;<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a> <a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</a><br />&nbsp;&nbsp;&nbsp;<a href="http://www.springframework.org/schema/context">http://www.springframework.org/schema/context</a> <a href="http://www.springframework.org/schema/context/spring-context-2.5.xsd">http://www.springframework.org/schema/context/spring-context-2.5.xsd</a><br />&nbsp;&nbsp;&nbsp;<a href="http://www.springframework.org/schema/jee">http://www.springframework.org/schema/jee</a> <a href="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">http://www.springframework.org/schema/jee/spring-jee-2.5.xsd</a><br />&nbsp;&nbsp;&nbsp;<a href="http://www.springframework.org/schema/tx">http://www.springframework.org/schema/tx</a> <a href="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">http://www.springframework.org/schema/tx/spring-tx-2.5.xsd</a><br />&nbsp;&nbsp;&nbsp;<a href="http://www.springframework.org/schema/aop">http://www.springframework.org/schema/aop</a> <a href="http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">http://www.springframework.org/schema/aop/spring-aop-2.5.xsd</a>&quot;&gt;<br />&nbsp;<br />&nbsp;<br />&nbsp;&lt;!--&lt;bean id=&quot;propertyConfigurer&quot; class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt;<br />&nbsp;&nbsp;&lt;property name=&quot;locations&quot;&gt;<br />&nbsp;&nbsp;&lt;list&gt;<br />&nbsp;&nbsp;&lt;value&gt;classpath:jdbc.properties&lt;/value&gt;<br />&nbsp;&nbsp;&lt;/list&gt;<br />&nbsp;&nbsp;&lt;/property&gt;<br />&nbsp;&nbsp;&lt;/bean&gt;<br />&nbsp;--&gt;<br />&nbsp;&lt;context:property-placeholder location=&quot;classpath:jdbc.properties&quot; /&gt;<br />&nbsp;&lt;!-- =====使用dbcp 连接池 --&gt;<br />&nbsp;&lt;bean id=&quot;dataSource&quot;<br />&nbsp;&nbsp;class=&quot;org.apache.commons.dbcp.BasicDataSource&quot; destroy-method=&quot;close&quot;<br />&nbsp;&nbsp;p:driverClassName=&quot;${jdbc.driverClassName}&quot; p:url=&quot;${jdbc.url}&quot;<br />&nbsp;&nbsp;p:username=&quot;${jdbc.username}&quot; p:password=&quot;${jdbc.password}&quot; /&gt;<br />&nbsp;&lt;!-- Hibernate SessionFactory --&gt;<br />&nbsp;&lt;bean id=&quot;sessionFactory&quot;<br />&nbsp;&nbsp;class=&quot;org.springframework.orm.hibernate3.LocalSessionFactoryBean&quot;<br />&nbsp;&nbsp;p:dataSource-ref=&quot;dataSource&quot;<br />&nbsp;&nbsp;p:mappingResources=&quot;testHibernate.hbm.xml&quot;&gt;<br />&nbsp;&nbsp;&lt;property name=&quot;hibernateProperties&quot;&gt;<br />&nbsp;&nbsp;&nbsp;&lt;props&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop key=&quot;hibernate.dialect&quot;&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${hibernate.dialect}<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/prop&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;prop key=&quot;hibernate.show_sql&quot;&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${hibernate.show_sql}<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/prop&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--&lt;prop key=&quot;hibernate.generate_statistics&quot;&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${hibernate.generate_statistics}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/prop&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;--&gt;<br />&nbsp;&nbsp;&nbsp;&lt;/props&gt;<br />&nbsp;&nbsp;&lt;/property&gt;<br />&nbsp;&lt;/bean&gt;<br />&nbsp;&lt;bean id=&quot;transactionManager&quot;<br />&nbsp;&nbsp;class=&quot;org.springframework.orm.hibernate3.HibernateTransactionManager&quot;<br />&nbsp;&nbsp;p:sessionFactory-ref=&quot;sessionFactory&quot; /&gt;<br />&nbsp;&lt;tx:advice id=&quot;txAdvice&quot; transaction-manager=&quot;transactionManager&quot;&gt;<br />&nbsp;&nbsp;&lt;tx:attributes&gt;<br />&nbsp;&nbsp;&nbsp;&lt;tx:method name=&quot;find*&quot; read-only=&quot;true&quot; /&gt;<br />&nbsp;&nbsp;&nbsp;&lt;tx:method name=&quot;get*&quot; read-only=&quot;true&quot; /&gt;<br />&nbsp;&nbsp;&nbsp;&lt;!-- 所有抛出异常都要回滚 --&gt;<br />&nbsp;&nbsp;&nbsp;&lt;tx:method name=&quot;*&quot; rollback-for=&quot;Throwable&quot; /&gt; <br />&nbsp;&nbsp;&lt;/tx:attributes&gt;<br />&nbsp;&lt;/tx:advice&gt;</p><p>&lt;!-- com.jmesa.test.service包下面(包括子包)后缀为Service的所有bean --&gt;<br />&nbsp;&lt;aop:config&gt;<br />&nbsp;&nbsp;&lt;aop:pointcut id=&quot;serviceOperation&quot;<br />&nbsp;&nbsp;&nbsp;expression=&quot;execution(* com.jmesa.test.service..*Service.*(..))&quot; /&gt;<br />&nbsp;&nbsp;&lt;aop:advisor pointcut-ref=&quot;serviceOperation&quot;<br />&nbsp;&nbsp;&nbsp;advice-ref=&quot;txAdvice&quot;/&gt;<br />&nbsp;&lt;/aop:config&gt;<br />&lt;/beans&gt;</p><p>&nbsp;</p><p>(2) applicationContext-dao.xml</p><p>&nbsp;</p><p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</p><p>&lt;beans xmlns=&quot;<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>&quot;<br />&nbsp;xmlns:xsi=&quot;<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>&quot;<br />&nbsp;xmlns:p=&quot;<a href="http://www.springframework.org/schema/p">http://www.springframework.org/schema/p</a>&quot;<br />&nbsp;xmlns:aop=&quot;<a href="http://www.springframework.org/schema/aop">http://www.springframework.org/schema/aop</a>&quot;<br />&nbsp;xmlns:context=&quot;<a href="http://www.springframework.org/schema/context">http://www.springframework.org/schema/context</a>&quot;<br />&nbsp;xmlns:jee=&quot;<a href="http://www.springframework.org/schema/jee">http://www.springframework.org/schema/jee</a>&quot;<br />&nbsp;xmlns:tx=&quot;<a href="http://www.springframework.org/schema/tx">http://www.springframework.org/schema/tx</a>&quot;<br />&nbsp;xsi:schemaLocation=&quot;<br />&nbsp;&nbsp;&nbsp;<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a> <a href="http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</a><br />&nbsp;&nbsp;&nbsp;<a href="http://www.springframework.org/schema/context">http://www.springframework.org/schema/context</a> <a href="http://www.springframework.org/schema/context/spring-context-2.5.xsd">http://www.springframework.org/schema/context/spring-context-2.5.xsd</a><br />&nbsp;&nbsp;&nbsp;<a href="http://www.springframework.org/schema/jee">http://www.springframework.org/schema/jee</a> htp://www.springframework.org/schema/jee/spring-jee-2.5.xsd<br />&nbsp;&nbsp;&nbsp;<a href="http://www.springframework.org/schema/tx">http://www.springframework.org/schema/tx</a> <a href="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">http://www.springframework.org/schema/tx/spring-tx-2.5.xsd</a>&quot;&gt;<br />&nbsp;<br />&nbsp;<br />&nbsp;&lt;bean name=&quot;boysDAO&quot; class=&quot;com.jmesa.test.dao.hibernate.BoysDAOImpl&quot; autowire=&quot;byName&quot;&gt;&lt;/bean&gt;<br />&lt;/beans&gt;</p><p>&nbsp;</p><p>(3)jdbc.properties</p><p>jdbc.driverClassName=com.mysql.jdbc.Driver<br />jdbc.url=jdbc:mysql://localhost/myproject?createDatabaseIfNotExist=true&amp;amp;useUnicode=true&amp;amp;characterEncoding=utf-8<br />jdbc.username=root<br />jdbc.password=root<br />hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect<br />hibernate.show_sql=true</p><p>(4) boys 实体的hibernate配置文件:</p><p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br />&lt;!DOCTYPE hibernate-mapping PUBLIC &quot;-//Hibernate/Hibernate Mapping DTD//EN&quot;<br />&nbsp;&nbsp;&quot;<a href="http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd</a>&quot;&gt;</p><p>&lt;hibernate-mapping auto-import=&quot;true&quot; default-lazy=&quot;false&quot;&gt;<br />&nbsp;&lt;class name=&quot;com.jmesa.test.model.Boy&quot; table=&quot;boys&quot;&gt;<br />&nbsp;&nbsp;&lt;id name=&quot;id&quot; column=&quot;id&quot; type=&quot;long&quot;&gt;<br />&nbsp;&nbsp;&nbsp;&lt;generator class=&quot;identity&quot;&gt;&lt;/generator&gt;<br />&nbsp;&nbsp;&lt;/id&gt;<br />&nbsp;&nbsp;&lt;property name=&quot;phone&quot; column=&quot;phone&quot;&gt;&lt;/property&gt;<br />&nbsp;&nbsp;&lt;property name=&quot;sex&quot; column=&quot;sex&quot;&gt;&lt;/property&gt;<br />&nbsp;&nbsp;&lt;property name=&quot;address&quot;&gt;&lt;/property&gt;<br />&nbsp;&lt;/class&gt;<br />&lt;/hibernate-mapping&gt;</p><p>&nbsp;</p><p>&nbsp;</p><p>创建BoysDAO 接口</p><p>&nbsp;</p><p>package com.jmesa.test.dao;</p><p>import java.util.List;</p><p>import com.jmesa.test.common.EntityFilters;<br />import com.jmesa.test.common.EntitySorts;<br />import com.jmesa.test.model.Boy;</p><p>public interface BoysDAO {<br />&nbsp;/**<br />&nbsp; * <br />&nbsp; * @param boy<br />&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 域对象<br />&nbsp; */<br />&nbsp;public void save(Boy boy);</p><p>&nbsp;/**<br />&nbsp; * 更新boy数据<br />&nbsp; * <br />&nbsp; * @param boy<br />&nbsp; */<br />&nbsp;public void update(Boy boy);</p><p>&nbsp;/**<br />&nbsp; * 依据id值获得boy域对象<br />&nbsp; * <br />&nbsp; * @param id<br />&nbsp; * @return<br />&nbsp; */<br />&nbsp;public Boy get(String id);</p><p>&nbsp;/**<br />&nbsp; * 获得所有boy域对象<br />&nbsp; * <br />&nbsp; * @return<br />&nbsp; */<br />&nbsp;public List&lt;Boy&gt; getAll();</p><p>/**<br />&nbsp; * 依据boy 属性条件查询boy列表<br />&nbsp; * <br />&nbsp; * @param phone<br />&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 空或者null值将不作为查询条件<br />&nbsp; * @param sex<br />&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 空或者null值将不作为查询条件<br />&nbsp; * @return<br />&nbsp; */<br />&nbsp;public List&lt;Boy&gt; queryBoys(String phone, String sex);</p><p>}</p><p>&nbsp;</p><p>BoysDAO 接口实现类:</p><p>package com.jmesa.test.dao.hibernate;</p><p>import java.sql.SQLException;<br />import java.util.List;</p><p>import org.hibernate.Criteria;<br />import org.hibernate.HibernateException;<br />import org.hibernate.Session;<br />import org.hibernate.criterion.DetachedCriteria;<br />import org.hibernate.criterion.Projections;<br />import org.hibernate.criterion.Property;<br />import org.springframework.orm.hibernate3.HibernateCallback;<br />import org.springframework.orm.hibernate3.support.HibernateDaoSupport;<br />import com.jmesa.test.dao.BoysDAO;<br />import com.jmesa.test.model.Boy;</p><p>public class BoysDAOImpl extends HibernateDaoSupport implements BoysDAO {</p><p>&nbsp;public Boy get(String id) {<br />&nbsp;&nbsp;return (Boy) this.getHibernateTemplate().load(Boy.class, id);<br />&nbsp;}</p><p>&nbsp;public List&lt;Boy&gt; getAll() {<br />&nbsp;&nbsp;return queryBoys(null, null);<br />&nbsp;}</p><p>&nbsp;public List&lt;Boy&gt; queryBoys(String phone, String sex) {<br />&nbsp;&nbsp;DetachedCriteria query = DetachedCriteria.forClass(Boy.class);<br />&nbsp;&nbsp;if (phone != null &amp;&amp; !phone.equals(&quot;&quot;)) {<br />&nbsp;&nbsp;&nbsp;query.add(Property.forName(&quot;phone&quot;).eq(phone));<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;if (sex != null &amp;&amp; !sex.equals(&quot;&quot;)) {<br />&nbsp;&nbsp;&nbsp;query.add(Property.forName(&quot;sex&quot;).eq(sex));<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;return this.getHibernateTemplate().findByCriteria(query);<br />&nbsp;}</p><p>public void save(Boy boy) {<br />&nbsp;&nbsp;this.getHibernateTemplate().save(boy);</p><p>&nbsp;}</p><p>public void update(Boy boy) {<br />&nbsp;&nbsp;this.getHibernateTemplate().update(boy);<br />&nbsp;}</p><p>}</p><p>&nbsp;</p><p>开始测试:</p><p>基于AbstractTransactionalDataSourceSpringContextTests 创建测试基类:</p><p>package com.jmesa.test.integrateTest;</p><p>import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;</p><p><span style="color: #ff0000">public class BaseDataSourceSpringContextIntegrationTest extends<br />&nbsp;&nbsp;AbstractTransactionalDataSourceSpringContextTests {</span></p><p><span style="color: #ff0000">//加载spring配置文件,其路径在classpath的根目录下<br />&nbsp;private static final String[] configFiles = new String[] {<br />&nbsp;&nbsp;&nbsp;&quot;applicationContext-resources.xml&quot;, &quot;applicationContext-dao.xml&quot; };</span></p><p><span style="color: #ff0000">//设置dao类的注入方式,默认为类型注入,此处改为名称注入</span></p><p><span style="color: #ff0000">public BaseDataSourceSpringContextIntegrationTest() {<br />&nbsp;&nbsp;this.setAutowireMode(AUTOWIRE_BY_NAME);<br />&nbsp;}</span></p><p><span style="color: #ff0000">//通过此方法提供spring配置文件</span></p><p><span style="color: #ff0000">protected String[] getConfigLocations() {<br />&nbsp;&nbsp;return configFiles;<br />&nbsp;}<br />}</span></p><p>&nbsp;</p><p>BoysDAO的测试类:</p><p>&nbsp;</p><p>package com.jmesa.test.integrateTest;</p><p>import java.util.List;</p><p>import org.junit.Assert;<br />import org.junit.Test;</p><p>import com.jmesa.test.common.EntityFilters;<br />import com.jmesa.test.common.EntitySorts;<br />import com.jmesa.test.dao.BoysDAO;<br />import com.jmesa.test.model.Boy;</p><p>/**<br />&nbsp;* @author&nbsp;&nbsp; jack<br />&nbsp;*/<br />public class BoyDAOTest extends BaseDataSourceSpringContextIntegrationTest {<br />&nbsp;<span style="color: #0000ff">private BoysDAO boysDAO;</span></p><p><span style="color: #0000ff">public BoyDAOTest(){<br />&nbsp;}<br />&nbsp;/**<br />&nbsp; *&nbsp;必须要提供变量boysDAO的setter方法,这样注入才会成功<br />&nbsp; */<br />&nbsp;public void setBoysDAO(BoysDAO boysDAO) {<br />&nbsp;&nbsp;this.boysDAO = boysDAO;<br />&nbsp;}</span></p><p><span style="color: #0000ff"><span style="color: #ffcc00">@before</span></span></p><p><span style="color: #0000ff"><span style="color: #ffcc00">&nbsp;public void initialize(){</span></span></p><p><span style="color: #0000ff"><span style="color: #ffcc00">&nbsp;}</span></span></p><p><span style="color: #0000ff"><span style="color: #ffcc00">@after</span></span></p><p><span style="color: #0000ff"><span style="color: #ffcc00">public void destroy(){</span></span></p><p><span style="color: #0000ff"><span style="color: #ffcc00">}<br /></span></span></p><p>@Test<br />&nbsp;public void testGetAll(){<br />&nbsp;&nbsp;int count = this.countRowsInTable(&quot;boys&quot;);<br />&nbsp;&nbsp;Assert.assertEquals(boysDAO.getAll().size(),count);<br />&nbsp;}<br />&nbsp;@Test<br />&nbsp;public void testQueryBoys(){<br />&nbsp;&nbsp;Assert.assertEquals(0,boysDAO.queryBoys(&quot;www&quot;, &quot;wwww&quot;).size());<br />&nbsp;&nbsp;Assert.assertEquals(1,boysDAO.queryBoys(&quot;eee&quot;, &quot;33&quot;).size());<br />&nbsp;}<br />&nbsp;@Test<br />&nbsp;public void testSave(){<br />&nbsp;&nbsp;int boysCount = this.countRowsInTable(&quot;boys&quot;);<br />&nbsp;&nbsp;String phone =&quot;13401108995&quot;;<br />&nbsp;&nbsp;String sex = &quot;1&quot;;<br />&nbsp;&nbsp;String address = &quot;南京路&quot;;<br />&nbsp;&nbsp;Boy boy = new Boy();<br />&nbsp;&nbsp;boy.setPhone(phone);<br />&nbsp;&nbsp;boy.setSex(sex);<br />&nbsp;&nbsp;boy.setAddress(address);<br />&nbsp;&nbsp;boysDAO.save(boy);<br />&nbsp;&nbsp;Assert.assertEquals(boysCount+1, this.countRowsInTable(&quot;boys&quot;));&nbsp;<br />&nbsp;}<br />&nbsp;@Test<br />&nbsp;public void testGetBoysCountWithFilter(){<br />&nbsp;&nbsp;EntityFilters entityFilters = new EntityFilters();<br />&nbsp;&nbsp;int count = boysDAO.getBoysCountWithFilter(entityFilters);<br />&nbsp;&nbsp;Assert.assertEquals(20, count);<br />&nbsp;&nbsp;EntityFilters entityFilters1 = new EntityFilters();<br />&nbsp;&nbsp;entityFilters1.addFilter(&quot;phone&quot;, &quot;342432&quot;);<br />&nbsp;&nbsp;int size = entityFilters1.filterSize();<br />&nbsp;&nbsp;int count1 = boysDAO.getBoysCountWithFilter(entityFilters1);<br />&nbsp;&nbsp;Assert.assertEquals(1, count1);<br />&nbsp;}<br />&nbsp;@Test<br />&nbsp;public void testGetBoysWithFilterAndSort(){<br />&nbsp;&nbsp;EntityFilters entityFilters = new EntityFilters();<br />&nbsp;&nbsp;//entityFilters.addFilter(&quot;phone&quot;, &quot;342432&quot;);<br />&nbsp;&nbsp;EntitySorts entitySorts = new EntitySorts();<br />&nbsp;&nbsp;entitySorts.addSort(&quot;phone&quot;, &quot;desc&quot;);<br />&nbsp;&nbsp;List&lt;Boy&gt; boysList = boysDAO.getBoysWithFilterAndSort(entityFilters, entitySorts, 0, 10);<br />&nbsp;&nbsp;Assert.assertEquals(10, boysList.size());<br />&nbsp;&nbsp;Boy boy = boysList.get(0);<br />&nbsp;&nbsp;Assert.assertEquals(&quot;ww&quot;,boy.getPhone() );<br />&nbsp;&nbsp;<br />&nbsp;}<br />}&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p><span style="color: #99cc00">此处测试用到了&nbsp;<span style="color: #ff0000">AbstractTransactionalDataSourceSpringContextTests 的四方面的功能:</span></span></p><p><span style="color: #ff0000"><span style="color: #99cc00">1. 加载spring的资源配置文件</span></span></p><p><span style="color: #ff0000"><span style="color: #99cc00">2. dao bean 引入测试类</span></span></p><p><span style="color: #ff0000"><span style="color: #99cc00">3. 简便的函数使用,如:countRowsInTable</span></span></p><p><span style="color: #ff0000"><span style="color: #99cc00">4. 事务自动回滚.</span></span></p><p>&nbsp;</p><p>请注意此BoyDAOTest测试类中<span style="color: #ff0000">使用@before,@after修饰的方法在测试中没有被执行, 可能原因是BoyDAOTest 间接继承了TestCase类,要想执行此类方法,必须沿用以前版本的junit的setUp,tearDown</span></p><p><span style="color: #ff0000">方法,然而</span><span style="color: #ff0000">AbstractTransactionalDataSourceSpringContextTests 类已经overide此方法,并且加了final修饰,其子类不可以在overide了. 所以加入类似before,after这样的方法,目前还没有想出比较好的办法? 还请大侠们赐教.</span></p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p><span style="color: #ff0000">参考文献:<a href="http://www.infoq.com/articles/testing-in-spring">http://www.infoq.com/articles/testing-in-spring</a></span></p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>
          <br/>
          <span style="color:red;">
            <a href="http://zhuxue-feng.javaeye.com/blog/179648#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sat, 05 Apr 2008 13:17:11 +0800</pubDate>
        <link>http://zhuxue-feng.javaeye.com/blog/179648</link>
        <guid>http://zhuxue-feng.javaeye.com/blog/179648</guid>
      </item>
      <item>
        <title>struts2 基于convention的开发(1) </title>
        <author>笨鸟先飞</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://zhuxue-feng.javaeye.com">笨鸟先飞</a>&nbsp;
          链接：<a href="http://zhuxue-feng.javaeye.com/blog/166614" style="color:red;">http://zhuxue-feng.javaeye.com/blog/166614</a>&nbsp;
          发表时间: 2008年03月02日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p><span style="font-size: small; color: #3366ff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当你使用struts2开发一个user实体的&quot;增删改查&quot;应用时，你或许是这样</span></p><p><span style="font-size: small; color: #3366ff">来配置action</span></p><p><span style="font-size: small; color: #3366ff">&nbsp;&nbsp; &lt;action name=&quot;createUser&quot;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class=&quot;com.mycompany.webapp.user.UserAction&quot;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; method=&quot;create&quot;&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp; &lt;result name=&quot;success&quot;&gt;/user/userCreate.jsp&lt;/result&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp; &lt;result name=&quot;input&quot;&gt;/user/userEdit.jsp&lt;/result&gt;<br />&nbsp; &nbsp;&nbsp; &lt;result name=&quot;home&quot;&gt;/user/home.jsp&lt;/result&gt;<br />&nbsp; &lt;/action&gt;</span></p><p><span style="font-size: small; color: #3366ff">........</span></p><p><span style="font-size: small; color: #3366ff">其所对应的url可能是这样：/app/createUser.action,/app/retrieveUser.action,</span></p><p><span style="font-size: small; color: #3366ff"> /app/updateUser.action,/app/deleteUser.action</span></p><p><span style="font-size: small; color: #3366ff">依次类推，一个实体的&quot;增删改查&quot;需要重复4次这样的工作。一个实体类已有如此多配置信息。多个实体又如何呢？</span></p><p><span style="font-size: small; color: #3366ff">&nbsp;</span></p><p><span style="font-size: small; color: #3366ff">那么我们可以使用struts2的另一个功能：在action标签中的name属性里指定wildcard（通配符）。</span></p><p><span style="font-size: small; color: #3366ff">struts2提供星号(*)的通配符，以上应用我们可以这样配置：</span></p><p><span style="font-size: small; color: #3366ff">&lt;action name=&quot;*_*&quot; class=&quot;com.mycompany.webapp.user.{2}Action&quot; method=&quot;{1}&quot;&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result name=&quot;success&quot;&gt;/{2}/{1}.jsp&lt;/result&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;result name=&quot;input&quot;&gt;/{2}/edit.jsp&lt;/result&gt;<br />
&nbsp; &nbsp; &lt;result name=&quot;home&quot;&gt;/{2}/home.jsp&lt;/result&gt;<br />
&lt;/action&gt; </span></p><p><span style="font-size: small; color: #3366ff">其所对应的user实体的url可能是：/app/create_User.action,/app/retrieve_User.action,</span></p><p><span style="font-size: small; color: #3366ff"> /app/update_User.action,/app/delete_User.action</span></p><p><span style="font-size: small; color: #3366ff">这样的话其它的实体也可以使用这种convention。</span></p><p><span style="font-size: small; color: #3366ff">请注意在name属性中的分隔符显然是要有的，不然就无法区分两个星号所配置的部分</span></p><p><span style="font-size: small; color: #3366ff">现在大概来总结一下此应用的convention：</span></p><p><span style="font-size: small; color: #3366ff">1 action类的命名为：实体名称（大写）+Action</span></p><p><span style="font-size: small; color: #3366ff">2 action标签中name属性的值为：方法名称＋实体名称（大写）</span></p><p><span style="font-size: small; color: #3366ff">3 实体所对应的jsp文件放在实体名称所对应的目录里面<br />4 初始jsp页面文件名称定义为&quot;home&quot;, </span></p><p><span style="font-size: small; color: #3366ff">validatation返回页面(也就是input所定义的页面)文件名称定义为edit&nbsp; <br />5 在success中定义的jsp文件名称为：方法名称</span>  </p><p><span style="font-size: small; color: #3366ff">&nbsp;</span></p><p><span style="font-size: small; color: #3366ff">&nbsp;</span></p>
          <br/>
          <span style="color:red;">
            <a href="http://zhuxue-feng.javaeye.com/blog/166614#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sun, 02 Mar 2008 11:08:00 +0800</pubDate>
        <link>http://zhuxue-feng.javaeye.com/blog/166614</link>
        <guid>http://zhuxue-feng.javaeye.com/blog/166614</guid>
      </item>
      <item>
        <title>有关jacob的word操作研究</title>
        <author>笨鸟先飞</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://zhuxue-feng.javaeye.com">笨鸟先飞</a>&nbsp;
          链接：<a href="http://zhuxue-feng.javaeye.com/blog/35350" style="color:red;">http://zhuxue-feng.javaeye.com/blog/35350</a>&nbsp;
          发表时间: 2006年11月21日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p><font color="#0000ff" face="Arial">程序测试环境：</font></p>
<p><font color="#0000ff">windowsXP sp2</font></p>
<p><font color="#0000ff">office2003</font></p>
<p><font color="#0000ff" face="Arial">jacob_1.11-pre1.zip jacob开发包</font></p>
<p>&nbsp;</p>
<p><font face="Arial">package com.langchaoauto.eimip.common;</font></p>
<p><font face="Arial">import java.util.HashMap;</font></p>
<p><font face="Arial">import com.jacob.activeX.ActiveXComponent;<br />
import com.jacob.com.Dispatch;<br />
import com.jacob.com.Variant;</font></p>
<p><font face="Arial">/**<br />
&nbsp;* <br />
&nbsp;* @author zhuzhen_hua@yahoo.com.cn<br />
&nbsp;* <br />
&nbsp;*/</font></p>
<p><font face="Arial">public class MSWordManager {<br />
&nbsp;// word文档<br />
&nbsp;private Dispatch doc;</font></p>
<p><font face="Arial">&nbsp;// word运行程序对象<br />
&nbsp;private ActiveXComponent word;</font></p>
<p><font face="Arial">&nbsp;// 所有word文档集合<br />
&nbsp;private Dispatch documents;</font></p>
<p><font face="Arial">&nbsp;// 选定的范围或插入点<br />
&nbsp;private Dispatch selection;<br />
&nbsp;</font></p>
<p><font face="Arial">&nbsp;private boolean saveOnExit = true;</font></p>
<p><font face="Arial">&nbsp;public MSWordManager() {<br />
&nbsp;&nbsp;if (word == null) {<br />
&nbsp;&nbsp;&nbsp;word = new ActiveXComponent(&quot;Word.Application&quot;);<br />
&nbsp;&nbsp;&nbsp;word.setProperty(&quot;Visible&quot;, new Variant(false));<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;if (documents == null)<br />
&nbsp;&nbsp;&nbsp;documents = word.getProperty(&quot;Documents&quot;).toDispatch();<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 设置退出时参数<br />
&nbsp; * <br />
&nbsp; * @param saveOnExit<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean true-退出时保存文件，false-退出时不保存文件<br />
&nbsp; */<br />
&nbsp;public void setSaveOnExit(boolean saveOnExit) {<br />
&nbsp;&nbsp;this.saveOnExit = saveOnExit;<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 创建一个新的word文档<br />
&nbsp; * <br />
&nbsp; */<br />
&nbsp;public void createNewDocument() {<br />
&nbsp;&nbsp;doc = Dispatch.call(documents, &quot;Add&quot;).toDispatch();<br />
&nbsp;&nbsp;selection = Dispatch.get(word, &quot;Selection&quot;).toDispatch();<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 打开一个已存在的文档<br />
&nbsp; * <br />
&nbsp; * @param docPath<br />
&nbsp; */<br />
&nbsp;public void openDocument(String docPath) {<br />
&nbsp;&nbsp;closeDocument();<br />
&nbsp;&nbsp;doc = Dispatch.call(documents, &quot;Open&quot;, docPath).toDispatch();<br />
&nbsp;&nbsp;selection = Dispatch.get(word, &quot;Selection&quot;).toDispatch();<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 把选定的内容或插入点向上移动<br />
&nbsp; * <br />
&nbsp; * @param pos<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 移动的距离<br />
&nbsp; */<br />
&nbsp;public void moveUp(int pos) {<br />
&nbsp;&nbsp;if (selection == null)<br />
&nbsp;&nbsp;&nbsp;selection = Dispatch.get(word, &quot;Selection&quot;).toDispatch();<br />
&nbsp;&nbsp;for (int i = 0; i &lt; pos; i++)<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(selection, &quot;MoveUp&quot;);</font></p>
<p><font face="Arial">&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 把选定的内容或者插入点向下移动<br />
&nbsp; * <br />
&nbsp; * @param pos<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 移动的距离<br />
&nbsp; */<br />
&nbsp;public void moveDown(int pos) {<br />
&nbsp;&nbsp;if (selection == null)<br />
&nbsp;&nbsp;&nbsp;selection = Dispatch.get(word, &quot;Selection&quot;).toDispatch();<br />
&nbsp;&nbsp;for (int i = 0; i &lt; pos; i++)<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(selection, &quot;MoveDown&quot;);<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 把选定的内容或者插入点向左移动<br />
&nbsp; * <br />
&nbsp; * @param pos<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 移动的距离<br />
&nbsp; */<br />
&nbsp;public void moveLeft(int pos) {<br />
&nbsp;&nbsp;if (selection == null)<br />
&nbsp;&nbsp;&nbsp;selection = Dispatch.get(word, &quot;Selection&quot;).toDispatch();<br />
&nbsp;&nbsp;for (int i = 0; i &lt; pos; i++) {<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(selection, &quot;MoveLeft&quot;);<br />
&nbsp;&nbsp;}<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 把选定的内容或者插入点向右移动<br />
&nbsp; * <br />
&nbsp; * @param pos<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 移动的距离<br />
&nbsp; */<br />
&nbsp;public void moveRight(int pos) {<br />
&nbsp;&nbsp;if (selection == null)<br />
&nbsp;&nbsp;&nbsp;selection = Dispatch.get(word, &quot;Selection&quot;).toDispatch();<br />
&nbsp;&nbsp;for (int i = 0; i &lt; pos; i++)<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(selection, &quot;MoveRight&quot;);<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 把插入点移动到文件首位置<br />
&nbsp; * <br />
&nbsp; */<br />
&nbsp;public void moveStart() {<br />
&nbsp;&nbsp;if (selection == null)<br />
&nbsp;&nbsp;&nbsp;selection = Dispatch.get(word, &quot;Selection&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(selection, &quot;HomeKey&quot;, new Variant(6));<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 从选定内容或插入点开始查找文本<br />
&nbsp; * <br />
&nbsp; * @param toFindText<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 要查找的文本<br />
&nbsp; * @return boolean true-查找到并选中该文本，false-未查找到文本<br />
&nbsp; */<br />
&nbsp;public boolean find(String toFindText) {<br />
&nbsp;&nbsp;if (toFindText == null || toFindText.equals(&quot;&quot;))<br />
&nbsp;&nbsp;&nbsp;return false;<br />
&nbsp;&nbsp;// 从selection所在位置开始查询<br />
&nbsp;&nbsp;Dispatch find = word.call(selection, &quot;Find&quot;).toDispatch();<br />
&nbsp;&nbsp;// 设置要查找的内容<br />
&nbsp;&nbsp;Dispatch.put(find, &quot;Text&quot;, toFindText);<br />
&nbsp;&nbsp;// 向前查找<br />
&nbsp;&nbsp;Dispatch.put(find, &quot;Forward&quot;, &quot;True&quot;);<br />
&nbsp;&nbsp;// 设置格式<br />
&nbsp;&nbsp;Dispatch.put(find, &quot;Format&quot;, &quot;True&quot;);<br />
&nbsp;&nbsp;// 大小写匹配<br />
&nbsp;&nbsp;Dispatch.put(find, &quot;MatchCase&quot;, &quot;True&quot;);<br />
&nbsp;&nbsp;// 全字匹配<br />
&nbsp;&nbsp;Dispatch.put(find, &quot;MatchWholeWord&quot;, &quot;True&quot;);<br />
&nbsp;&nbsp;// 查找并选中<br />
&nbsp;&nbsp;return Dispatch.call(find, &quot;Execute&quot;).getBoolean();<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 把选定选定内容设定为替换文本<br />
&nbsp; * <br />
&nbsp; * @param toFindText<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 查找字符串<br />
&nbsp; * @param newText<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 要替换的内容<br />
&nbsp; * @return<br />
&nbsp; */<br />
&nbsp;public boolean replaceText(String toFindText, String newText) {<br />
&nbsp;&nbsp;if (!find(toFindText))<br />
&nbsp;&nbsp;&nbsp;return false;<br />
&nbsp;&nbsp;Dispatch.put(selection, &quot;Text&quot;, newText);<br />
&nbsp;&nbsp;return true;<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 全局替换文本<br />
&nbsp; * <br />
&nbsp; * @param toFindText<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 查找字符串<br />
&nbsp; * @param newText<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 要替换的内容<br />
&nbsp; */<br />
&nbsp;public void replaceAllText(String toFindText, String newText) {<br />
&nbsp;&nbsp;while (find(toFindText)) {<br />
&nbsp;&nbsp;&nbsp;Dispatch.put(selection, &quot;Text&quot;, newText);<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(selection, &quot;MoveRight&quot;);<br />
&nbsp;&nbsp;}<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 在当前插入点插入字符串<br />
&nbsp; * <br />
&nbsp; * @param newText<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 要插入的新字符串<br />
&nbsp; */<br />
&nbsp;public void insertText(String newText) {<br />
&nbsp;&nbsp;Dispatch.put(selection, &quot;Text&quot;, newText);<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * <br />
&nbsp; * @param toFindText<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 要查找的字符串<br />
&nbsp; * @param imagePath<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 图片路径<br />
&nbsp; * @return<br />
&nbsp; */<br />
&nbsp;public boolean replaceImage(String toFindText, String imagePath) {<br />
&nbsp;&nbsp;if (!find(toFindText))<br />
&nbsp;&nbsp;&nbsp;return false;<br />
&nbsp;&nbsp;Dispatch.call(Dispatch.get(selection, &quot;InLineShapes&quot;).toDispatch(),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&quot;AddPicture&quot;, imagePath);<br />
&nbsp;&nbsp;return true;<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 全局替换图片<br />
&nbsp; * <br />
&nbsp; * @param toFindText<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 查找字符串<br />
&nbsp; * @param imagePath<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 图片路径<br />
&nbsp; */<br />
&nbsp;public void replaceAllImage(String toFindText, String imagePath) {<br />
&nbsp;&nbsp;while (find(toFindText)) {<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(Dispatch.get(selection, &quot;InLineShapes&quot;).toDispatch(),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;AddPicture&quot;, imagePath);<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(selection, &quot;MoveRight&quot;);<br />
&nbsp;&nbsp;}<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 在当前插入点插入图片<br />
&nbsp; * <br />
&nbsp; * @param imagePath<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 图片路径<br />
&nbsp; */<br />
&nbsp;public void insertImage(String imagePath) {<br />
&nbsp;&nbsp;Dispatch.call(Dispatch.get(selection, &quot;InLineShapes&quot;).toDispatch(),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&quot;AddPicture&quot;, imagePath);<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 合并单元格<br />
&nbsp; * <br />
&nbsp; * @param tableIndex<br />
&nbsp; * @param fstCellRowIdx<br />
&nbsp; * @param fstCellColIdx<br />
&nbsp; * @param secCellRowIdx<br />
&nbsp; * @param secCellColIdx<br />
&nbsp; */<br />
&nbsp;public void mergeCell(int tableIndex, int fstCellRowIdx, int fstCellColIdx,<br />
&nbsp;&nbsp;&nbsp;int secCellRowIdx, int secCellColIdx) {<br />
&nbsp;&nbsp;// 所有表格<br />
&nbsp;&nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;// 要填充的表格<br />
&nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;, new Variant(tableIndex))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;Dispatch fstCell = Dispatch.call(table, &quot;Cell&quot;,<br />
&nbsp;&nbsp;&nbsp;&nbsp;new Variant(fstCellRowIdx), new Variant(fstCellColIdx))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;Dispatch secCell = Dispatch.call(table, &quot;Cell&quot;,<br />
&nbsp;&nbsp;&nbsp;&nbsp;new Variant(secCellRowIdx), new Variant(secCellColIdx))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(fstCell, &quot;Merge&quot;, secCell);<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 在指定的单元格里填写数据<br />
&nbsp; * <br />
&nbsp; * @param tableIndex<br />
&nbsp; * @param cellRowIdx<br />
&nbsp; * @param cellColIdx<br />
&nbsp; * @param txt<br />
&nbsp; */<br />
&nbsp;public void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx,<br />
&nbsp;&nbsp;&nbsp;String txt) {<br />
&nbsp;&nbsp;// 所有表格<br />
&nbsp;&nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;// 要填充的表格<br />
&nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;, new Variant(tableIndex))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;Dispatch cell = Dispatch.call(table, &quot;Cell&quot;, new Variant(cellRowIdx),<br />
&nbsp;&nbsp;&nbsp;&nbsp;new Variant(cellColIdx)).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(cell, &quot;Select&quot;);<br />
&nbsp;&nbsp;Dispatch.put(selection, &quot;Text&quot;, txt);<br />
&nbsp;}<br />
&nbsp;/**<br />
&nbsp; * 在当前文档拷贝剪贴板数据<br />
&nbsp; * @param pos<br />
&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public void pasteExcelSheet(String pos){<br />
&nbsp;&nbsp;&nbsp; &nbsp;moveStart();<br />
&nbsp;&nbsp;&nbsp; &nbsp;if (this.find(pos)) {<br />
&nbsp;&nbsp;&nbsp;Dispatch textRange = Dispatch.get(selection, &quot;Range&quot;).toDispatch();<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(textRange, &quot;Paste&quot;);<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp;/**<br />
&nbsp; * 在当前文档指定的位置拷贝表格<br />
&nbsp; * <br />
&nbsp; * @param pos<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当前文档指定的位置<br />
&nbsp; * @param tableIndex<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 被拷贝的表格在word文档中所处的位置<br />
&nbsp; */<br />
&nbsp;public void copyTable(String pos, int tableIndex) {<br />
&nbsp;&nbsp;// 所有表格<br />
&nbsp;&nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;// 要填充的表格<br />
&nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;, new Variant(tableIndex))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;Dispatch range = Dispatch.get(table, &quot;Range&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(range, &quot;Copy&quot;);<br />
&nbsp;&nbsp;if (this.find(pos)) {<br />
&nbsp;&nbsp;&nbsp;Dispatch textRange = Dispatch.get(selection, &quot;Range&quot;).toDispatch();<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(textRange, &quot;Paste&quot;);<br />
&nbsp;&nbsp;}<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 在当前文档指定的位置拷贝来自另一个文档中的表格<br />
&nbsp; * <br />
&nbsp; * @param anotherDocPath<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 另一个文档的磁盘路径<br />
&nbsp; * @param tableIndex<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 被拷贝的表格在另一格文档中的位置<br />
&nbsp; * @param pos<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当前文档指定的位置<br />
&nbsp; */<br />
&nbsp;public void copyTableFromAnotherDoc(String anotherDocPath, int tableIndex,<br />
&nbsp;&nbsp;&nbsp;String pos) {<br />
&nbsp;&nbsp;Dispatch doc2 = null;<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;doc2 = Dispatch.call(documents, &quot;Open&quot;, anotherDocPath)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;&nbsp;// 所有表格<br />
&nbsp;&nbsp;&nbsp;Dispatch tables = Dispatch.get(doc2, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;&nbsp;// 要填充的表格<br />
&nbsp;&nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new Variant(tableIndex)).toDispatch();<br />
&nbsp;&nbsp;&nbsp;Dispatch range = Dispatch.get(table, &quot;Range&quot;).toDispatch();<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(range, &quot;Copy&quot;);<br />
&nbsp;&nbsp;&nbsp;if (this.find(pos)) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;Dispatch textRange = Dispatch.get(selection, &quot;Range&quot;).toDispatch();<br />
&nbsp;&nbsp;&nbsp;&nbsp;Dispatch.call(textRange, &quot;Paste&quot;);<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;} catch (Exception e) {<br />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;} finally {<br />
&nbsp;&nbsp;&nbsp;if (doc2 != null) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;Dispatch.call(doc2, &quot;Close&quot;, new Variant(saveOnExit));<br />
&nbsp;&nbsp;&nbsp;&nbsp;doc2 = null;<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
&nbsp;}<br />
&nbsp;/**<br />
&nbsp; * 在当前文档指定的位置拷贝来自另一个文档中的图片<br />
&nbsp; * @param anotherDocPath 另一个文档的磁盘路径<br />
&nbsp; * @param shapeIndex 被拷贝的图片在另一格文档中的位置<br />
&nbsp; * @param pos 当前文档指定的位置<br />
&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public void copyImageFromAnotherDoc(String anotherDocPath,int shapeIndex,String pos){<br />
&nbsp;&nbsp;&nbsp; &nbsp;Dispatch doc2 = null;<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;doc2 = Dispatch.call(documents, &quot;Open&quot;, anotherDocPath)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;&nbsp;Dispatch shapes = Dispatch.get(doc2, &quot;InLineShapes&quot;).toDispatch();<br />
&nbsp;&nbsp;&nbsp;Dispatch shape = Dispatch.call(shapes, &quot;Item&quot;, new Variant(shapeIndex)).toDispatch();<br />
&nbsp;&nbsp;&nbsp;Dispatch imageRange = Dispatch.get(shape, &quot;Range&quot;).toDispatch();<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(imageRange, &quot;Copy&quot;);<br />
&nbsp;&nbsp;&nbsp;if (this.find(pos)) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;Dispatch textRange = Dispatch.get(selection, &quot;Range&quot;).toDispatch();<br />
&nbsp;&nbsp;&nbsp;&nbsp;Dispatch.call(textRange, &quot;Paste&quot;);<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;} catch (Exception e) {<br />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;} finally {<br />
&nbsp;&nbsp;&nbsp;if (doc2 != null) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;Dispatch.call(doc2, &quot;Close&quot;, new Variant(saveOnExit));<br />
&nbsp;&nbsp;&nbsp;&nbsp;doc2 = null;<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp; }<br />
&nbsp;/**<br />
&nbsp; * 创建表格<br />
&nbsp; * <br />
&nbsp; * @param pos<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 位置<br />
&nbsp; * @param cols<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 列数<br />
&nbsp; * @param rows<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 行数<br />
&nbsp; */<br />
&nbsp;public void createTable(String pos, int numCols, int numRows) {<br />
&nbsp;&nbsp;if (find(pos)) {<br />
&nbsp;&nbsp;&nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;&nbsp;Dispatch range = Dispatch.get(selection, &quot;Range&quot;).toDispatch();<br />
&nbsp;&nbsp;&nbsp;Dispatch newTable = Dispatch.call(tables, &quot;Add&quot;, range,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new Variant(numRows), new Variant(numCols)).toDispatch();<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(selection, &quot;MoveRight&quot;);<br />
&nbsp;&nbsp;}<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 在指定行前面增加行<br />
&nbsp; * <br />
&nbsp; * @param tableIndex<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; word文件中的第N张表(从1开始)<br />
&nbsp; * @param rowIndex<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 指定行的序号(从1开始)<br />
&nbsp; */<br />
&nbsp;public void addTableRow(int tableIndex, int rowIndex) {<br />
&nbsp;&nbsp;// 所有表格<br />
&nbsp;&nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;// 要填充的表格<br />
&nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;, new Variant(tableIndex))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;// 表格的所有行<br />
&nbsp;&nbsp;Dispatch rows = Dispatch.get(table, &quot;Rows&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch row = Dispatch.call(rows, &quot;Item&quot;, new Variant(rowIndex))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(rows, &quot;Add&quot;, new Variant(row));<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 在第1行前增加一行<br />
&nbsp; * <br />
&nbsp; * @param tableIndex<br />
&nbsp; *&nbsp; word文档中的第N张表(从1开始)<br />
&nbsp; */<br />
&nbsp;public void addFirstTableRow(int tableIndex) {<br />
&nbsp;&nbsp;// 所有表格<br />
&nbsp;&nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;// 要填充的表格<br />
&nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;, new Variant(tableIndex))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;// 表格的所有行<br />
&nbsp;&nbsp;Dispatch rows = Dispatch.get(table, &quot;Rows&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch row = Dispatch.get(rows, &quot;First&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(rows, &quot;Add&quot;, new Variant(row));<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 在最后1行前增加一行<br />
&nbsp; * <br />
&nbsp; * @param tableIndex<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; word文档中的第N张表(从1开始)<br />
&nbsp; */<br />
&nbsp;public void addLastTableRow(int tableIndex) {<br />
&nbsp;&nbsp;// 所有表格<br />
&nbsp;&nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;// 要填充的表格<br />
&nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;, new Variant(tableIndex))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;// 表格的所有行<br />
&nbsp;&nbsp;Dispatch rows = Dispatch.get(table, &quot;Rows&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch row = Dispatch.get(rows, &quot;Last&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(rows, &quot;Add&quot;, new Variant(row));<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 增加一行<br />
&nbsp; * <br />
&nbsp; * @param tableIndex<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; word文档中的第N张表(从1开始)<br />
&nbsp; */<br />
&nbsp;public void addRow(int tableIndex) {<br />
&nbsp;&nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;// 要填充的表格<br />
&nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;, new Variant(tableIndex))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;// 表格的所有行<br />
&nbsp;&nbsp;Dispatch rows = Dispatch.get(table, &quot;Rows&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(rows, &quot;Add&quot;);<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 增加一列<br />
&nbsp; * <br />
&nbsp; * @param tableIndex<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; word文档中的第N张表(从1开始)<br />
&nbsp; */<br />
&nbsp;public void addCol(int tableIndex) {<br />
&nbsp;&nbsp;// 所有表格<br />
&nbsp;&nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;// 要填充的表格<br />
&nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;, new Variant(tableIndex))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;// 表格的所有行<br />
&nbsp;&nbsp;Dispatch cols = Dispatch.get(table, &quot;Columns&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(cols, &quot;Add&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(cols, &quot;AutoFit&quot;);<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 在指定列前面增加表格的列<br />
&nbsp; * <br />
&nbsp; * @param tableIndex<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; word文档中的第N张表(从1开始)<br />
&nbsp; * @param colIndex<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 制定列的序号 (从1开始)<br />
&nbsp; */<br />
&nbsp;public void addTableCol(int tableIndex, int colIndex) {<br />
&nbsp;&nbsp;// 所有表格<br />
&nbsp;&nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;// 要填充的表格<br />
&nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;, new Variant(tableIndex))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;// 表格的所有行<br />
&nbsp;&nbsp;Dispatch cols = Dispatch.get(table, &quot;Columns&quot;).toDispatch();<br />
&nbsp;&nbsp;System.out.println(Dispatch.get(cols, &quot;Count&quot;));<br />
&nbsp;&nbsp;Dispatch col = Dispatch.call(cols, &quot;Item&quot;, new Variant(colIndex))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;// Dispatch col = Dispatch.get(cols, &quot;First&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(cols, &quot;Add&quot;, col).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(cols, &quot;AutoFit&quot;);<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 在第1列前增加一列<br />
&nbsp; * <br />
&nbsp; * @param tableIndex<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; word文档中的第N张表(从1开始)<br />
&nbsp; */<br />
&nbsp;public void addFirstTableCol(int tableIndex) {<br />
&nbsp;&nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;// 要填充的表格<br />
&nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;, new Variant(tableIndex))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;// 表格的所有行<br />
&nbsp;&nbsp;Dispatch cols = Dispatch.get(table, &quot;Columns&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch col = Dispatch.get(cols, &quot;First&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(cols, &quot;Add&quot;, col).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(cols, &quot;AutoFit&quot;);<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 在最后一列前增加一列<br />
&nbsp; * <br />
&nbsp; * @param tableIndex<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; word文档中的第N张表(从1开始)<br />
&nbsp; */<br />
&nbsp;public void addLastTableCol(int tableIndex) {<br />
&nbsp;&nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;// 要填充的表格<br />
&nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;, new Variant(tableIndex))<br />
&nbsp;&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;// 表格的所有行<br />
&nbsp;&nbsp;Dispatch cols = Dispatch.get(table, &quot;Columns&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch col = Dispatch.get(cols, &quot;Last&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(cols, &quot;Add&quot;, col).toDispatch();<br />
&nbsp;&nbsp;Dispatch.call(cols, &quot;AutoFit&quot;);<br />
&nbsp;}<br />
&nbsp;/**<br />
&nbsp; * 自动调整表格<br />
&nbsp; *<br />
&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public void autoFitTable(){<br />
&nbsp;&nbsp;&nbsp; &nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;&nbsp; &nbsp;int count = Dispatch.get(tables, &quot;Count&quot;).toInt();<br />
&nbsp;&nbsp;&nbsp; &nbsp;for(int i=0;i &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;, new Variant(i+1))<br />
&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;Dispatch cols = Dispatch.get(table, &quot;Columns&quot;).toDispatch();<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;Dispatch.call(cols, &quot;AutoFit&quot;);<br />
&nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * 调用word里的宏以调整表格的宽度,其中宏保存在document下<br />
&nbsp;&nbsp;&nbsp;&nbsp; *<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public void callWordMacro(){<br />
&nbsp;&nbsp;&nbsp; &nbsp;Dispatch tables = Dispatch.get(doc, &quot;Tables&quot;).toDispatch();<br />
&nbsp;&nbsp;&nbsp; &nbsp;int count = Dispatch.get(tables, &quot;Count&quot;).toInt();<br />
&nbsp;&nbsp;&nbsp; &nbsp;Variant vMacroName = new Variant(&quot;Normal.NewMacros.tableFit&quot;);<br />
&nbsp;&nbsp;&nbsp; &nbsp;Variant vParam = new Variant(&quot;param1&quot;);<br />
&nbsp;&nbsp;&nbsp; &nbsp;Variant para[]=new Variant[]{vMacroName};<br />
&nbsp;&nbsp;&nbsp; &nbsp;for(int i=0;i &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;Dispatch table = Dispatch.call(tables, &quot;Item&quot;, new Variant(i+1))<br />
&nbsp;&nbsp;&nbsp;.toDispatch();<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;Dispatch.call(table, &quot;Select&quot;);<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;Dispatch.call(word,&quot;Run&quot;,&quot;tableFitContent&quot;);<br />
&nbsp;&nbsp;&nbsp; &nbsp;}&nbsp;<br />
&nbsp;&nbsp;&nbsp; }<br />
&nbsp;/**<br />
&nbsp; * 设置当前选定内容的字体<br />
&nbsp; * <br />
&nbsp; * @param boldSize<br />
&nbsp; * @param italicSize<br />
&nbsp; * @param underLineSize<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 下划线<br />
&nbsp; * @param colorSize<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 字体颜色<br />
&nbsp; * @param size<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 字体大小<br />
&nbsp; * @param name<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 字体名称<br />
&nbsp; */<br />
&nbsp;public void setFont(boolean bold, boolean italic, boolean underLine,<br />
&nbsp;&nbsp;&nbsp;String colorSize, String size, String name) {<br />
&nbsp;&nbsp;Dispatch font = Dispatch.get(selection, &quot;Font&quot;).toDispatch();<br />
&nbsp;&nbsp;Dispatch.put(font, &quot;Name&quot;, new Variant(name));<br />
&nbsp;&nbsp;Dispatch.put(font, &quot;Bold&quot;, new Variant(bold));<br />
&nbsp;&nbsp;Dispatch.put(font, &quot;Italic&quot;, new Variant(italic));<br />
&nbsp;&nbsp;Dispatch.put(font, &quot;Underline&quot;, new Variant(underLine));<br />
&nbsp;&nbsp;Dispatch.put(font, &quot;Color&quot;, colorSize);<br />
&nbsp;&nbsp;Dispatch.put(font, &quot;Size&quot;, size);<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 文件保存或另存为<br />
&nbsp; * <br />
&nbsp; * @param savePath<br />
&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 保存或另存为路径<br />
&nbsp; */<br />
&nbsp;public void save(String savePath) {<br />
&nbsp;&nbsp;Dispatch.call(Dispatch.call(word, &quot;WordBasic&quot;).getDispatch(),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&quot;FileSaveAs&quot;, savePath);<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 关闭当前word文档<br />
&nbsp; * <br />
&nbsp; */<br />
&nbsp;public void closeDocument() {<br />
&nbsp;&nbsp;if (doc != null) {<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(doc, &quot;Save&quot;);<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(doc, &quot;Close&quot;, new Variant(saveOnExit));<br />
&nbsp;&nbsp;&nbsp;doc = null;<br />
&nbsp;&nbsp;}<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 关闭全部应用<br />
&nbsp; * <br />
&nbsp; */<br />
&nbsp;public void close() {<br />
&nbsp;&nbsp;closeDocument();<br />
&nbsp;&nbsp;if (word != null) {<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(word, &quot;Quit&quot;);<br />
&nbsp;&nbsp;&nbsp;word = null;<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;selection = null;<br />
&nbsp;&nbsp;documents = null;<br />
&nbsp;}</font></p>
<p><font face="Arial">&nbsp;/**<br />
&nbsp; * 打印当前word文档<br />
&nbsp; * <br />
&nbsp; */<br />
&nbsp;public void printFile() {<br />
&nbsp;&nbsp;if (doc != null) {<br />
&nbsp;&nbsp;&nbsp;Dispatch.call(doc, &quot;PrintOut&quot;);<br />
&nbsp;&nbsp;}<br />
&nbsp;}<br />
&nbsp;public static void main(String args[]) {<br />
&nbsp;&nbsp;MSWordManager msWordManager = new MSWordManager();<br />
&nbsp;&nbsp;try {<br />
&nbsp;&nbsp;&nbsp;msWordManager.openDocument(&quot;D:\\SUPERADMIN20061121100.doc&quot;);<br />
&nbsp;&nbsp;&nbsp;msWordManager.callWordMacro();<br />
&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;} catch (Exception e) {<br />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;msWordManager.close();<br />
&nbsp;}</font></p>
<p><font face="Arial">}<br />
</font></p>
          <br/>
          <span style="color:red;">
            <a href="http://zhuxue-feng.javaeye.com/blog/35350#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Tue, 21 Nov 2006 14:52:00 +0800</pubDate>
        <link>http://zhuxue-feng.javaeye.com/blog/35350</link>
        <guid>http://zhuxue-feng.javaeye.com/blog/35350</guid>
      </item>
      <item>
        <title>选择人生的路</title>
        <author>笨鸟先飞</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://zhuxue-feng.javaeye.com">笨鸟先飞</a>&nbsp;
          链接：<a href="http://zhuxue-feng.javaeye.com/blog/32272" style="color:red;">http://zhuxue-feng.javaeye.com/blog/32272</a>&nbsp;
          发表时间: 2006年11月03日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <span style="font-size: small">最近不时的冒出做生意的想法，晚上躺在床上，静静的想，回想这4年间的点点滴滴，刚走出校门的我，找了一个和所学专业对口的工作，那是在一个私营企业里面，老板以及老板娘他们对我们这些大学生还是不错的，公司里面还有一个算和我一起来的老乡，我的这位老乡和我性格差不多，都是那种生性豪爽之人，那时的我们每周六下班后，买了一只鸡，两瓶啤酒，骑着车来到一块空的草萍上，喝着小酒，无所不谈，真有那种煮酒论英雄的感觉，通宵游戏厮杀后，休息日也就在睡梦中度过了，每月的工资所剩多少就可想而之了，那时，正是夏天，天气异常的热，我天天骑着自行车在昆山开发区寻找着客户，也有一段时间去周庄做一段时间的工程，现在回想起那段日子，虽然苦了一点，累了一点，但那时我们还是很快乐的，人生真的很奇妙，往往一件事就能改变了你，对我来说，那时能改变我的就是从初中，更确切的说是从初一的时候就认识的那个她，很显然她是一个女孩，并且我喜欢上她了，这要从上初中说起，我记得就是在初二上半年的一个冬天的早上，教室外面是昨晚下了一夜厚厚的雪，她由于家庭作业没带来，老师很严厉地让她马上回去把作业取回来，就在我们的早读课还没有结束的时候，她拿回来了，她证实了她没有说谎，穿着红色的尼子套服从寒冷的门外半低着头快步地走入到她的座位上，口里随着略带急促的呼吸，吐着热气，脸庞通红，通红的，就在那一刻，情窦初开的我情不自禁的喜欢上她了，真的太美了，从那时起，上课时我看着她，下课时只要她在教室里，也同样偷偷的看着她，有时候她会回回头，我瞬间把目光转向老师的黑板上，那时候有很多甜蜜的梦，和她一起上高中，一起上大学&hellip;&hellip;，就在高中开学前几天还在胡思乱想的我，高中开学时，原来她和我在一班，我很清楚得记得那天我快乐的飞速的骑着自行车，那是我由始以来骑车最快的一次，如果那时的我像小说里写的那样就好了，她的成绩不断的在提高，而我总是在原地，各个方面都表现的很差，家里比较穷，又有好多事情困扰着我，从此我开始漫长的自卑之路，对自己做的任何事全盘否定，在上课时，大家都聚集绘神的听讲时，我却偷偷得看着她，或者看着窗外，就这样度过了高中的生活，就在高中生活的最后一天，我在她语文书里塞了一张卡片，具体写什么我现在记不清了，应该是一些祝福的话，署名是&ldquo;流星&rdquo; 。卡片是在前一天晚上写的，由于我的字写得很不好，字体是卡通形的，整整花我两个小时，那天晚上我妈陪着我，她以为我是在学习，她也知道我高考可能不会有什么希望，但她还是期望着，可能老天爷对我还有点偏爱，我勉强上了大学，在上大学期间我给她总共做了两件事，一件事是给她寄了一张卡片，那是在大一的圣诞，她也给我回了一张，署名相对比较亲呢，于是我鼓起勇气做了第二件事，我把我打工7天所挣来的薪水的一半换了一只蝴蝶，一个可以挂在胸前的玻璃挂缀，玻璃里面还有一个小昆虫。打工也就是做些类似于体力活的事情，所以没有多少钱。不过这次打工是我第一次挣钱，尽管很少，我却是非常的开心，因为我可以用我自己的劳动所得买礼物送给我心爱的人，接续&hellip;&hellip;</span><p><span style="font-size: small">&nbsp;</span></p>
          <br/>
          <span style="color:red;">
            <a href="http://zhuxue-feng.javaeye.com/blog/32272#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/92' target='_blank'><span style="color:red;font-weight:bold;">快来参加7月17日在成都举行的SOA中国技术论坛</span></a></li><li><a href='/adverts/97' target='_blank'><span style="color:blue;font-weight:bold;">Oracle专区上线，有Oracle最新文章，重要下载及知识库等精彩内容，欢迎访问。</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/106' target='_blank'><span style="color:blue;font-weight:bold;">JavaEye问答大赛开始了！ 从6月23日 至 7月6日，奖品丰厚 ！</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Fri, 03 Nov 2006 02:37:49 +0800</pubDate>
        <link>http://zhuxue-feng.javaeye.com/blog/32272</link>
        <guid>http://zhuxue-feng.javaeye.com/blog/32272</guid>
      </item>
  </channel>
</rss>