??xml version="1.0" encoding="utf-8" standalone="yes"?>欧美日韩亚洲天堂,国产亚洲精品自在久久,亚洲精品一区视频http://www.aygfsteel.com/czcode/category/18133.htmlzh-cnThu, 15 May 2008 12:39:33 GMTThu, 15 May 2008 12:39:33 GMT60Struts 2多文件上?/title><link>http://www.aygfsteel.com/czcode/archive/2008/05/15/200582.html</link><dc:creator>崔振</dc:creator><author>崔振</author><pubDate>Thu, 15 May 2008 02:42:00 GMT</pubDate><guid>http://www.aygfsteel.com/czcode/archive/2008/05/15/200582.html</guid><wfw:comment>http://www.aygfsteel.com/czcode/comments/200582.html</wfw:comment><comments>http://www.aygfsteel.com/czcode/archive/2008/05/15/200582.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/czcode/comments/commentRss/200582.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/czcode/services/trackbacks/200582.html</trackback:ping><description><![CDATA[     摘要:   <a href='http://www.aygfsteel.com/czcode/archive/2008/05/15/200582.html'>阅读全文</a><img src ="http://www.aygfsteel.com/czcode/aggbug/200582.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/czcode/" target="_blank">崔振</a> 2008-05-15 10:42 <a href="http://www.aygfsteel.com/czcode/archive/2008/05/15/200582.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>struts2拦截器概q?/title><link>http://www.aygfsteel.com/czcode/archive/2008/05/15/200581.html</link><dc:creator>崔振</dc:creator><author>崔振</author><pubDate>Thu, 15 May 2008 02:39:00 GMT</pubDate><guid>http://www.aygfsteel.com/czcode/archive/2008/05/15/200581.html</guid><wfw:comment>http://www.aygfsteel.com/czcode/comments/200581.html</wfw:comment><comments>http://www.aygfsteel.com/czcode/archive/2008/05/15/200581.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/czcode/comments/commentRss/200581.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/czcode/services/trackbacks/200581.html</trackback:ping><description><![CDATA[     摘要:   <a href='http://www.aygfsteel.com/czcode/archive/2008/05/15/200581.html'>阅读全文</a><img src ="http://www.aygfsteel.com/czcode/aggbug/200581.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/czcode/" target="_blank">崔振</a> 2008-05-15 10:39 <a href="http://www.aygfsteel.com/czcode/archive/2008/05/15/200581.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Struts2 在ActioncM获得HttpServletResponse对象的四U方?/title><link>http://www.aygfsteel.com/czcode/archive/2008/05/15/200580.html</link><dc:creator>崔振</dc:creator><author>崔振</author><pubDate>Thu, 15 May 2008 02:37:00 GMT</pubDate><guid>http://www.aygfsteel.com/czcode/archive/2008/05/15/200580.html</guid><wfw:comment>http://www.aygfsteel.com/czcode/comments/200580.html</wfw:comment><comments>http://www.aygfsteel.com/czcode/archive/2008/05/15/200580.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.aygfsteel.com/czcode/comments/commentRss/200580.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/czcode/services/trackbacks/200580.html</trackback:ping><description><![CDATA[     摘要:   <a href='http://www.aygfsteel.com/czcode/archive/2008/05/15/200580.html'>阅读全文</a><img src ="http://www.aygfsteel.com/czcode/aggbug/200580.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/czcode/" target="_blank">崔振</a> 2008-05-15 10:37 <a href="http://www.aygfsteel.com/czcode/archive/2008/05/15/200580.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Collection接口的ȝ http://www.aygfsteel.com/czcode/archive/2007/04/18/111549.html崔振崔振Wed, 18 Apr 2007 03:18:00 GMThttp://www.aygfsteel.com/czcode/archive/2007/04/18/111549.htmlhttp://www.aygfsteel.com/czcode/comments/111549.htmlhttp://www.aygfsteel.com/czcode/archive/2007/04/18/111549.html#Feedback0http://www.aygfsteel.com/czcode/comments/commentRss/111549.htmlhttp://www.aygfsteel.com/czcode/services/trackbacks/111549.html Map接口Q?/strong>
    |
    + -- WeakHashMap: 以弱?实现的基于哈希表?Map。在 WeakHashMap 中,当某个键不再正常使用Ӟ自动移除其?br>    |      目。更_地说Q对于一个给定的键,其映的存在q不L垃圾回收器对该键的丢弃,q就使该键成为可l止的,被终
    |      止,然后被回收。丢弃某个键Ӟ其条目从映射中有效地U除Q因此,该类的行Z其他?Map 实现有所不同?font color=#ff0000>此实?/font>
    |      不是同步的?/font>
    |
    + -- TreeMap:该映根据其键的自然序q行排序Q或者根据创建映时提供?Comparator q行排序Q具体取决于使用?br>    |    构造方法?font color=#ff0000>此实C是同步的?/font>
    |
    + -- HashMap:Z哈希表的 Map 接口的实现。此实现提供所有可选的映射操作Qƈ允许使用 null 值和 null 键。(除了      
    |        非同步和允许使用 null 之外QHashMap cM Hashtable 大致相同。)此类不保证映的序Q特别是它不保证该顺   
    |       序恒久不变?font color=#ff0000>此实C是同步的?br>    |
    +-- SortedMap: q一步提供关于键的M排序 ?Map。该映射是根据其键的自然序q行排序的,或者根据通常在创建有
         序映时提供?Comparator q行排序。对有序映射?collection 视图Q由 entrySet、keySet ?values Ҏ(gu)q回
         Q进行P代时Q此序׃(x)反映出来。要采用此排序方式,q需要提供一些其他操作(此接口是 SortedSet 的对应映
         )?br>
Collection接口Q?/strong>
    |
    + -- Set接口:一个不包含重复元素?collection。更正式地说Qset 不包含满?e1.equals(e2) 的元素对 e1 ?e2Qƈ
    |      |     且最多包含一?null 元素。正如其名称所暗示的,此接口模仿了数学上的 set 抽象?br>    |      |
    |      + -- HashSet:此类实现 Set 接口Q由哈希表(实际上是一?HashMap 实例Q支持。它不保?set 的P代顺序;
    |      |    特别是它不保证该序恒久不变。此cd怋?null 元素。此cMؓ(f)基本操作提供了稳定性能Q?font color=#ff0000>此实C是同
    |      |    步的?/font>
    |      |
    |      + -- LinkedHashSet:h可预知P代顺序的 Set 接口的哈希表和链接列表实现。此实现?HashSet 的不同之外在
    |      |    于,后者维护着一个运行于所有条目的双重链接列表。此链接列表定义了P代顺序,x照将元素插入?set
    |      |    ?的顺序(插入序Q进行P代。注意,插入序?受在 set 中重新插入的 元素的媄响?font color=#ff0000>此实C是同?/font>
    |      |    的?
    |      |
    |      + -- TreeSet:Z TreeMap ?NavigableSet 实现。用元素的自然序对元素进行排序,或者根据创?set ?br>    |           提供?Comparator q行排序Q具体取决于使用的构造方法。此实现为基本操作(add、remove ?containsQ?br>    |           提供受保证的 log(n) 旉开销?font color=#ff0000>此实C是同步的?/font>
    |
    + -- List接口:有序?collectionQ也UCؓ(f)序列Q。此接口的用户可以对列表中每个元素的插入位置q行_地控制。用?br>           |      可以Ҏ(gu)元素的整数烦引(在列表中的位|)讉K元素Qƈ搜烦列表中的元素?br>           |
           + -- ArrayList:List 接口的大可变数l的实现。实C所有可选列表操作,q允许包?null 在内的所有元素?br>           |    除了实现 List 接口外,此类q提供一些方法来操作内部用来存储列表的数l的大小。(此类大致上等同于
           |    Vector c,除了此类是不同步的。)每个 ArrayList 实例都有一个容量。该定w是指用来存储列表元素的数
           |    l的大小。它L臛_{于列表的大。随着?ArrayList 中不断添加元素,其容量也自动增长。ƈ未指定增
           |    长策略的l节Q因不只是添加元素会(x)带来分摊固定旉开销那样单?font color=#ff0000>此实C是同步的?/font>
           |
           + -- LinkedList:List 接口的链接列表实现。实现所有可选的列表操作Qƈ且允许所有元素(包括 nullQ。除了实
           |    ?List 接口外,LinkedList c还为在列表的开头及(qing)l尾 get、remove ?insert 元素提供了统一的命名方
           |    法。这些操作允许将链接列表用作堆栈、队列或双端队列?font color=#ff0000>提供先进先出队列操作(FIFO)。此实现不是同步的?/font>
           |
           + -- Vector:Vector cd以实现可增长的对象数l。与数组一P它包含可以用整数烦引进行访问的lg。但?br>                QVector 的大可以根据需要增大或~小Q以适应创徏 Vector 后进行添加或U除的操作?font color=#ff0000>此实现是同步?br>                ?/font>

崔振 2007-04-18 11:18 发表评论
]]>
tomcat5.5日志http://www.aygfsteel.com/czcode/archive/2007/01/09/92690.html崔振崔振Tue, 09 Jan 2007 10:42:00 GMThttp://www.aygfsteel.com/czcode/archive/2007/01/09/92690.htmlhttp://www.aygfsteel.com/czcode/comments/92690.htmlhttp://www.aygfsteel.com/czcode/archive/2007/01/09/92690.html#Feedback0http://www.aygfsteel.com/czcode/comments/commentRss/92690.htmlhttp://www.aygfsteel.com/czcode/services/trackbacks/92690.html自从用上了tomcat5.5Q发现日志信息没了,出错了也找不着有用的信息,上apache扄案,果然

Tomcat 5.5 uses Commons Logging throughout its internal code allowing the developer to choose a logging configuration that suits their needs, e.g java.util.logging or Log4J. Commons Logging provides Tomcat the ability to log hierarchially across various log levels without needing to rely on a particular logging implementation.

An important consequence for Tomcat 5.5 is that the <Logger> element found in previous versions to create a localhost_log is no longer a valid nested element of <Context>. Instead, the default Tomcat configuration will use java.util.logging. If the developer wishes to collect detailed internal Tomcat logging (i.e what is happening within the Tomcat engine), then they should configure a logging system such as java.util.logging or log4j as detailed next

?fn)惯用log4j来配|log信息的输出?/p>

新徏log4j.propertiesQ内容ؓ(f)

log4j.rootLogger=info,Console,R

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
#log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.Console.layout.ConversionPattern=%d{yy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/tomcat.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{yyyy.MM.dd HH:mm:ss} %5p %c{1}(%L):  %m%n

log4j.logger.org.apache=info, R
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=DEBUG, R
log4j.logger.org.apache.catalina.core=info, R
log4j.logger.org.apache.catalina.session=info, R

最后四行是tomcat的信息,如果改ؓ(f)debug日志文g十分庞大?/p>

这个文件放?{catalina.home}/common/classes下,再将log4j.jar和commons-logging.jar攑օ${catalina.home}/common/lib下,可以在${catalina.home}/logs/下见到日志了?/p>



崔振 2007-01-09 18:42 发表评论
]]>
怎样动态添加文本框q处?/title><link>http://www.aygfsteel.com/czcode/archive/2007/01/04/91752.html</link><dc:creator>崔振</dc:creator><author>崔振</author><pubDate>Thu, 04 Jan 2007 06:44:00 GMT</pubDate><guid>http://www.aygfsteel.com/czcode/archive/2007/01/04/91752.html</guid><wfw:comment>http://www.aygfsteel.com/czcode/comments/91752.html</wfw:comment><comments>http://www.aygfsteel.com/czcode/archive/2007/01/04/91752.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.aygfsteel.com/czcode/comments/commentRss/91752.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/czcode/services/trackbacks/91752.html</trackback:ping><description><![CDATA[ <p>我在做项目时曄到这个问题,怎样动态地d一个文本框或文件域Q这在网上很Ҏ(gu)扑ֈ。但Ҏ(gu)样处理提交的动态数据就很少有这斚w的信息了。本人翻阅了一些资料,把自qҎ(gu)写了下来Q以飨读者?/p> <p>1.提交表单</p> <p> </p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <span style="COLOR: #000000"><html><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /><body><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <form name</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">form</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> method</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">post</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> action</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">add.jsp</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />   <input name</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">button</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> type</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">button onClick</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">'additem(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">tb</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">)' value</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">d>></span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <table id</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">tb</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </table><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /><script language</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">javascript</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />function additem(id)<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />  var row</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">cell</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">str</span> <span style="COLOR: #008000">;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #000000">  row </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> eval(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">document.all[</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+id+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">]</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">).insertRow()</span> <span style="COLOR: #008000">;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #000000">  if(row !</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> null )<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />     {<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />        cell </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> row.insertCell()</span> <span style="COLOR: #008000">;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #000000">        str</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"><input type=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">text</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> name=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">StuName</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">><input type=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">button</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> value=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">删除</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> onclick='deleteitem(this,</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">tb</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">);'></span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />   cell.innerHTML</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">str</span> <span style="COLOR: #008000">;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />      }<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />}<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />function deleteitem(obj</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">id)<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />{<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />  var rowNum</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">curRow</span> <span style="COLOR: #008000">;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #000000">  curRow </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> obj.parentNode.parentNode</span> <span style="COLOR: #008000">;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #000000">  rowNum </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> eval(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">document.all.</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+id).rows.length - </span> <span style="COLOR: #000000">1</span> <span style="COLOR: #008000">;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #000000">  eval(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">document.all[</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+id+'</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">'+</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">]</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">).deleteRow(curRow.rowIndex)</span> <span style="COLOR: #008000">;<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> <span style="COLOR: #000000">}<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></script><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />   <p><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />     <input type</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">submit</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> name</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">submit</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000"> value</span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">提交</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" />   </p><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </form><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></body><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></html><br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> </div> <p> </p> <p>2.处理数据</p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <span style="COLOR: #0000ff"><</span> <span style="COLOR: #800000">body</span> <span style="COLOR: #0000ff">></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff"><</span> <span style="COLOR: #800000">table </span> <span style="COLOR: #ff0000">border</span> <span style="COLOR: #0000ff">="1"</span> <span style="COLOR: #0000ff">><</span> <span style="COLOR: #800000">tr</span> <span style="COLOR: #0000ff">><</span> <span style="COLOR: #800000">td </span> <span style="COLOR: #ff0000">colspan</span> <span style="COLOR: #0000ff">="2"</span> <span style="COLOR: #0000ff">></span> <span style="COLOR: #000000">eg</span> <span style="COLOR: #0000ff"></</span> <span style="COLOR: #800000">td</span> <span style="COLOR: #0000ff">></</span> <span style="COLOR: #800000">tr</span> <span style="COLOR: #0000ff">></span> <span style="COLOR: #000000"> <br /> <img id="Codehighlighter1_60_261_Open_Image" onclick="this.style.display='none'; Codehighlighter1_60_261_Open_Text.style.display='none'; Codehighlighter1_60_261_Closed_Image.style.display='inline'; Codehighlighter1_60_261_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /> <img id="Codehighlighter1_60_261_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_60_261_Closed_Text.style.display='none'; Codehighlighter1_60_261_Open_Image.style.display='inline'; Codehighlighter1_60_261_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /> </span> <span style="COLOR: #000000; BACKGROUND-COLOR: #ffff00"><%</span> <span id="Codehighlighter1_60_261_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_60_261_Open_Text"> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />Enumeration params</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">request.getParameterNames();<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /></span> <span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">while</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">(params.hasMoreElements()){<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /></span> <span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">String</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> name</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">(</span> <span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">String</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">)params.nextElement();<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /></span> <span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">String</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">[] values</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">request.getParameterValues(name);<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" /></span> <span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">if</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">(name.equals(</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">StuName</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">)){<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> </span> <span style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">%></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff"><</span> <span style="COLOR: #800000">tr</span> <span style="COLOR: #0000ff">></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff"><</span> <span style="COLOR: #800000">td</span> <span style="COLOR: #0000ff">></span> <span style="COLOR: #000000; BACKGROUND-COLOR: #ffff00"><%</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">name</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">%></span> <span style="COLOR: #0000ff"></</span> <span style="COLOR: #800000">td</span> <span style="COLOR: #0000ff">></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff"><</span> <span style="COLOR: #800000">td</span> <span style="COLOR: #0000ff">></span> <span style="COLOR: #000000"> <br /> <img id="Codehighlighter1_296_342_Open_Image" onclick="this.style.display='none'; Codehighlighter1_296_342_Open_Text.style.display='none'; Codehighlighter1_296_342_Closed_Image.style.display='inline'; Codehighlighter1_296_342_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /> <img id="Codehighlighter1_296_342_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_296_342_Closed_Text.style.display='none'; Codehighlighter1_296_342_Open_Image.style.display='inline'; Codehighlighter1_296_342_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /> </span> <span style="COLOR: #000000; BACKGROUND-COLOR: #ffff00"><%</span> <span id="Codehighlighter1_296_342_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_296_342_Open_Text"> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" /> </span> <span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">for</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">(</span> <span style="COLOR: #0000ff; BACKGROUND-COLOR: #f5f5f5">int</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"> index</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">0</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">;index</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"><</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">values.length;index</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">++</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">){<br /><img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /></span> </span> <span style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">%></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #000000; BACKGROUND-COLOR: #ffff00"><%</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">values[index]</span> <span style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">%></span> <span style="COLOR: #000000"> <br /> <img id="Codehighlighter1_367_368_Open_Image" onclick="this.style.display='none'; Codehighlighter1_367_368_Open_Text.style.display='none'; Codehighlighter1_367_368_Closed_Image.style.display='inline'; Codehighlighter1_367_368_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /> <img id="Codehighlighter1_367_368_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_367_368_Closed_Text.style.display='none'; Codehighlighter1_367_368_Open_Image.style.display='inline'; Codehighlighter1_367_368_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /> </span> <span style="COLOR: #000000; BACKGROUND-COLOR: #ffff00"><%</span> <span id="Codehighlighter1_367_368_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_367_368_Open_Text"> <span style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">}}</span> </span> <span style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">%></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff"></</span> <span style="COLOR: #800000">td</span> <span style="COLOR: #0000ff">></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff"></</span> <span style="COLOR: #800000">tr</span> <span style="COLOR: #0000ff">></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff"></</span> <span style="COLOR: #800000">table</span> <span style="COLOR: #0000ff">></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff"></</span> <span style="COLOR: #800000">body</span> <span style="COLOR: #0000ff">></span> <span style="COLOR: #000000"> <br /> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> </div> <br /> <img src ="http://www.aygfsteel.com/czcode/aggbug/91752.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/czcode/" target="_blank">崔振</a> 2007-01-04 14:44 <a href="http://www.aygfsteel.com/czcode/archive/2007/01/04/91752.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Servlet和Filter的url匚w以及(qing)url-pattern详解http://www.aygfsteel.com/czcode/archive/2006/12/18/88456.html崔振崔振Mon, 18 Dec 2006 02:45:00 GMThttp://www.aygfsteel.com/czcode/archive/2006/12/18/88456.htmlhttp://www.aygfsteel.com/czcode/comments/88456.htmlhttp://www.aygfsteel.com/czcode/archive/2006/12/18/88456.html#Feedback0http://www.aygfsteel.com/czcode/comments/commentRss/88456.htmlhttp://www.aygfsteel.com/czcode/services/trackbacks/88456.html?/span>filter?/span>J2EE开发中常用的技术,使用方便Q配|简单,老少皆宜。估计大多数朋友都是直接配置用,也没有关心过具体的细节,今天遇到一个问题,上网查了servlet的规范才发现Q?/span>servlet?/span>filter中的url-patternq是有一些文章在里面的,ȝ了一些东西,攑և来供大家参考,以免遇到问题又要费旉?/span>

    一Q?/font>servlet容器?/span>url的匹配过E:(x)

     

当一个请求发送到servlet容器的时候,容器先会(x)请求的url减去当前应用上下文的路径作ؓ(f)servlet的映?/span>urlQ比如我讉K的是http://localhost/test/aaa.htmlQ我的应用上下文?/span>testQ容器会(x)?/span>http://localhost/testLQ剩下的/aaa.html部分拿来?/span>servlet的映匹配。这个映匹配过E是有顺序的Q而且当有一?/span>servlet匚w成功以后Q就不会(x)ȝ?x)剩下?/span>servlet了(filter不同Q后文会(x)提到Q。其匚w规则和顺序如下:(x)

1.     _路径匚w。例子:(x)比如servletA ?/span>url-pattern?/span> /testQ?/span>servletB?/span>url-pattern?/span>/* Q这个时候,如果我访问的url?/span>http://localhost/testQ这个时候容器就?x)?/span>q行_路径匚wQ发?/span>/test正好?/span>servletA_匚wQ那么就去调?/span>servletAQ也不会(x)ȝ?x)其他?/span>servlet了?/span>

2.     最长\径匹配。例子:(x)servletA?/span>url-pattern?/span>/test/*Q?/span>servletB?/span>url-pattern?/span>/test/a/*Q此时访?/span>http://localhost/test/aӞ容器?x)选择路径最长的servlet来匹配,也就是这里的servletB?/span>

3.     扩展匚wQ如?/span>url最后一D包含扩展,容器会(x)Ҏ(gu)扩展选择合适的servlet。例子:(x)servletA?/span>url-patternQ?/span>*.action

4.     如果前面三条规则都没有找C?/span>servletQ容器会(x)Ҏ(gu)url选择对应的请求资源。如果应用定义了一?/span>default servletQ则容器?x)将h丢给default servletQ什么是default servletQ后面会(x)Ԍ?/span>

     Ҏ(gu)q个规则表,p很清楚的知道servlet的匹配过E,所以定?/span>servlet的时候也要考虑url-pattern的写法,以免出错?/span>

      对于filterQ不?x)?/span>servlet那样只匹配一?/span>servletQ因?/span>filter的集合是一个链Q所以只?x)有处理的顺序不同,而不?x)出现只选择一?/span>filter?/span>Filter的处理顺序和filter-mapping?/span>web.xml中定义的序相同?/span>

    二,url-pattern详解

         ?/span>web.xml文g中,以下语法用于定义映射Q?/span>

l  ?/span>??/span>开头和?/span>?*?/span>l尾的是用来做\径映的?/span>

l  以前~?.?/span>开头的是用来做扩展映射的?/span>

l  ??是用来定?/span>default servlet映射的?/span>

l  剩下的都是用来定义详l映的。比如:(x) /aa/bb/cc.action

所以,Z么定?/span>?*.action?/span>q样一个看h很正常的匚w?x)错Q因个匹配即属于路径映射Q也属于扩展映射Q导致容器无法判断?/span>



崔振 2006-12-18 10:45 发表评论
]]>
saveorUpdate与unsaved-valueQsaveQupdateQdeletehttp://www.aygfsteel.com/czcode/archive/2006/12/18/88453.html崔振崔振Mon, 18 Dec 2006 02:40:00 GMThttp://www.aygfsteel.com/czcode/archive/2006/12/18/88453.htmlhttp://www.aygfsteel.com/czcode/comments/88453.htmlhttp://www.aygfsteel.com/czcode/archive/2006/12/18/88453.html#Feedback0http://www.aygfsteel.com/czcode/comments/commentRss/88453.htmlhttp://www.aygfsteel.com/czcode/services/trackbacks/88453.html 希望对刚开始学Hibernate的同学有所帮助?/strong>

一、saveorUpdate与unsaved-value
到底是savaq是update
Hibernate需要判断被操作的对象究竟是一个已l持久化q的持久对象q是临时对象?
1Q.主键Hibernate的id generator产生
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="increment" />
</id>

Project project = new Project();
project.setId(XXX);
this.projectDao.saveOrUpdate(project);

1、默认unsaved-value="null"
主键是对象类型,hebernate判断project的主键是否位nullQ来判断project是否已被持久?/b>
是的话,对project对象发送save(project)Q?
若自p|了主键则直接生成update的sqlQ发送update(project)Q即便数据库里没有那条记录?
主键是基本类型如int/long/double/
自己讄unsaved-null="0"?
所以这L(fng)话save和update操作肯定不会(x)报错?/p>

2、unsaved-value="none"Q?
׃不论主键属性ؓ(f)M|都不可能为noneQ因此HibernateL对project对象发送update(project)

3、unsaved-value="any"
׃不论主键属性ؓ(f)M|都肯定ؓ(f)anyQ因此HibernateL对project对象发送save(project)Qhibernate生成主键?/p>

Hibernate文档中写?
saveOrUpdate()完成了如下工作:(x)
如果对象已经在这个session中持久化q了Q什么都不用?
如果对象没有标识|调用save()来保存它
如果对象的标识gunsaved-value中的条g匚wQ调用save()来保存它
如果对象使用了版?version或timestamp),那么除非讄unsaved-value="undefined",版本(g)查会(x)发生在标识符(g)查之?
如果q个session中有另外一个对象具有同L(fng)标识W,抛出一个异?

2Q.主键p己来赋?/b>
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="assigned" />
</id>

Project project = new Project();
project.setId(XXX);
this.projectDao.saveOrUpdate(project);

1、默认unsaved-value="null"
q时有所不同Qhibernate?x)根据主键生一个selectQ来判断此对象是否已被持久化
已被持久化则updateQ未被持久化则save?
2、unsaved-value="none"Qupdate对象Q同?/p>

3、unsaved-value="any" Qsave对象Q?
如果自己自己讄的ID在数据库中已存在Q则报错?/p>

二、save与update操作
昑ּ的用session.save()或者session.update()操作一个对象的时候,实际上是用不到unsaved-value?
在同一SessionQsave没什么可说得
update对象? 最直接的更改一个对象的Ҏ(gu)是load()它,保持Session打开Q然后直接修改即?
Session s =?
Project p = (Project) sess.load(Project.class, id) );
p.setName(“test?;
s.flush();
不用调用s.update(p);hibernate能察觉到它的变化Q会(x)自动更新。当然显C用的话也不会(x)?/p>

Hibernate文档中写?
update()Ҏ(gu)在下列情形下使用Q?
E序在前面的session中装载了对象
对象被传递到UIQ界面)?
对该对象q行了一些修?
对象被传递回业务?
应用E序在第二个session中调用update()保存修改

三、delete操作
删除时直接自己构造一个project卛_删除
this.projectDao.delete(preojct);

以前删除我是q样写的
public void deleteProject(String id) {
Project project = (Project) this.projectDao.get(Project.class, id);
if (project != null) {
this.projectDao.delete(project);
}
卌样也是可以的
Project project = new Project();
project.setId(id);
this.projectDao.delete(project).

如果有联关p,需要把U联的子cM构造出来addq去Q同样可以删除?/p>

好了Q罗嗦的够多了?/p>

崔振 2006-12-18 10:40 发表评论
]]>
POI 搞excellhttp://www.aygfsteel.com/czcode/archive/2006/12/18/88450.html崔振崔振Mon, 18 Dec 2006 02:36:00 GMThttp://www.aygfsteel.com/czcode/archive/2006/12/18/88450.htmlhttp://www.aygfsteel.com/czcode/comments/88450.htmlhttp://www.aygfsteel.com/czcode/archive/2006/12/18/88450.html#Feedback0http://www.aygfsteel.com/czcode/comments/commentRss/88450.htmlhttp://www.aygfsteel.com/czcode/services/trackbacks/88450.html

1、遍历workbook

代码
  1. // load源文?   
  2. POIFSFileSystem fs =  new  POIFSFileSystem( new  FileInputStream(filePath));   
  3. HSSFWorkbook wb =  new  HSSFWorkbook(fs);   
  4. for  ( int  i =  0 ; i < wb.getNumberOfSheets(); i++) {   
  5.     HSSFSheet sheet = wb.getSheetAt(i);   
  6.      for  ( int  i = sheet.getFirstRowNum(); i < sheet.getLastRowNum(); i ++) {   
  7.     HSSFRow row = sheet.getRow(i);   
  8.              if  (row !=  null ) {   
  9.         。。。操作}   
  10.        }   
  11.      }   
  12. // 目标文g   
  13. FileOutputStream fos =  new  FileOutputStream(objectPath);   
  14. //写文?   
  15. swb.write(fos);   
  16. fos.close();  

2、得到列和单元格

代码
  1. HSSFRow row = sheet.getRow(i);   
  2. HSSFCell cell = row.getCell(( short ) j);  

3、设|sheet名称和单元格内容Z?/b>

代码
  1. wb.setSheetName(n,  "中文" ,HSSFCell.ENCODING_UTF_16);       
  2. cell.setEncoding(( short 1 );   
  3. cell.setCellValue( "中文" );  

4、单元格内容未公式或数|可以q样d

代码
  1. cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);   
  2. cell.getNumericCellValue()  


5、设|列宽、行?/b>

代码
  1. sheet.setColumnWidth(( short )column,( short )width);   
  2. row.setHeight(( short )height);  


6、添加区域,合ƈ单元?/b>

代码
  1. Region region =  new  Region(( short )rowFrom,( short )columnFrom,( short )rowTo,( short )columnTo);   
  2. sheet.addMergedRegion(region);   
  3. //得到所有区?   
  4. sheet.getNumMergedRegions()  

7、常用方?/b>
Ҏ(gu)单元g同属性返回字W串数?

代码
  1. public  String getCellStringValue(HSSFCell cell) {   
  2.         String cellValue =  "" ;   
  3.          switch  (cell.getCellType()) {   
  4.          case  HSSFCell.CELL_TYPE_STRING:   
  5.             cellValue = cell.getStringCellValue();   
  6.              if (cellValue.trim().equals( "" )||cellValue.trim().length()<= 0 )   
  7.                 cellValue= " " ;   
  8.              break ;   
  9.          case  HSSFCell.CELL_TYPE_NUMERIC:   
  10.             cellValue = String.valueOf(cell.getNumericCellValue());   
  11.              break ;   
  12.          case  HSSFCell.CELL_TYPE_FORMULA:   
  13.             cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);   
  14.             cellValue = String.valueOf(cell.getNumericCellValue());   
  15.              break ;   
  16.          case  HSSFCell.CELL_TYPE_BLANK:   
  17.             cellValue= " " ;   
  18.              break ;   
  19.          case  HSSFCell.CELL_TYPE_BOOLEAN:   
  20.              break ;   
  21.          case  HSSFCell.CELL_TYPE_ERROR:   
  22.              break ;   
  23.          default :   
  24.              break ;   
  25.         }   
  26.          return  cellValue;   
  27.     }  


8、常用单元格Ҏ(gu)格式

虚线HSSFCellStyle.BORDER_DOTTED
实线HSSFCellStyle.BORDER_THIN

代码
  1. public   static  HSSFCellStyle getCellStyle( short  type)   
  2.     {      
  3.        HSSFWorkbook wb =  new  HSSFWorkbook();   
  4.        HSSFCellStyle style = wb.createCellStyle();   
  5.        style.setBorderBottom(type); //下边框?   
  6.         style.setBorderLeft(type); //左边框?   
  7.         style.setBorderRight(type); //双框?   
  8.         style.setBorderTop(type); //上边框?   
  9.         return  style;   
  10.     }  


9、设|字体和内容位置

代码
  1. HSSFFont f  = wb.createFont();   
  2. f.setFontHeightInPoints(( short 11 ); //字号   
  3. f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); //加粗   
  4. style.setFont(f);   
  5. style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //左右居中   
  6. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //上下居中   
  7. style.setRotation( short  rotation); //单元格内容的旋{的角?   
  8. HSSFDataFormat df = wb.createDataFormat();   
  9. style1.setDataFormat(df.getFormat( "0.00%" )); //讄单元格数据格?   
  10. cell.setCellFormula(string); //l单元格讑օ?   
  11. style.setRotation( short  rotation); //单元格内容的旋{的角?   
  12. cell.setCellStyle(style);   


10、插入图?/b>
论坛里看到的
代码
  1. //先把读进来的囄攑ֈ一个ByteArrayOutputStream中,以便产生ByteArray   
  2.       ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();   
  3.       BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));   
  4.       ImageIO.write(bufferImg,"jpg",byteArrayOut);   
  5. //读进一个excel模版   
  6. FileInputStream fos = new FileInputStream(filePathName+"/stencil.xlt");    
  7. fs = new POIFSFileSystem(fos);   
  8. //创徏一个工作薄   
  9. HSSFWorkbook wb = new HSSFWorkbook(fs);   
  10. HSSFSheet sheet = wb.getSheetAt(0);   
  11. HSSFPatriarch patriarch = sheet.createDrawingPatriarch();   
  12. HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1023,255,(short0,0,(short)10,10);        
  13. patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));  

最后更斎ͼ(x)2006-10-30 20:12


崔振 2006-12-18 10:36 发表评论
]]>
利用JavaQPOI dExcel文档&向Excel中插入图?/title><link>http://www.aygfsteel.com/czcode/archive/2006/12/18/88448.html</link><dc:creator>崔振</dc:creator><author>崔振</author><pubDate>Mon, 18 Dec 2006 02:35:00 GMT</pubDate><guid>http://www.aygfsteel.com/czcode/archive/2006/12/18/88448.html</guid><wfw:comment>http://www.aygfsteel.com/czcode/comments/88448.html</wfw:comment><comments>http://www.aygfsteel.com/czcode/archive/2006/12/18/88448.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.aygfsteel.com/czcode/comments/commentRss/88448.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/czcode/services/trackbacks/88448.html</trackback:ping><description><![CDATA[ <p> <font face="宋体" size="2">一QPOI?/font> </p> <p> <font face="宋体" size="2">Jakarta POI 是apache的子目Q目标是处理ole2对象。它提供了一l操UWindows文档的Java API</font> </p> <p> <font face="宋体" size="2">目前比较成熟的是HSSF接口Q处理MS ExcelQ?7-2002Q对象。它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西,而是真正的Excel对象Q你可以控制一些属性如sheet,cell{等?/font> </p> <p> <font face="宋体" size="2">二.HSSF概况</font> </p> <p> <font face="宋体" size="2">HSSF 是Horrible SpreadSheet Format的羃写,也即“讨厌的?sh)子表格格式”。也许HSSF的名字有Ҏ(gu)E,本质而言它是一个非怸肃、正规的API。通过HSSFQ你可以用纯Java代码来读取、写入、修改Excel文g?/font> </p> <p> <font face="宋体" size="2">HSSF 取操作提供了两类APIQusermodel和eventusermodelQ即“用h型”和“事?用户模型”。前者很好理解,后者比较抽象,但操作效率要高得多?/font> </p> <p> <font face="宋体" size="2">三.开始编?/font> </p> <p> <font face="宋体" size="2">1 Q?准备工作</font> </p> <p> <font face="宋体" size="2">要求:JDK 1.4+POI开发包</font> </p> <p> <font face="宋体" size="2">可以?/font> <a > <font face="宋体" size="2">http://www.apache.org/dyn/closer.cgi/jakarta/poi/</font> </a> <font face="宋体" size="2">最新的POI工具?/font> </p> <p> <font face="宋体" size="2">2 Q?EXCEL l构</font> </p> <p> <font face="宋体" size="2">HSSFWorkbook excell 文档对象介绍<br />HSSFSheet excell的表?br />HSSFRow excell的行<br />HSSFCell excell的格子单?br />HSSFFont excell字体<br />HSSFName 名称<br />HSSFDataFormat 日期格式<br />在poi1.7中才有以?:(x)<br />HSSFHeader sheet?br />HSSFFooter sheet?br />和这个样?br />HSSFCellStyle cell样式<br />辅助操作包括<br />HSSFDateUtil 日期<br />HSSFPrintSetup 打印<br />HSSFErrorConstants 错误信息?/font> </p> <p> <font face="宋体" size="2">3 Q具体用法实?Q采?usermodel Q?/font> </p> <p> <font face="宋体" size="2">如何读Excel</font> </p> <p> <font face="宋体" size="2">dExcel文gӞ首先生成一个POIFSFileSystem对象Q由POIFSFileSystem对象构造一个HSSFWorkbookQ该HSSFWorkbook对象׃表了Excel文档。下面代码读取上面生成的Excel文g写入的消息字Ԍ(x)<br />try{<br />   POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream("d:/workbook.xls"));<br />   HSSFWorkbook wb = new HSSFWorkbook(fs);<br />   HSSFSheet sheet = wb.getSheetAt(0);<br />   HSSFRow row = sheet.getRow(0);<br />   HSSFCell cell = row.getCell((short) 0);<br />   String msg = cell.getStringCellValue();<br />   System.out.println(msg);<br />  }catch(Exception e){<br />   e.printStackTrace();<br />  }</font> </p> <p> <font face="宋体" size="2">如何写excelQ?/font> </p> <p> <font face="宋体" size="2">excel的第一个表单第一行的W一个单元格的值写成“a test”?/font> </p> <p> <font face="宋体" size="2">POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream("workbook.xls"));</font> </p> <p> <font face="宋体" size="2">    HSSFWorkbook wb = new HSSFWorkbook(fs);</font> </p> <p> <font face="宋体" size="2">    HSSFSheet sheet = wb.getSheetAt(0);</font> </p> <p> <font face="宋体" size="2">    HSSFRow row = sheet.getRow(0);</font> </p> <p> <font face="宋体" size="2">    HSSFCell cell = row.getCell((short)0);</font> </p> <p> <font face="宋体" size="2">    cell.setCellValue("a test");</font> </p> <p> <font face="宋体" size="2">    // Write the output to a file</font> </p> <p> <font face="宋体" size="2">    FileOutputStream fileOut = new FileOutputStream("workbook.xls");</font> </p> <p> <font face="宋体" size="2">    wb.write(fileOut);</font> </p> <p> <font face="宋体" size="2">fileOut.close();</font> </p> <p> <font face="宋体" size="2">4 Q?可参考文?/font> </p> <p> <font face="宋体" size="2">POI 主页Q?/font> <a > <font face="宋体" size="2">http://jakarta.apache.org/poi/</font> </a> <font face="宋体" size="2">Q?/font> </p> <p> <font face="宋体" size="2">初学者如何快速上手用POI HSSF</font> </p> <p> <a > <font face="宋体" size="2">http://jakarta.apache.org/poi/hssf/quick-guide.html</font> </a> <font face="宋体" size="2">?/font> </p> <p> <font face="宋体" size="2">里面有很多例子代码,可以很方便上手?/font> </p> <p> <font face="宋体" size="2">四.使用心得</font> </p> <p> <font face="宋体" size="2">POI HSSF 的usermodel包把Excel文g映射成我们熟(zhn)的l构Q诸如Workbook、Sheet、Row、Cell{,它把整个l构以一l对象的形式保存在内存之中,便于理解Q操作方便,基本上能够满x们的要求Q所以说q个一个不错的选择?/font> </p> <p> <font color="#000066"> <strong>Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q-Q?br /></strong>前面已经讲过利用POIdExcelQ下面是一个用POI向Excel中插入图片的例子?br /><br /></font> <font color="#000066"> <span id="wmqeeuq" class="postbody" twffan="done">官方文档Q?br />Images are part of the drawing support. To add an image just call createPicture() on the drawing patriarch. At the time of writing the following types are supported:<br />PNG<br />JPG<br />DIB<br />It is not currently possible to read existing images and it should be noted that any existing drawings may be erased once you add a image to a sheet.<br /><br />// Create the drawing patriarch. This is the top level container for<br />// all shapes. This will clear out any existing shapes for that sheet.</span> <br /> <br /> </font> <font color="#000066"> <span id="wmqeeuq" class="postbody" twffan="done">通过HSSFPatriarchccreatePictureҎ(gu)的在指定的wb中的sheet创徏囄,它接受二个参敎ͼW一个是HSSFClientAnchorQ设定图片的大小?br /><br /></span> </font> </p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <span style="COLOR: #008080"> 1</span> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <span style="COLOR: #0000ff">package</span> <span style="COLOR: #000000"> com.poi.hssf.test;<br /></span> <span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <br /> </span> <span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> java.io.FileOutputStream;<br /></span> <span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> java.io.File;<br /></span> <span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> java.io.ByteArrayOutputStream;<br /></span> <span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> java.io.IOException;<br /></span> <span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <br /> </span> <span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> java.awt.image.BufferedImage;<br /></span> <span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> javax.imageio.</span> <span style="COLOR: #000000">*</span> <span style="COLOR: #000000">;<br /></span> <span style="COLOR: #008080">10</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <br /> </span> <span style="COLOR: #008080">11</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> org.apache.poi.hssf.usermodel.HSSFWorkbook;<br /></span> <span style="COLOR: #008080">12</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> org.apache.poi.hssf.usermodel.HSSFSheet;<br /></span> <span style="COLOR: #008080">13</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> org.apache.poi.hssf.usermodel.HSSFPatriarch;<br /></span> <span style="COLOR: #008080">14</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> </span> <span style="COLOR: #0000ff">import</span> <span style="COLOR: #000000"> org.apache.poi.hssf.usermodel.HSSFClientAnchor;;<br /></span> <span style="COLOR: #008080">15</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align="top" /> <br /> </span> <span style="COLOR: #008080">16</span> <span style="COLOR: #000000"> <img id="Codehighlighter1_440_2605_Open_Image" onclick="this.style.display='none'; Codehighlighter1_440_2605_Open_Text.style.display='none'; Codehighlighter1_440_2605_Closed_Image.style.display='inline'; Codehighlighter1_440_2605_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /> <img id="Codehighlighter1_440_2605_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_440_2605_Closed_Text.style.display='none'; Codehighlighter1_440_2605_Open_Image.style.display='inline'; Codehighlighter1_440_2605_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /> </span> <span style="COLOR: #0000ff">public</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">class</span> <span style="COLOR: #000000"> TestPOI </span> <span id="Codehighlighter1_440_2605_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_440_2605_Open_Text"> <span style="COLOR: #000000">{<br /></span> <span style="COLOR: #008080">17</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /> <br /> </span> <span style="COLOR: #008080">18</span> <span style="COLOR: #000000"> <img id="Codehighlighter1_486_2603_Open_Image" onclick="this.style.display='none'; Codehighlighter1_486_2603_Open_Text.style.display='none'; Codehighlighter1_486_2603_Closed_Image.style.display='inline'; Codehighlighter1_486_2603_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /> <img id="Codehighlighter1_486_2603_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_486_2603_Closed_Text.style.display='none'; Codehighlighter1_486_2603_Open_Image.style.display='inline'; Codehighlighter1_486_2603_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span> <span style="COLOR: #0000ff">public</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">static</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">void</span> <span style="COLOR: #000000"> main(String[] args) </span> <span id="Codehighlighter1_486_2603_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_486_2603_Open_Text"> <span style="COLOR: #000000">{<br /></span> <span style="COLOR: #008080">19</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />            FileOutputStream fileOut </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">null</span> <span style="COLOR: #000000">;<br /></span> <span style="COLOR: #008080">20</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />            BufferedImage bufferImg </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #0000ff">null</span> <span style="COLOR: #000000">;<br /></span> <span style="COLOR: #008080">21</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />            BufferedImage bufferImg1 </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">null</span> <span style="COLOR: #000000">;<br /></span> <span style="COLOR: #008080">22</span> <span style="COLOR: #000000"> <img id="Codehighlighter1_636_2010_Open_Image" onclick="this.style.display='none'; Codehighlighter1_636_2010_Open_Text.style.display='none'; Codehighlighter1_636_2010_Closed_Image.style.display='inline'; Codehighlighter1_636_2010_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /> <img id="Codehighlighter1_636_2010_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_636_2010_Closed_Text.style.display='none'; Codehighlighter1_636_2010_Open_Image.style.display='inline'; Codehighlighter1_636_2010_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />            </span> <span style="COLOR: #0000ff">try</span> <span id="Codehighlighter1_636_2010_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_636_2010_Open_Text"> <span style="COLOR: #000000">{<br /></span> <span style="COLOR: #008080">23</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />                <br /></span> <span style="COLOR: #008080">24</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />          </span> <span style="COLOR: #008000">//</span> <span style="COLOR: #008000">先把读进来的囄攑ֈ一个ByteArrayOutputStream中,以便产生ByteArray</span> <span style="COLOR: #008000"> <br /> </span> <span style="COLOR: #008080">25</span> <span style="COLOR: #008000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /> </span> <span style="COLOR: #000000">          ByteArrayOutputStream byteArrayOut </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> ByteArrayOutputStream();<br /></span> <span style="COLOR: #008080">26</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />          ByteArrayOutputStream byteArrayOut1 </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> ByteArrayOutputStream();<br /></span> <span style="COLOR: #008080">27</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />          bufferImg </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> ImageIO.read(</span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> File(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">d:/PieChart.jpg</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">));<br /></span> <span style="COLOR: #008080">28</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />          bufferImg1 </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> ImageIO.read(</span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> File(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">d:/fruitBarChart.jpg</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">));<br /></span> <span style="COLOR: #008080">29</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />          ImageIO.write(bufferImg,</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">jpg</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">,byteArrayOut);<br /></span> <span style="COLOR: #008080">30</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />          ImageIO.write(bufferImg1,</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">jpg</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">,byteArrayOut1);<br /></span> <span style="COLOR: #008080">31</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />          <br /></span> <span style="COLOR: #008080">32</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span> <span style="COLOR: #008000">//</span> <span style="COLOR: #008000">创徏一个工作薄</span> <span style="COLOR: #008000"> <br /> </span> <span style="COLOR: #008080">33</span> <span style="COLOR: #008000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /> </span> <span style="COLOR: #000000">        HSSFWorkbook wb </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> HSSFWorkbook();<br /></span> <span style="COLOR: #008080">34</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        HSSFSheet sheet1 </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> wb.createSheet(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">new sheet</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">);<br /></span> <span style="COLOR: #008080">35</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span> <span style="COLOR: #008000">//</span> <span style="COLOR: #008000">HSSFRow row = sheet1.createRow(2);</span> <span style="COLOR: #008000"> <br /> </span> <span style="COLOR: #008080">36</span> <span style="COLOR: #008000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /> </span> <span style="COLOR: #000000">        HSSFPatriarch patriarch </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> sheet1.createDrawingPatriarch();<br /></span> <span style="COLOR: #008080">37</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        HSSFClientAnchor anchor </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> HSSFClientAnchor(</span> <span style="COLOR: #000000">0</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">0</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">512</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">255</span> <span style="COLOR: #000000">,(</span> <span style="COLOR: #0000ff">short</span> <span style="COLOR: #000000">) </span> <span style="COLOR: #000000">1</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">1</span> <span style="COLOR: #000000">,(</span> <span style="COLOR: #0000ff">short</span> <span style="COLOR: #000000">)</span> <span style="COLOR: #000000">10</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">20</span> <span style="COLOR: #000000">);<br /></span> <span style="COLOR: #008080">38</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        HSSFClientAnchor anchor1 </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> HSSFClientAnchor(</span> <span style="COLOR: #000000">0</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">0</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">512</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">255</span> <span style="COLOR: #000000">,(</span> <span style="COLOR: #0000ff">short</span> <span style="COLOR: #000000">) </span> <span style="COLOR: #000000">2</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">30</span> <span style="COLOR: #000000">,(</span> <span style="COLOR: #0000ff">short</span> <span style="COLOR: #000000">)</span> <span style="COLOR: #000000">10</span> <span style="COLOR: #000000">,</span> <span style="COLOR: #000000">60</span> <span style="COLOR: #000000">);<br /></span> <span style="COLOR: #008080">39</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        anchor1.setAnchorType(</span> <span style="COLOR: #000000">2</span> <span style="COLOR: #000000">);<br /></span> <span style="COLOR: #008080">40</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span> <span style="COLOR: #008000">//</span> <span style="COLOR: #008000">插入囄</span> <span style="COLOR: #008000"> <br /> </span> <span style="COLOR: #008080">41</span> <span style="COLOR: #008000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /> </span> <span style="COLOR: #000000">        patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));<br /></span> <span style="COLOR: #008080">42</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));<br /></span> <span style="COLOR: #008080">43</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        <br /></span> <span style="COLOR: #008080">44</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />            fileOut </span> <span style="COLOR: #000000">=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">new</span> <span style="COLOR: #000000"> FileOutputStream(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">d:/workbook.xls</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">);<br /></span> <span style="COLOR: #008080">45</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />            </span> <span style="COLOR: #008000">//</span> <span style="COLOR: #008000">写入excel文g</span> <span style="COLOR: #008000"> <br /> </span> <span style="COLOR: #008080">46</span> <span style="COLOR: #008000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /> </span> <span style="COLOR: #000000">            wb.write(fileOut);<br /></span> <span style="COLOR: #008080">47</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />            fileOut.close();<br /></span> <span style="COLOR: #008080">48</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />        <br /></span> <span style="COLOR: #008080">49</span> <span style="COLOR: #000000"> <img id="Codehighlighter1_2032_2161_Open_Image" onclick="this.style.display='none'; Codehighlighter1_2032_2161_Open_Text.style.display='none'; Codehighlighter1_2032_2161_Closed_Image.style.display='inline'; Codehighlighter1_2032_2161_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /> <img id="Codehighlighter1_2032_2161_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2032_2161_Closed_Text.style.display='none'; Codehighlighter1_2032_2161_Open_Image.style.display='inline'; Codehighlighter1_2032_2161_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />            }</span> </span> <span style="COLOR: #0000ff">catch</span> <span style="COLOR: #000000">(IOException io)</span> <span id="Codehighlighter1_2032_2161_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_2032_2161_Open_Text"> <span style="COLOR: #000000">{<br /></span> <span style="COLOR: #008080">50</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />                    io.printStackTrace();<br /></span> <span style="COLOR: #008080">51</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />                    System.out.println(</span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">io erorr :  </span> <span style="COLOR: #000000">"</span> <span style="COLOR: #000000">+</span> <span style="COLOR: #000000"> io.getMessage());<br /></span> <span style="COLOR: #008080">52</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />            }</span> </span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">finally</span> <span style="COLOR: #000000"> <br /> </span> <span style="COLOR: #008080">53</span> <span style="COLOR: #000000"> <img id="Codehighlighter1_2183_2597_Open_Image" onclick="this.style.display='none'; Codehighlighter1_2183_2597_Open_Text.style.display='none'; Codehighlighter1_2183_2597_Closed_Image.style.display='inline'; Codehighlighter1_2183_2597_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /> <img id="Codehighlighter1_2183_2597_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2183_2597_Closed_Text.style.display='none'; Codehighlighter1_2183_2597_Open_Image.style.display='inline'; Codehighlighter1_2183_2597_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />            </span> <span id="Codehighlighter1_2183_2597_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_2183_2597_Open_Text"> <span style="COLOR: #000000">{<br /></span> <span style="COLOR: #008080">54</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />                </span> <span style="COLOR: #0000ff">if</span> <span style="COLOR: #000000"> (fileOut </span> <span style="COLOR: #000000">!=</span> <span style="COLOR: #000000"> </span> <span style="COLOR: #0000ff">null</span> <span style="COLOR: #000000">)<br /></span> <span style="COLOR: #008080">55</span> <span style="COLOR: #000000"> <img id="Codehighlighter1_2238_2583_Open_Image" onclick="this.style.display='none'; Codehighlighter1_2238_2583_Open_Text.style.display='none'; Codehighlighter1_2238_2583_Closed_Image.style.display='inline'; Codehighlighter1_2238_2583_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /> <img id="Codehighlighter1_2238_2583_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2238_2583_Closed_Text.style.display='none'; Codehighlighter1_2238_2583_Open_Image.style.display='inline'; Codehighlighter1_2238_2583_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />                </span> <span id="Codehighlighter1_2238_2583_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_2238_2583_Open_Text"> <span style="COLOR: #000000">{<br /></span> <span style="COLOR: #008080">56</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />                           <br /></span> <span style="COLOR: #008080">57</span> <span style="COLOR: #000000"> <img id="Codehighlighter1_2292_2366_Open_Image" onclick="this.style.display='none'; Codehighlighter1_2292_2366_Open_Text.style.display='none'; Codehighlighter1_2292_2366_Closed_Image.style.display='inline'; Codehighlighter1_2292_2366_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /> <img id="Codehighlighter1_2292_2366_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2292_2366_Closed_Text.style.display='none'; Codehighlighter1_2292_2366_Open_Image.style.display='inline'; Codehighlighter1_2292_2366_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />                    </span> <span style="COLOR: #0000ff">try</span> <span style="COLOR: #000000"> </span> <span id="Codehighlighter1_2292_2366_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_2292_2366_Open_Text"> <span style="COLOR: #000000">{<br /></span> <span style="COLOR: #008080">58</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />                              fileOut.close();<br /></span> <span style="COLOR: #008080">59</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />                         }</span> </span> <span style="COLOR: #000000"> <br /> </span> <span style="COLOR: #008080">60</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />                    </span> <span style="COLOR: #0000ff">catch</span> <span style="COLOR: #000000"> (IOException e)<br /></span> <span style="COLOR: #008080">61</span> <span style="COLOR: #000000"> <img id="Codehighlighter1_2430_2565_Open_Image" onclick="this.style.display='none'; Codehighlighter1_2430_2565_Open_Text.style.display='none'; Codehighlighter1_2430_2565_Closed_Image.style.display='inline'; Codehighlighter1_2430_2565_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /> <img id="Codehighlighter1_2430_2565_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_2430_2565_Closed_Text.style.display='none'; Codehighlighter1_2430_2565_Open_Image.style.display='inline'; Codehighlighter1_2430_2565_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />                    </span> <span id="Codehighlighter1_2430_2565_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"> <img src="http://www.aygfsteel.com/images/dot.gif" /> </span> <span id="Codehighlighter1_2430_2565_Open_Text"> <span style="COLOR: #000000">{<br /></span> <span style="COLOR: #008080">62</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" />                            </span> <span style="COLOR: #008000">//</span> <span style="COLOR: #008000"> TODO Auto-generated catch block</span> <span style="COLOR: #008000"> <br /> </span> <span style="COLOR: #008080">63</span> <span style="COLOR: #008000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align="top" /> </span> <span style="COLOR: #000000">                            e.printStackTrace();<br /></span> <span style="COLOR: #008080">64</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />                     }</span> </span> <span style="COLOR: #000000"> <br /> </span> <span style="COLOR: #008080">65</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />                }</span> </span> <span style="COLOR: #000000"> <br /> </span> <span style="COLOR: #008080">66</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />            }</span> </span> <span style="COLOR: #000000"> <br /> </span> <span style="COLOR: #008080">67</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span> </span> <span style="COLOR: #000000"> <br /> </span> <span style="COLOR: #008080">68</span> <span style="COLOR: #000000"> <img src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span> </span> </div> <img src ="http://www.aygfsteel.com/czcode/aggbug/88448.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/czcode/" target="_blank">崔振</a> 2006-12-18 10:35 <a href="http://www.aygfsteel.com/czcode/archive/2006/12/18/88448.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用Java动态代理实现AOP http://www.aygfsteel.com/czcode/archive/2006/12/13/87433.html崔振崔振Wed, 13 Dec 2006 03:43:00 GMThttp://www.aygfsteel.com/czcode/archive/2006/12/13/87433.htmlhttp://www.aygfsteel.com/czcode/comments/87433.htmlhttp://www.aygfsteel.com/czcode/archive/2006/12/13/87433.html#Feedback0http://www.aygfsteel.com/czcode/comments/commentRss/87433.htmlhttp://www.aygfsteel.com/czcode/services/trackbacks/87433.html目前整个开发社区对AOP(Aspect Oriented Programing)推崇备至Q也涌现出大量支持AOP的优UFramework,--Spring, JAC, Jboss AOP {等。AOPg一时之间成了潮。Java初学者不要发出感慨QOOPq没有学通呢Q又来AOP。本文不是要在理Z具体阐述何ؓ(f)AOP, Z要进行AOP . 要详l了解学?fn)AOP可以到它老家http://aosd.netȝ瞧。这里只是意N过一个简单的例子向初学者展CZ下如何来q行AOP.

  Z单v见,例子没有没有使用MW三方的AOP Framework, 而是利用Java语言本n自带的动态代理功能来实现AOP.

  让我们先回到AOP本nQAOP主要应用于日志记录,性能l计Q安全控?事务处理{方面。它的主要意囑ְ要将日志记录Q性能l计Q安全控制等{代码从商业逻辑代码中清楚的划分出来Q我们可以把q些行ؓ(f)一个一个单独看作系l所要解决的问题Q就是所谓的面向问题的编E?不知AOP译作面向问题的编E是否欠?。通过对这些行为的分离Q我们希望可以将它们独立地配|到商业Ҏ(gu)中,而要改变q些行ؓ(f)也不需要媄响到商业Ҏ(gu)代码?

  假设pȝ׃pd的BusinessObject所完成业务逻辑功能Q系l要求在每一ơ业务逻辑处理时要做日志记录。这里我们略d体的业务逻辑代码?

public interface BusinessInterface {
 public void processBusiness();
}

public class BusinessObject implements BusinessInterface {
 private Logger logger = Logger.getLogger(this.getClass().getName());
 public void processBusiness(){
  try {
   logger.info("start to processing...");
   //business logic here.
   System.out.println(“here is business logic?;
   logger.info("end processing...");
  } catch (Exception e){
   logger.info("exception happends...");
   //exception handling
  }
 }
}
  q里处理商业逻辑的代码和日志记录代码混合在一Pq给日后的维护带来一定的困难Qƈ且也?x)造成大量的代码重复。完全相同的log代码出现在pȝ的每一个BusinessObject中?

  按照AOP的思想Q我们应该把日志记录代码分离出来。要这些代码分d涉及(qing)C个问题,我们必须知道商业逻辑代码何时被调用,q样我们好插入日志记录代码。一般来说要截获一个方法,我们可以采用回调Ҏ(gu)或者动态代理。动态代理一般要更加灉|一些,目前多数的AOP Framework也大都采用了动态代理来实现。这里我们也采用动态代理作Z子?

  JDK1.2以后提供了动态代理的支持Q程序员通过实现java.lang.reflect.InvocationHandler接口提供一个执行处理器Q然后通过java.lang.reflect.Proxy得到一个代理对象,通过q个代理对象来执行商业方?在商业方法被调用的同Ӟ执行处理器会(x)被自动调用?

  有了JDK的这U支持,我们所要做的仅仅是提供一个日志处理器?

public class LogHandler implements InvocationHandler {

 private Logger logger Q?Logger.getLogger(this.getClass().getName());
  private Object delegate;
  public LogHandler(Object delegate){
   this.delegate = delegate;
  }

 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  Object o = null;
  try {
   logger.info("method stats..." Q?method);
   o = method.invoke(delegate,args);
   logger.info("method ends..." + method);
  } catch (Exception e){
   logger.info("Exception happends...");
   //excetpion handling.
  }
  return o;
 }
}
  现在我们可以把BusinessObject里面的所有日志处理代码全部去掉了?

public class BusinessObject implements BusinessInterface {

 private Logger logger = Logger.getLogger(this.getClass().getName());
 public void processBusiness(){
  //business processing
  System.out.println(“here is business logic?;
 }
}
  客户端调用商业方法的代码如下Q?br />
BusinessInterface businessImp = new BusinessObject();

InvocationHandler handler = new LogHandler(businessImp);

BusinessInterface proxy = (BusinessInterface) Proxy.newProxyInstance(
 businessImp.getClass().getClassLoader(),
 businessImp.getClass().getInterfaces(),
 handler);

proxy.processBusiness();
  E序输出如下Q?

INFO: method stats...
here is business logic
INFO: method ends...
  x我们的第一ơ小试是完成了。可以看刎ͼ采用AOP之后Q日志记录和业务逻辑代码完全分开了,以后要改变日志记录的话只需要修Ҏ(gu)志记录处理器p了,而业务对象本w(BusinessObjectQ无需做Q何修攏Vƈ且这个日志记录不?x)造成重复代码了,所有的商业处理对象都可以重用这个日志处理器?

  当然在实际应用中Q这个例子就昑־太粗p了。由于JDK的动态代理ƈ没有直接支持一ơ注册多个InvocationHandlerQ那么我们对业务处理Ҏ(gu)既要日志记录又要性能l计Ӟ需要自己做一些变通了。一般我们可以自己定义一个Handler接口Q然后维护一个队列存放所有Handler, 当InvocationHandler被触发的时候我们依ơ调用自qHandler。所q的是目前几乎所有的AOP Framework都对q方面提供了很好的支?q里推荐大家使用Spring?br />



崔振 2006-12-13 11:43 发表评论
]]>
用JS的g?/title><link>http://www.aygfsteel.com/czcode/archive/2006/12/06/85839.html</link><dc:creator>崔振</dc:creator><author>崔振</author><pubDate>Wed, 06 Dec 2006 06:22:00 GMT</pubDate><guid>http://www.aygfsteel.com/czcode/archive/2006/12/06/85839.html</guid><wfw:comment>http://www.aygfsteel.com/czcode/comments/85839.html</wfw:comment><comments>http://www.aygfsteel.com/czcode/archive/2006/12/06/85839.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/czcode/comments/commentRss/85839.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/czcode/services/trackbacks/85839.html</trackback:ping><description><![CDATA[ <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <span style="COLOR: #008080"> 1</span> <span style="COLOR: #000000"><</span><span style="COLOR: #000000">script</span><span style="COLOR: #000000">></span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">var</span><span style="COLOR: #000000"> start</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">var</span><span style="COLOR: #000000"> n;<br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> init(){<br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">  start</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">  }<br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000"> process(i){<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">  n</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">setTimeout(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">init()</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">1000</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(start</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">i){<br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">   clearTimeout(n);<br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">    start</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">q里写你需要执行的代码</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #008000"></span><span style="COLOR: #000000">  }<br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">}<br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000"></span><span style="COLOR: #000000"></</span><span style="COLOR: #000000">script</span><span style="COLOR: #000000">></span></div>其中process(i) iZ要设|gq的旉 Uؓ(f)单位 <br />关于timeout:<br />它其实就是一个@环而已Q在循环里调用它的第一个参敎ͼW?个参数是Thread.sleep的时间间隔,当然如果你不在合适的时候clearTimeout的话Q它?yu)成Z个死循环 <img src ="http://www.aygfsteel.com/czcode/aggbug/85839.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/czcode/" target="_blank">崔振</a> 2006-12-06 14:22 <a href="http://www.aygfsteel.com/czcode/archive/2006/12/06/85839.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <a href="http://www.aygfsteel.com/" title="狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频">狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频</a> </div> </footer> վ֩ģ壺 <a href="http://" target="_blank">ʡ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ϻ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ĵʡ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">»</a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Դ</a>| <a href="http://" target="_blank">Ϫ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">̩˳</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɳ</a>| <a href="http://" target="_blank">ڳ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">鴨</a>| <a href="http://" target="_blank">IJ</a>| <a href="http://" target="_blank">ӥ̶</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ǿ</a>| <a href="http://" target="_blank">¡</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ʯ</a>| <a href="http://" target="_blank">̨</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>