??xml version="1.0" encoding="utf-8" standalone="yes"?>青梅竹马是消防员在线,亚洲欧美国产一区二区三区,中文字幕在线官网http://www.aygfsteel.com/ec2008/category/26384.html·古之学者必有师·做学者亦要做师者·FIGHTING·zh-cnFri, 25 Apr 2008 14:34:42 GMTFri, 25 Apr 2008 14:34:42 GMT60q接各种数据库方式速查?/title><link>http://www.aygfsteel.com/ec2008/archive/2008/04/25/196000.html</link><dc:creator>月芽?/dc:creator><author>月芽?/author><pubDate>Fri, 25 Apr 2008 08:07:00 GMT</pubDate><guid>http://www.aygfsteel.com/ec2008/archive/2008/04/25/196000.html</guid><wfw:comment>http://www.aygfsteel.com/ec2008/comments/196000.html</wfw:comment><comments>http://www.aygfsteel.com/ec2008/archive/2008/04/25/196000.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/ec2008/comments/commentRss/196000.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/ec2008/services/trackbacks/196000.html</trackback:ping><description><![CDATA[q接各种数据库方式速查?br /> <br /> 下面|列了各U数据库使用JDBCq接的方式,可以作ؓ一个手册用?<br /> <br /> 1、Oracle8/8i/9i数据库(thin模式Q?<br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;">Class.forName(</span><span style="color: #000000;">"</span><span style="color: #000000;">oracle.jdbc.driver.OracleDriver</span><span style="color: #000000;">"</span><span style="color: #000000;">).newInstance(); <br /> String url</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">jdbc:oracle:thin:@localhost:1521:orcl</span><span style="color: #000000;">"</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">orcl为数据库的SID </span><span style="color: #008000;"><br /> </span><span style="color: #000000;">String user</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">test</span><span style="color: #000000;">"</span><span style="color: #000000;">; <br /> String password</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">test</span><span style="color: #000000;">"</span><span style="color: #000000;">; <br /> Connection conn</span><span style="color: #000000;">=</span><span style="color: #000000;"> DriverManager.getConnection(url,user,password);  <br /> </span></div> 2、DB2数据?<br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;">Class.forName(</span><span style="color: #000000;">"</span><span style="color: #000000;">com.ibm.db2.jdbc.app.DB2Driver </span><span style="color: #000000;">"</span><span style="color: #000000;">).newInstance(); <br /> String url</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">jdbc:db2://localhost:5000/sample</span><span style="color: #000000;">"</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">sampleZ的数据库?nbsp;</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">String user</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">admin</span><span style="color: #000000;">"</span><span style="color: #000000;">; <br /> String password</span><span style="color: #000000;">=</span><span style="color: #000000;">""</span><span style="color: #000000;">; <br /> Connection conn</span><span style="color: #000000;">=</span><span style="color: #000000;"> DriverManager.getConnection(url,user,password); <br /> </span></div> 3、Sql Server7.0/2000数据?<br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;"><br /> Class.forName(</span><span style="color: #000000;">"</span><span style="color: #000000;">com.microsoft.jdbc.sqlserver.SQLServerDriver</span><span style="color: #000000;">"</span><span style="color: #000000;">).newInstance(); <br /> String url</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb</span><span style="color: #000000;">"</span><span style="color: #000000;">; <br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">mydb为数据库 </span><span style="color: #008000;"><br /> </span><span style="color: #000000;">String user</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">sa</span><span style="color: #000000;">"</span><span style="color: #000000;">; <br /> String password</span><span style="color: #000000;">=</span><span style="color: #000000;">""</span><span style="color: #000000;">; <br /> Connection conn</span><span style="color: #000000;">=</span><span style="color: #000000;"> DriverManager.getConnection(url,user,password);  <br /> </span></div> 4、Sybase数据?<br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;">Class.forName(</span><span style="color: #000000;">"</span><span style="color: #000000;">com.sybase.jdbc.SybDriver</span><span style="color: #000000;">"</span><span style="color: #000000;">).newInstance(); <br /> String url </span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;"> jdbc:sybase:Tds:localhost:5007/myDB</span><span style="color: #000000;">"</span><span style="color: #000000;">;</span><span style="color: #008000;">//</span><span style="color: #008000;">myDBZ的数据库?nbsp;</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">Properties sysProps </span><span style="color: #000000;">=</span><span style="color: #000000;"> System.getProperties(); <br /> SysProps.put(</span><span style="color: #000000;">"</span><span style="color: #000000;">user</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">userid</span><span style="color: #000000;">"</span><span style="color: #000000;">); <br /> SysProps.put(</span><span style="color: #000000;">"</span><span style="color: #000000;">password</span><span style="color: #000000;">"</span><span style="color: #000000;">,</span><span style="color: #000000;">"</span><span style="color: #000000;">user_password</span><span style="color: #000000;">"</span><span style="color: #000000;">); <br /> Connection conn</span><span style="color: #000000;">=</span><span style="color: #000000;"> DriverManager.getConnection(url, SysProps);  <br /> </span></div> 5、Informix数据?<br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;">Class.forName(</span><span style="color: #000000;">"</span><span style="color: #000000;">com.informix.jdbc.IfxDriver</span><span style="color: #000000;">"</span><span style="color: #000000;">).newInstance(); <br /> String url </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver; </span><span style="color: #000000;"><br /> </span><span style="color: #000000;">user</span><span style="color: #000000;">=</span><span style="color: #000000;">testuser;password</span><span style="color: #000000;">=</span><span style="color: #000000;">testpassword</span><span style="color: #000000;">"</span><span style="color: #000000;">; //myDB为数据库?nbsp;</span><span style="color: #000000;"><br /> </span><span style="color: #000000;">Connection conn</span><span style="color: #000000;">=</span><span style="color: #000000;"> DriverManager.getConnection(url);  <br /> </span></div> 6、MySQL数据?<br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;">Class.forName(</span><span style="color: #000000;">"</span><span style="color: #000000;">org.gjt.mm.mysql.Driver</span><span style="color: #000000;">"</span><span style="color: #000000;">).newInstance(); <br /> String url </span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1</span><span style="color: #000000;">"</span><span style="color: #000000;"> <br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">myDB为数据库?nbsp;</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">Connection conn</span><span style="color: #000000;">=</span><span style="color: #000000;"> DriverManager.getConnection(url);  <br /> </span></div> 7、PostgreSQL数据?<br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;">Class.forName(</span><span style="color: #000000;">"</span><span style="color: #000000;">org.postgresql.Driver</span><span style="color: #000000;">"</span><span style="color: #000000;">).newInstance(); <br /> String url </span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">jdbc:postgresql://localhost/myDB</span><span style="color: #000000;">"</span><span style="color: #000000;"> </span><span style="color: #008000;">//</span><span style="color: #008000;">myDB为数据库?nbsp;</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">String user</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">myuser</span><span style="color: #000000;">"</span><span style="color: #000000;">; <br /> String password</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">mypassword</span><span style="color: #000000;">"</span><span style="color: #000000;">; <br /> Connection conn</span><span style="color: #000000;">=</span><span style="color: #000000;"> DriverManager.getConnection(url,user,password);  <br /> </span></div> 8、access数据库直q用ODBC?br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;"><br /> Class.forName(</span><span style="color: #000000;">"</span><span style="color: #000000;">sun.jdbc.odbc.JdbcOdbcDriver</span><span style="color: #000000;">"</span><span style="color: #000000;">) ;<br /> String url</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=</span><span style="color: #000000;">"</span><span style="color: #000000;">+</span><span style="color: #000000;">application.getRealPath(</span><span style="color: #000000;">"</span><span style="color: #000000;">/Data/ReportDemo.mdb</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br /> Connection conn </span><span style="color: #000000;">=</span><span style="color: #000000;"> DriverManager.getConnection(url,</span><span style="color: #000000;">""</span><span style="color: #000000;">,</span><span style="color: #000000;">""</span><span style="color: #000000;">);<br /> Statement stmtNew</span><span style="color: #000000;">=</span><span style="color: #000000;">conn.createStatement() ; <br /> </span></div> <div style="text-align: right;"><span style="font-size: 8pt;">《{?/span><br /> <br /> </div> <img src ="http://www.aygfsteel.com/ec2008/aggbug/196000.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/ec2008/" target="_blank">月芽?/a> 2008-04-25 16:07 <a href="http://www.aygfsteel.com/ec2008/archive/2008/04/25/196000.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java基础Q持l更CQ?/title><link>http://www.aygfsteel.com/ec2008/archive/2007/11/04/158125.html</link><dc:creator>月芽?/dc:creator><author>月芽?/author><pubDate>Sun, 04 Nov 2007 14:03:00 GMT</pubDate><guid>http://www.aygfsteel.com/ec2008/archive/2007/11/04/158125.html</guid><wfw:comment>http://www.aygfsteel.com/ec2008/comments/158125.html</wfw:comment><comments>http://www.aygfsteel.com/ec2008/archive/2007/11/04/158125.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/ec2008/comments/commentRss/158125.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/ec2008/services/trackbacks/158125.html</trackback:ping><description><![CDATA[<span style="color: #6494a0;">资料攉于互联网Q如有不妥,请告之)、个人心得?/span><br /> <br /> 1.默认的Q点类型是双精度(doubleQ,要想要一个float必须在QҎ后面加F或者f。如Qfloat pi = 3.14;是错误的<br /> <br /> 2.点型{化ؓ整型Ӟ不进行四舍五入,直接截断数点后面的?Java的类型检查很严格Q从低精度{换到高精度是无须昑ּ转换的,double d = 123;。但是反q来Q进行窄化{换,由高_ֺ向低_ֺQ或者一U类型到另一U类型,则必M用强制类型{化?br /> <br /> 3.在数l声明中包含数组长度永远是不合法的!如:int[5] arr; 。因为,声明的时候ƈ没有实例化Q何对象,只有在实例化数组对象ӞJVM才分配空_q时才与长度有关?br /> <br /> 4.对象cd的数l虽然被默认初始化了Q但是ƈ没有调用其构造函数。也是_Car[] myCar = new Car[10];只创Z一个myCar数组对象Qƈ没有创徏Car对象的Q何实例!q与单纯的new一个对象不同!单个的new对象Ӟ׃调用相应的构造函数?br /> <br /> 5.数组有length属性,字符串有lengthQ)Ҏ<br /> <br /> 6.String中的每个字符都是一?6位的Unicode字符Q用Unicode很容易表达丰富的国际化字W集Q比如很好的中文支持。char中可以保存一个中文汉字,应ؓchar也是一?6位的Unicode字符度ؓ0?br /> <br /> 8.String对象是不变的Q可以变化的是String对象的引用?br /> <br /> 9.String的不变性的机制昄会在String帔R内有大量的冗余。如Q?1" + "2" + "3" +......+ "n" 产生了n+(n+1)个String对象Q因此JavaZ更有效地使用内存QJVM留出一块特D的内存区域Q被UCؓ“String帔R?#8221;。对 String多么照顾啊!当编译器遇见String帔R的时候,它检查该池内是否已经存在相同的String帔R。如果找刎ͼ把新常量的引用指向现有?StringQ不创徏M新的String帔R对象?br /> <br /> 10.java中的对象是在堆中被创建的QnewQ,但常量类的String有点特别Q像“haha”q样的字W串ҎW?条是存放在String帔R池的Q而String s=new String("haha");实在堆中被创徏的?br /> <br /> 11.throw和throws关键字。throw用在Ҏ体中抛出异常Q后面是一个具体的异常对象。throws用在Ҏ参数列表括号的后面,用来声明此方法会抛出的异常种c,后面跟着一个异常类?br /> <br /> 12.要把相应的System.out和System.err信息输入到文件中。代码:<br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;">        </span><span style="color: #0000ff;">try</span><span style="color: #000000;">   {<br />             System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">没上</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />             System.setOut(</span><span style="color: #0000ff;">new</span><span style="color: #000000;">   PrintStream(</span><span style="color: #0000ff;">new</span><span style="color: #000000;">   FileOutputStream(</span><span style="color: #0000ff;">new</span><span style="color: #000000;">   File( </span><span style="color: #000000;">"</span><span style="color: #000000;">c:/test.txt </span><span style="color: #000000;">"</span><span style="color: #000000;">))));<br />             System.out.println( </span><span style="color: #000000;">"</span><span style="color: #000000;">haha </span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />             System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">error</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />         }   </span><span style="color: #0000ff;">catch</span><span style="color: #000000;">   (Exception   e)   {<br />             e.printStackTrace();<br />         }</span></div> err的吧setOut改ؓsetErr可以了?br /> <br /> <br /> <img src ="http://www.aygfsteel.com/ec2008/aggbug/158125.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/ec2008/" target="_blank">月芽?/a> 2007-11-04 22:03 <a href="http://www.aygfsteel.com/ec2008/archive/2007/11/04/158125.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Spring容器的启动方?/title><link>http://www.aygfsteel.com/ec2008/archive/2007/10/11/152063.html</link><dc:creator>月芽?/dc:creator><author>月芽?/author><pubDate>Thu, 11 Oct 2007 07:50:00 GMT</pubDate><guid>http://www.aygfsteel.com/ec2008/archive/2007/10/11/152063.html</guid><wfw:comment>http://www.aygfsteel.com/ec2008/comments/152063.html</wfw:comment><comments>http://www.aygfsteel.com/ec2008/archive/2007/10/11/152063.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/ec2008/comments/commentRss/152063.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/ec2008/services/trackbacks/152063.html</trackback:ping><description><![CDATA[一、在web.xml中配|Spring容器的启?nbsp;Q通用的方法)<br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;"><</span><span style="color: #000000;">context</span><span style="color: #000000;">-</span><span style="color: #000000;">param</span><span style="color: #000000;">></span><span style="color: #000000;"><br />      </span><span style="color: #000000;"><</span><span style="color: #000000;">param</span><span style="color: #000000;">-</span><span style="color: #000000;">name</span><span style="color: #000000;">></span><span style="color: #000000;">contextConfigLocation</span><span style="color: #000000;"></</span><span style="color: #000000;">param</span><span style="color: #000000;">-</span><span style="color: #000000;">name</span><span style="color: #000000;">></span><span style="color: #000000;"> <br />      </span><span style="color: #000000;"><</span><span style="color: #000000;">param</span><span style="color: #000000;">-</span><span style="color: #000000;">value</span><span style="color: #000000;">>/</span><span style="color: #000000;">WEB</span><span style="color: #000000;">-</span><span style="color: #000000;">INF</span><span style="color: #000000;">/</span><span style="color: #000000;">classes</span><span style="color: #000000;">/</span><span style="color: #000000;">applicationContext.xml</span><span style="color: #000000;"></</span><span style="color: #000000;">param</span><span style="color: #000000;">-</span><span style="color: #000000;">value</span><span style="color: #000000;">></span><span style="color: #000000;">  <br />                        </span><span style="color: #008000;">//</span><span style="color: #008000;">如有多个spring配置文g用逗号隔开</span><span style="color: #008000;"><br /> </span><span style="color: #000000;"> </span><span style="color: #000000;"></</span><span style="color: #000000;">context</span><span style="color: #000000;">-</span><span style="color: #000000;">param</span><span style="color: #000000;">></span><span style="color: #000000;"><br />      </span><span style="color: #000000;"><!--</span><span style="color: #000000;"> 通过Listener配置Spring容器随Web应用的启动而初始化 </span><span style="color: #000000;">--></span><span style="color: #000000;"><br /> </span><span style="color: #000000;"><</span><span style="color: #000000;">listener</span><span style="color: #000000;">></span><span style="color: #000000;"><br />      </span><span style="color: #000000;"><</span><span style="color: #000000;">listener</span><span style="color: #000000;">-</span><span style="color: #0000ff;">class</span><span style="color: #000000;">></span><span style="color: #000000;">org.springframework.web.context.ContextLoaderListener</span><span style="color: #000000;"></</span><span style="color: #000000;">listener</span><span style="color: #000000;">-</span><span style="color: #0000ff;">class</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #000000;"></</span><span style="color: #000000;">listener</span><span style="color: #000000;">></span></div> <br /> 二、struts1的启动方?br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;"><</span><span style="color: #000000;">plug</span><span style="color: #000000;">-</span><span style="color: #000000;">in className</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">org.springframework.web.struts.ContextLoaderPlugIn</span><span style="color: #000000;">"</span><span style="color: #000000;">></span><span style="color: #000000;"><br />     </span><span style="color: #000000;"><</span><span style="color: #000000;">set</span><span style="color: #000000;">-</span><span style="color: #000000;">property property</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">contextConfigLocation</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;">/WEB-INF/applicationContext.xml,</span><span style="color: #000000;"><br /> </span><span style="color: #000000;">               </span><span style="color: #000000;">/</span><span style="color: #000000;">WEB</span><span style="color: #000000;">-</span><span style="color: #000000;">INF</span><span style="color: #000000;">/</span><span style="color: #000000;">action</span><span style="color: #000000;">-</span><span style="color: #000000;">servlet.xml</span><span style="color: #000000;">"</span><span style="color: #000000;"> /></span><span style="color: #000000;"><br /> </span><span style="color: #000000;"></</span><span style="color: #000000;">plug</span><span style="color: #000000;">-</span><span style="color: #000000;">in</span><span style="color: #000000;">></span></div> <?gt;<br /> <img src ="http://www.aygfsteel.com/ec2008/aggbug/152063.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/ec2008/" target="_blank">月芽?/a> 2007-10-11 15:50 <a href="http://www.aygfsteel.com/ec2008/archive/2007/10/11/152063.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ActionServletc?/title><link>http://www.aygfsteel.com/ec2008/archive/2007/10/11/151961.html</link><dc:creator>月芽?/dc:creator><author>月芽?/author><pubDate>Thu, 11 Oct 2007 02:19:00 GMT</pubDate><guid>http://www.aygfsteel.com/ec2008/archive/2007/10/11/151961.html</guid><wfw:comment>http://www.aygfsteel.com/ec2008/comments/151961.html</wfw:comment><comments>http://www.aygfsteel.com/ec2008/archive/2007/10/11/151961.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/ec2008/comments/commentRss/151961.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/ec2008/services/trackbacks/151961.html</trackback:ping><description><![CDATA[    <p class="MsoNormal" style="text-indent: 18pt;"><span style="font-size: 9pt;" lang="EN-US">ActionServlet </span><span style="font-size: 9pt; font-family: 宋体;">cd应的cd?/span><span style="font-size: 9pt;" lang="EN-US">org.apche.struts.action.ActionServlet</span><span style="font-size: 9pt; font-family: 宋体;">Q它?/span><span style="font-size: 9pt;" lang="EN-US">struts</span><span style="font-size: 9pt; font-family: 宋体;">框架的核心控制器lgQ所有用Lh都先?/span><span style="font-size: 9pt;" lang="EN-US">ActionServlet</span><span style="font-size: 9pt; font-family: 宋体;">来处理,然后再由</span><span style="font-size: 9pt;" lang="EN-US">ActionServlet</span><span style="font-size: 9pt; font-family: 宋体;">把请求{发给其他lg?/span><span style="font-size: 9pt;" lang="EN-US"><br /> Struts</span><span style="font-size: 9pt; font-family: 宋体;">只允怸个应用中配置一?/span><span style="font-size: 9pt;" lang="EN-US"> ActionServlet</span><span style="font-size: 9pt; font-family: 宋体;">Q但是可以将</span><span style="font-size: 9pt;" lang="EN-US">ActionServlet</span><span style="font-size: 9pt; font-family: 宋体;">扩展?/span><span style="font-size: 9pt;" lang="EN-US"><br /> Servlet</span><span style="font-size: 9pt; font-family: 宋体;">容器在启动时Q或者用户首ơ请?/span><span style="font-size: 9pt;" lang="EN-US">ActionServlet</span><span style="font-size: 9pt; font-family: 宋体;">时加?/span><span style="font-size: 9pt;" lang="EN-US">ActionServlet</span><span style="font-size: 9pt; font-family: 宋体;">c,在上q情况下Q?/span><span style="font-size: 9pt;" lang="EN-US">Servlet</span><span style="font-size: 9pt; font-family: 宋体;">容器都会?/span><span style="font-size: 9pt;" lang="EN-US">ActionServlet</span><span style="font-size: 9pt; font-family: 宋体;">被加载后立即执行它的</span><span style="font-size: 9pt;" lang="EN-US">init()</span><span style="font-size: 9pt; font-family: 宋体;">Ҏ?/span><span style="font-size: 9pt;" lang="EN-US">Init()</span><span style="font-size: 9pt; font-family: 宋体;">q程如下Q?/span><span style="font-size: 9pt;" lang="EN-US"><br /> </span><span style="font-size: 9pt; font-family: 宋体;">  </span><span style="font-size: 9pt;" lang="EN-US">1.</span><span style="font-size: 9pt; font-family: 宋体;">调用</span><span style="font-size: 9pt;" lang="EN-US">initInternal()</span><span style="font-size: 9pt; font-family: 宋体;">ҎQ初始化</span><span style="font-size: 9pt;" lang="EN-US">struts</span><span style="font-size: 9pt; font-family: 宋体;">框架内在的消息资源,如与pȝ日志相关的通知、警告和错误信息?/span><span style="font-size: 9pt;" lang="EN-US"><br /> </span><span style="font-size: 9pt; font-family: 宋体;">  </span><span style="font-size: 9pt;" lang="EN-US">2.</span><span style="font-size: 9pt; font-family: 宋体;">调用</span><span style="font-size: 9pt;" lang="EN-US">initOther()</span><span style="font-size: 9pt; font-family: 宋体;">Q从</span><span style="font-size: 9pt;" lang="EN-US">web.xml</span><span style="font-size: 9pt; font-family: 宋体;">文g中加?/span><span style="font-size: 9pt;" lang="EN-US">ActionServlet</span><span style="font-size: 9pt; font-family: 宋体;">的初始化参数Q如</span><span style="font-size: 9pt;" lang="EN-US">config </span><span style="font-size: 9pt; font-family: 宋体;">参数</span><span style="font-size: 9pt;" lang="EN-US"><br /> </span><span style="font-size: 9pt; font-family: 宋体;">  </span><span style="font-size: 9pt;" lang="EN-US">3.</span><span style="font-size: 9pt; font-family: 宋体;">调用</span><span style="font-size: 9pt;" lang="EN-US">initServlet(),</span><span style="font-size: 9pt; font-family: 宋体;">?/span><span style="font-size: 9pt;" lang="EN-US">web.xml</span><span style="font-size: 9pt; font-family: 宋体;">文g中加?/span><span style="font-size: 9pt;" lang="EN-US">ActionServlet</span><span style="font-size: 9pt; font-family: 宋体;">?/span><span style="font-size: 9pt;" lang="EN-US">URL</span><span style="font-size: 9pt; font-family: 宋体;">映射信息。此外还会注?/span><span style="font-size: 9pt;" lang="EN-US">web.xml</span><span style="font-size: 9pt; font-family: 宋体;">?/span><span style="font-size: 9pt;" lang="EN-US"><br /> struts</span><span style="font-size: 9pt; font-family: 宋体;">配置文g所使用?/span><span style="font-size: 9pt;" lang="EN-US">DTD</span><span style="font-size: 9pt; font-family: 宋体;">文gQ这?/span><span style="font-size: 9pt;" lang="EN-US">DTD</span><span style="font-size: 9pt; font-family: 宋体;">文g用来验证</span><span style="font-size: 9pt;" lang="EN-US">web.xml</span><span style="font-size: 9pt; font-family: 宋体;">?/span><span style="font-size: 9pt;" lang="EN-US">struts</span><span style="font-size: 9pt; font-family: 宋体;">配置文g的语?/span><span style="font-size: 9pt;" lang="EN-US"><br /> </span><span style="font-size: 9pt; font-family: 宋体;">  </span><span style="font-size: 9pt;" lang="EN-US">4.</span><span style="font-size: 9pt; font-family: 宋体;">调用</span><span style="font-size: 9pt;" lang="EN-US">initModuleConfig()</span><span style="font-size: 9pt; font-family: 宋体;">Q加载ƈ解析默认子应用模块的</span><span style="font-size: 9pt;" lang="EN-US">struts</span><span style="font-size: 9pt; font-family: 宋体;">配置文gQ创?/span><span style="font-size: 9pt;" lang="EN-US">ModuleConfig</span><span style="font-size: 9pt; font-family: 宋体;">对象Q把它存?/span><span style="font-size: 9pt;" lang="EN-US">ServletContext</span><span style="font-size: 9pt; font-family: 宋体;">?/span><span style="font-size: 9pt;" lang="EN-US">  <br />         5.</span><span style="font-size: 9pt; font-family: 宋体;">调用</span><span style="font-size: 9pt;" lang="EN-US">intiModuleMessageResources()</span><span style="font-size: 9pt; font-family: 宋体;">Q加载ƈ初始化默认子应用模块的消息资源,创徏</span><span style="font-size: 9pt;" lang="EN-US"><br /> MessageResources</span><span style="font-size: 9pt; font-family: 宋体;">对象Q把它存?/span><span style="font-size: 9pt;" lang="EN-US">ServletContext</span><span style="font-size: 9pt; font-family: 宋体;">?/span><span style="font-size: 9pt;" lang="EN-US"><br />         6.</span><span style="font-size: 9pt; font-family: 宋体;">调用</span><span style="font-size: 9pt;" lang="EN-US">initModuleDataSources()</span><span style="font-size: 9pt; font-family: 宋体;">ҎQ加载ƈ初始化默认子应用模块的数据源。如果没有在</span><span style="font-size: 9pt;" lang="EN-US">struts</span><span style="font-size: 9pt; font-family: 宋体;">配置文g中没有定?/span><span style="font-size: 9pt;" lang="EN-US"><data-sources></span><span style="font-size: 9pt; font-family: 宋体;">元素Q就忽略q一程</span><span style="font-size: 9pt;" lang="EN-US"><br />          7.</span><span style="font-size: 9pt; font-family: 宋体;">调用</span><span style="font-size: 9pt;" lang="EN-US">initModulePlugins(),</span><span style="font-size: 9pt; font-family: 宋体;">加蝲q初始化默认子应用模块的所有插?/span><span style="font-size: 9pt;" lang="EN-US"><br />          8.</span><span style="font-size: 9pt; font-family: 宋体;">当默认子应用模块被成功初始化后,如果应用q包括其他子应用模块Q将重复程</span><span style="font-size: 9pt;" lang="EN-US">4-7</span><span style="font-size: 9pt; font-family: 宋体;">Q分别对其他子应用模块进行初始化。扩?/span><span style="font-size: 9pt;" lang="EN-US">ActionServlet</span><span style="font-size: 9pt; font-family: 宋体;">c,定义如下的类Q?/span><span style="font-size: 9pt;" lang="EN-US"><o:p></o:p></span></p> <span style="font-size: 8pt;"><span style="font-size: 8pt;"><span style="font-size: 36pt;"><span style="font-size: 8pt;"><span style="font-size: 12pt;"><font size="3"> </font> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #0000ff;">package</span><span style="color: #000000;"> sample;<br /> <br /> <br /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> ExtendedActionServlet </span><span style="color: #0000ff;">extends</span><span style="color: #000000;"> ActionServlet {<br />         </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">void</span><span style="color: #000000;"> init() </span><span style="color: #0000ff;">throws</span><span style="color: #000000;"> ServletException {<br />                </span><span style="color: #0000ff;">super</span><span style="color: #000000;">.init();<br />                </span><span style="color: #008000;">//</span><span style="color: #008000;">do some operations</span><span style="color: #008000;"><br /> </span><span style="color: #000000;">               ……………<br />         }<br /> }<br /> <br /> <br /> </span></div> </span></span></span></span></span><span style="font-size: 9pt; font-family: 宋体;">扩展完类后,q应该在</span><span style="font-size: 9pt; font-family: "Times New Roman";" lang="EN-US">web.xml</span><span style="font-size: 9pt; font-family: 宋体;">文g中如下配|:</span><span style="font-size: 8pt;"><span style="font-size: 8pt;"><span style="font-size: 36pt;"><span style="font-size: 8pt;"><span style="font-size: 12pt;"> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000;"><</span><span style="color: #000000;">servlet</span><span style="color: #000000;">></span><span style="color: #000000;"><br />         </span><span style="color: #000000;"><</span><span style="color: #000000;">servlet</span><span style="color: #000000;">-</span><span style="color: #000000;">name</span><span style="color: #000000;">></span><span style="color: #000000;">sample</span><span style="color: #000000;"></</span><span style="color: #000000;">servlet</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;"><br /> <</span><span style="color: #000000;">servlet</span><span style="color: #000000;">-</span><span style="color: #0000ff;">class</span><span style="color: #000000;">></span><span style="color: #000000;">sample.ExtendedActionServlet</span><span style="color: #000000;"></</span><span style="color: #000000;">servlet</span><span style="color: #000000;">-</span><span style="color: #0000ff;">class</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #000000;"></</span><span style="color: #000000;">servlet</span><span style="color: #000000;">></span><span style="color: #000000;"><br /> </span><span style="color: #000000;"><</span><span style="color: #000000;">servlet</span><span style="color: #000000;">-</span><span style="color: #000000;">mapping</span><span style="color: #000000;">></span><span style="color: #000000;"><br />        </span><span style="color: #000000;"><</span><span style="color: #000000;">servlet</span><span style="color: #000000;">-</span><span style="color: #000000;">name</span><span style="color: #000000;">></span><span style="color: #000000;">sample</span><span style="color: #000000;"></</span><span style="color: #000000;">servlet</span><span style="color: #000000;">-</span><span style="color: #000000;">name</span><span style="color: #000000;">></span><span style="color: #000000;"><br />        </span><span style="color: #000000;"><</span><span style="color: #000000;">url</span><span style="color: #000000;">-</span><span style="color: #000000;">pattern</span><span style="color: #000000;">>/</span><span style="color: #000000;">action</span><span style="color: #008000;">/*</span><span style="color: #008000;"><url-pattern><br /> //上面?action/*表示负责处理所有以/action为前~的URL,后面?表示转义<br /> <servlet-mapping><br /> <br /> </span></div> <span id="wmqeeuq" class="01"> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><br /> <span id="wmqeeuq" class="01"><span style="font-family: 宋体;"><font size="3"></font></span></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span id="wmqeeuq" class="01"></span><span style="font-size: 9pt; font-family: "Times New Roman";" lang="EN-US"><</span><span style="font-size: 9pt; font-family: 宋体;">?/span><span style="font-size: 9pt; font-family: "Times New Roman";" lang="EN-US">></span><br /> <span style="font-family: 宋体;"><font size="3"></font></span></p> <p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: 宋体;"><font size="3"><span lang="EN-US"><br /> </span></font></span></p> </span><br /> <br /> </span></span></span></span></span> <img src ="http://www.aygfsteel.com/ec2008/aggbug/151961.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/ec2008/" target="_blank">月芽?/a> 2007-10-11 10:19 <a href="http://www.aygfsteel.com/ec2008/archive/2007/10/11/151961.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java 构造函数调用顺?/title><link>http://www.aygfsteel.com/ec2008/archive/2007/10/10/151799.html</link><dc:creator>月芽?/dc:creator><author>月芽?/author><pubDate>Wed, 10 Oct 2007 08:46:00 GMT</pubDate><guid>http://www.aygfsteel.com/ec2008/archive/2007/10/10/151799.html</guid><wfw:comment>http://www.aygfsteel.com/ec2008/comments/151799.html</wfw:comment><comments>http://www.aygfsteel.com/ec2008/archive/2007/10/10/151799.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/ec2008/comments/commentRss/151799.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/ec2008/services/trackbacks/151799.html</trackback:ping><description><![CDATA[当一个复杂的对象被构造时,它的构造函数按下面的顺序被调用(that the order of constructor calls for a complex object is as follows) <p>    1.其基c?base-class)的构造函数被调用,q个步骤以递归的方式重?所以最底层(the root of hierarchy)的构造函数首先被执行,然后是它上一层派生类(the next-derived class)...直到最层的派生类(the most-derived class).</p> <p>The base-class constructor is called. This step is repeated recursively such that the root of the hierarchy is constructed first, followed by the next-derived class, etc., until the most-derived class is reached.)</p> <p>    2.如果有包含关p?composition),那么它的成员对象按照声明的顺序被构?</p> <p>Member initializers are called in the order of declaration.</p> <p>    3.zcL造函数的内容(body)被执?</p> <p>The body of the derived-class constructor is called.</p> <p>    一个实?</p> <br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #0000ff;">class</span><span style="color: #000000;"> Cake{<br />    Cake(){System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">Cake()</span><span style="color: #000000;">"</span><span style="color: #000000;">);}<br /> }<br /> <br /> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Meal {<br />   Meal() { System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">Meal()</span><span style="color: #000000;">"</span><span style="color: #000000;">); }<br /> }<br /> <br /> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Bread {<br />   Bread() { System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">Bread()</span><span style="color: #000000;">"</span><span style="color: #000000;">); }<br /> }<br /> <br /> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Cheese {<br />   Cheese() { System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">Cheese()</span><span style="color: #000000;">"</span><span style="color: #000000;">); }<br /> }<br /> <br /> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Lettuce {<br />   Lettuce() { System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">Lettuce()</span><span style="color: #000000;">"</span><span style="color: #000000;">); }<br /> }<br /> <br /> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Lunch </span><span style="color: #0000ff;">extends</span><span style="color: #000000;"> Meal {<br />   Lunch() { System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">Lunch()</span><span style="color: #000000;">"</span><span style="color: #000000;">); }<br /> }<br /> <br /> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> PortableLunch </span><span style="color: #0000ff;">extends</span><span style="color: #000000;"> Lunch {<br />    </span><span style="color: #008000;">//</span><span style="color: #008000;">if make derived-class object as the menber of the base-class will lead a infinite<br />    </span><span style="color: #008000;">//</span><span style="color: #008000;">loop and program will stop because of the memory consumed<br />    <br />    </span><span style="color: #008000;">//</span><span style="color: #008000;">private Sandwich s=new Sandwich(); </span><span style="color: #008000;"><br /> </span><span style="color: #000000;">  </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Cake a</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Cake();<br />   PortableLunch() { System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">PortableLunch()</span><span style="color: #000000;">"</span><span style="color: #000000;">);}<br /> }<br /> <br /> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Sandwich </span><span style="color: #0000ff;">extends</span><span style="color: #000000;"> PortableLunch<br /> {<br />   </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Bread b </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Bread();<br />   </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Cheese c </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Cheese();<br />   </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Lettuce l </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Lettuce();<br />   <br />   </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Sandwich() {<br />     System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">Sandwich()</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />   }<br />   <br />   </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)  {<br />     </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Sandwich();<br />   }<br /> }</span></div> <p>输出:</p> <p>Meal()<br /> Lunch()<br /> Cake()<br /> PortableLunch()<br /> Bread()<br /> Cheese()<br /> Lettuce()<br /> Sandwich()</p> <p>   main()函数中要构造一个Sandwich的对?调用(q不是执?它基cPortableLunch的构造函?PortableLunch又? 归的调用,然后是Meal,Meal是承的最底层的基c?不算Object)所以它的构造函数首先被执行,然后按次序返回到Lunch, PortableLunch,但在PortableLunch的构造函数被执行之前,它的成员对象Cake a先按照声明的序被构?然后执行PortableLunch(),接着是Sandwich的成员对?最后是Sandwich().</p> <p>?被注释掉的代?base-class的对象作为derive-class的成员对?q样会递归无法l束,最后程序因堆栈耗尽而结?Exception in  thread main java.lang.StackOverflowError).</p> <br /> <br /> <p id="TBPingURL">Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1134115(?</p> <p><br /> </p> <img src ="http://www.aygfsteel.com/ec2008/aggbug/151799.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/ec2008/" target="_blank">月芽?/a> 2007-10-10 16:46 <a href="http://www.aygfsteel.com/ec2008/archive/2007/10/10/151799.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Subversion(SVN)版本控制安装使用指南http://www.aygfsteel.com/ec2008/archive/2007/09/17/145881.html月芽?/dc:creator>月芽?/author>Mon, 17 Sep 2007 07:49:00 GMThttp://www.aygfsteel.com/ec2008/archive/2007/09/17/145881.htmlhttp://www.aygfsteel.com/ec2008/comments/145881.htmlhttp://www.aygfsteel.com/ec2008/archive/2007/09/17/145881.html#Feedback0http://www.aygfsteel.com/ec2008/comments/commentRss/145881.htmlhttp://www.aygfsteel.com/ec2008/services/trackbacks/145881.html阅读全文

]]>
What is Struts? Intro to a framework for web applications介绍Struts的一文?/title><link>http://www.aygfsteel.com/ec2008/archive/2007/09/12/144656.html</link><dc:creator>月芽?/dc:creator><author>月芽?/author><pubDate>Wed, 12 Sep 2007 15:58:00 GMT</pubDate><guid>http://www.aygfsteel.com/ec2008/archive/2007/09/12/144656.html</guid><wfw:comment>http://www.aygfsteel.com/ec2008/comments/144656.html</wfw:comment><comments>http://www.aygfsteel.com/ec2008/archive/2007/09/12/144656.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/ec2008/comments/commentRss/144656.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/ec2008/services/trackbacks/144656.html</trackback:ping><description><![CDATA[<strong>Introduction</strong> <p class="ttext-western">This Tutorial will explain you, what struts is, how it builds itself up and why it is advantage to use it for your web application. First, I will provide you some informations about Java Servlets, Java Server Pages and Java Beans, because they are parts of struts.</p> <h2>Java Servlets</h2> <p class="ttext-western">Servlets represents java programs that runs on a web server. They allow the developer to produce dynamic web sites with java.</p> <p class="ttext-western">A Servlet has the following tasks</p> <ul> <li> <p class="ttext-western">It reads and processed data, which a user typed in a HTML form on a web page. </p> </li> <li> <p class="ttext-western">If necessary other informations will be processed. For Example what browser or system will be used.</p> </li> <li> <p class="ttext-western">It generate results with the existing data. It calls the business logic directly in the servlet or another class, which contain the logic or executes a database query.</p> </li> <li> <p class="ttext-western">The results will be formated. If the browser. If the Browser expects an answer in the HTML format, then the results must be formatted in accordance with the standard. It is possible to return different formats of data with a servlet. (gif, jpeg, doc, etc.).</p> </li> <li> <p class="ttext-western">Suitable answer parameters are set. Befor the servlet return the data to the browser, it sends some parameter. The parameter contains the format, that will returned by the servlet, what time the browser use to cache the site and some more.</p> </li> <li> <p class="ttext-western">Return the document, in the format that it sends befor, to the browser.</p> </li> </ul> <h2>Java Server Pages (JSP)</h2> <p class="ttext-western">JavaServer Pages (JSP) are text documents, which are similar to HTML Files. But you find also java code in the JSP File. JavaServer Pages allow you to mix regulare, static HTML with dynamic generated contents of servlets. The java code is inserted in the HTML document on a JSP File, differently to a servlet, where the HTML code is embedded in the java code.</p> <strong>Java Beans</strong> <p class="ttext-western">Java Beans are nothing else as classes, which keep a fixed naming convention, defined by Sun, for their event processing and methods. The attributes (variables) of the java beans are private. The access to these attributes is managed by access methods. The java specification specify these access methodes (getter and setter methods). If a java bean contains a attribute name, you can set or get the value of the attributes with the methode setName() or getName().</p> <h2>Business logic</h2> <p class="ttext-western">The business logic is the core of the application. The processes are implemented in the business logic to manage the data. For Example: when someone borrows a book in a library, that is a process in the business logic. The process change the state of the data (is the book borrowsed or not) or reads the state and then provide this information for a dialog.</p> <p class="ttext-western"><br /> </p> <p class="ttext-western">This short outline should be sufficient, in order to understand the structure of struts.</p> <strong>Why is struts helpful?</strong> <h2>Sepration of dialogs and business logic (functionality)</h2> <p lang="de-DE">Some peoples develop web applications with Perl or PHP and implement their SQL Querys and the business logics directly in the HTML document.</p> <p lang="de-DE">The source code looks like the following example:</p> <br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">html</span><span style="color: rgb(0, 0, 0);">><</span><span style="color: rgb(0, 0, 0);">head</span><span style="color: rgb(0, 0, 0);">><</span><span style="color: rgb(0, 0, 0);">title</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);">Important title</span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">title</span><span style="color: rgb(0, 0, 0);">></</span><span style="color: rgb(0, 0, 0);">head</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">body</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">someScript</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> dbConnection </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> openDBConnection(someDB)<br /> resultSet </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> dbConnection.executeQuery(</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">select bookName from books</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /> loop over resultSet{<br />  print (resultSet.field(</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">bookName</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br /> }<br /> </span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">someScript</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">body</span><span style="color: rgb(0, 0, 0);">></</span><span style="color: rgb(0, 0, 0);">html</span><span style="color: rgb(0, 0, 0);">></span></div> <br /> <p lang="en-US">You can develop in this way, when you use java servlets or JSP. It is convenient in small projects. But imagine you have 70 dialogs, many database querys in this dialogs and you want to define a field status, setting if a book is deleted or not.</p> <p class="ttext-western" lang="de-DE">Good luck</p> <p class="ttext-western">In order to alter functions and database querys easily, we should seperate these from the dialogs.</p> <strong>Advantage of separation</strong> <ul> <li> <p class="ttext-western">Changes on the functionality, without working in the dialogs.</p> </li> <li> <p class="ttext-western">Better overview, functionality not mixed with the dialogs.</p> </li> <li> <p class="ttext-western">Easy maintain a application</p> </li> <li> <p class="ttext-western">Different dialogs, but the same functionality</p> </li> </ul> <h2>Central control</h2> <p class="ttext-western">In order to control the interaction between the dialogs and business processes, you need a central control unit. This control unit manage all importants courses of the application, when which business process and which dialog will be used.</p> <p>You have some disadvantages, if you implement the order of the processes directly to the business logic.</p> <ol> <li> <p>You can not reuse a part of the processes. In the following picture the process "Give Money" will bring you to the process "Select sort of icecream" every time. </p> </li> <li> <p>If you want to change the order of the processes or add more options, you have to change it directly in the business logic.</p> </li> </ol> <p><img src="http://www.laliluna.de/assets/images/tutorials/what-is-struts/process.gif" alt="" /><br /> </p> <br /> <h2>Advantages of the central control</h2> <ul> <li> <p class="ttext-western">It is easy to change the order of the business processes.</p> </li> <li> <p class="ttext-western">Better overview, which dialogs will be used on which business processes.</p> </li> <li> <p class="ttext-western"><span>A central place to control the interaction between business processes and dialogs.</span></p> </li> </ul> <h2>Translation into the technical language</h2> <p class="ttext-western"><strong>The name of this model is Model-View-Controller (MVC)</strong></p> <p class="ttext-western"><strong>Model </strong>(business logic / business processes ? Java Beans)</p> <p class="ttext-western"><strong>View</strong> (dialogs ? JavaServer Pages)</p> <p class="ttext-western"><strong>Controller</strong> (central control unit - Java Servlets)</p> <p class="ttext-western">You will find more about the MVC later in this tutorial.</p> <h2>More helpfully capabilities</h2> <h3>Internationalisation</h3> <p class="ttext-western">If you do not want to translate a dialog, you put the dialog texts directly into the JSP File.</p> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">html:form action</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">someAction</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />     Please give your name and the book title  </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">br</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />         Name:<br />  </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">html:text property</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">br</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />       Title:<br />  </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">html:text property</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">title</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">br</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">html:submit</span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">html:form</span><span style="color: rgb(0, 0, 0);">></span></div> <br /> Struts support resources files. In this files you can define a key for a text.<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);">bookDialog.formIntro</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">Please give your name and the title of the book.<br /> bookDialog.inputName</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">Name:<br /> bookDialog.bookTitle</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">Title:</span></div> <br /> This key can be used to diplay the text in the JSP file.<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">html:form action</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">someAction</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">bean:message key</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">bookDialog.formIntro</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">br</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">bean:message key</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">bookDialog.inputName</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">html:text property</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">br</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">bean:message key</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">bookDialog.bookTitle</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">html:text property</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">title</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">br</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">html:submit</span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">html:form</span><span style="color: rgb(0, 0, 0);">></span></div> <br /> <p class="ttext-western">Struts call the locale specific resource file by using the browser settings. You may overwrite this setting. It is quite easy to build an internationalized application with struts.</p> <h3>Error handling</h3> <p class="ttext-western">Struts saves error messages in the business logic and can display this in the dialogs. You can show all errors or bind an error to a field (ex. Emailaddress incorrect) and show the error beside of it.</p> <p class="ttext-western">The error messages will be added to the resource files, so the errors can be internationalised too.</p> <h3>Validation of input fields</h3> <p class="ttext-western">Whether an input field of a form contains a valid date, an email, a number or something else, in each application you have to check it. Struts supports a complete solution to validate form fields. You only have to define which form field will be checked and which error message will be displayed.</p> <strong>Components of struts</strong> <p class="ttext-western">JavaServer Pages (JSP) take over the role of dialogs in struts,</p> <p class="ttext-western">Java Beans take over the business logic and business processes and</p> <p class="ttext-western">Java Servlets take over the central control unit</p> <p class="ttext-western"><strong>Thats why we talk about three great parts of struts.</strong></p> <p class="ttext-western"><strong>Model </strong>(business logic / business processes ? Java Beans)</p> <p class="ttext-western"><strong>View</strong> (dialogs ? JavaServer Pages)</p> <p class="ttext-western"><strong>Controller</strong> (central control unit - Java Servlets)</p> <p class="ttext-western">The <strong>model</strong> represent the actual state of the application. Two kinds of java beans are used. There are java beans which contains the data of a form or data to display (ex. The books of a library) and java beans which includes the functionality of the application or call the business logic (when a user borrows a book).</p> <p class="ttext-western">The <strong>view component</strong> is responsible for the presentation of the data. The java server pages contain HTML, XML and Java Script, like a normal HTML site. Futhermore you can use java code. Struts provide tag libraries, a summary of functions, which can be use to prepare the data for displaying.</p> <p class="ttext-western">The last component is the controller. The controller manage the request of the web browser, which a user called by an address (URL). But also forward to an action which are execute and which dialogs will be used to display the informations.</p> <p class="ttext-western">The picture below illustrates the interaction between these components.</p> <p class="ttext-western"><img src="http://www.laliluna.de/assets/images/tutorials/what-is-struts/struts-workflow.gif" alt="" /><br /> <br /> </p> <p class="ttext-western">If the user sends a query with a browser, the controller(servlet) gets and processes this query. It decides which action will be called or to which view component it must be forward.</p> <p class="ttext-western">After the controller calls an action, the action can read data from a database and provide this data to the model component, java beans. The action (business logic) returns the "next step" to the controller. The controller checks what kind is the next step. (JSP View, next action, ...) and forwards to it.</p> <p class="ttext-western">The view component (JSP) reads the updated data from the model component and prepare this for the presentation. Then it sends the answer as HTML site back to the browser. The user sees the result of his query. </p> <strong>Struts configuration</strong> <p class="ttext-western">Struts will be configured with various configuration files. The following files are very important.</p> <p class="ttext-western">web.xml</p> <p class="ttext-western">struts-config.xml</p> <p class="ttext-western">Struts-Tag-Bibliotheken</p> <p class="ttext-western">Properties-Dateien</p> <p class="ttext-western">The illustration will show you, on which places struts use the configuration files.</p> <p class="ttext-western"><img src="http://www.laliluna.de/assets/images/tutorials/what-is-struts/configfiles-diagram.gif" alt="" /><br /> <br /> </p> <p class="ttext-western"><strong>web.xml</strong></p> <p class="ttext-western">With the web.xml you configure the web server for the struts application. In this file you can set where the web server find the struts-config.xml and some other global properties.</p> <p class="ttext-western"><strong>struts-config.xml</strong></p> <p class="ttext-western">The controller calls the business logic or a view with a name. The allocation of the names to the action classes or JSP Files(Views) will be set in the struts-config.xml. The advantage is that you can change the definition of the Workflows (Action Mapping), without using the long class names everytime. If you change a class name, you only have to change the name in the struts-config.xml. You don´t update any other part of your application.</p> <p class="ttext-western"><strong>Struts-Tag-Libraries</strong></p> <p class="ttext-western">The Struts-Tag-Libraries, a summary of functions, extends the functionality of JavaServer pages. They support the internationalisation (multi-language) of your web application, and the easy creation of form elements.</p> <p class="ttext-western"><strong>Properties files</strong></p> <p class="ttext-western">This files will be used to keep the internationalized textes of your web application. You can create a seperated properties file for each language, which contain all textes of the application.</p> <p lang="de-DE">转自Qhttp://www.laliluna.de<br /> </p> <img src ="http://www.aygfsteel.com/ec2008/aggbug/144656.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/ec2008/" target="_blank">月芽?/a> 2007-09-12 23:58 <a href="http://www.aygfsteel.com/ec2008/archive/2007/09/12/144656.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate对象的三U状?/title><link>http://www.aygfsteel.com/ec2008/archive/2007/09/12/144572.html</link><dc:creator>月芽?/dc:creator><author>月芽?/author><pubDate>Wed, 12 Sep 2007 09:58:00 GMT</pubDate><guid>http://www.aygfsteel.com/ec2008/archive/2007/09/12/144572.html</guid><wfw:comment>http://www.aygfsteel.com/ec2008/comments/144572.html</wfw:comment><comments>http://www.aygfsteel.com/ec2008/archive/2007/09/12/144572.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/ec2008/comments/commentRss/144572.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/ec2008/services/trackbacks/144572.html</trackback:ping><description><![CDATA[<p><font style="font-size: 14px;" face="Courier New">Hibernate的对象有3U状?分别?瞬时?Transient)、持久?Persistent)、脱?Detached).处于持久态的对象也称为PO(PersistenceObject),瞬时对象和脱对象也UCؓVO(ValueObject).</font></p> <p><font style="font-size: 14px;" face="Courier New"><strong>瞬时?/strong><br /> 由new命o开辟内存空间的java对象,</font></p> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);">eg.Person person</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">new Person(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">xiaoxiao</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">?/span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);</span></div> <p><font style="font-size: 14px;" face="Courier New">如果没有变量对该对象q行引用,它将被java虚拟机回?</font></p> <p><font style="font-size: 14px;" face="Courier New">瞬时对象在内存孤立存?它是携带信息的蝲?不和数据库的数据有Q何关联关p?在Hibernate?可通过session的save()或saveOrUpdate()Ҏ瞬时对象与数据库相兌,q将数据对应的插入数据库?此时该瞬时对象{变成持久化对?</font></p> <p><font style="font-size: 14px;" face="Courier New"><strong>持久?/strong><br /> 处于该状态的对象在数据库中具有对应的记录,q拥有一个持久化标识.如果是用hibernate的delete()Ҏ,对应的持久对象就变成瞬时对象,因数据库中的对应数据已被删除,该对象不再与数据库的记录兌.</font></p> <p><font style="font-size: 14px;" face="Courier New">当一个session执行close()或clear()、evict()之后,持久对象变成q对象,此时持久对象会变成脱对?此时该对象虽然具有数据库识别?但它已不在HIbernate持久层的理之下.</font></p> <p><font style="font-size: 14px;" face="Courier New">持久对象h如下特点:</font></p> <p><font style="font-size: 14px;" face="Courier New">1.和session实例兌;</font></p> <p><font style="font-size: 14px;" face="Courier New">2.在数据库中有与之兌的记?</font></p> <p><font style="font-size: 14px;" face="Courier New"><strong>q?br /> </strong>当与某持久对象关联的session被关闭后,该持久对象{变ؓq对象.当脱对象被重新兌到session上时,q再ơ{变成持久对象.</font></p> <p><font style="font-size: 14px;" face="Courier New">q对象拥有数据库的识别?可通过update()、saveOrUpdate(){方?转变成持久对?</font></p> <p><font style="font-size: 14px;" face="Courier New">q对象h如下特点:</font></p> <p><font style="font-size: 14px;" face="Courier New">1.本质上与瞬时对象相同,在没有Q何变量引用它?JVM会在适当的时候将它回?</font></p> <p><font style="font-size: 14px;" face="Courier New">2.比瞬时对象多了一个数据库记录标识?<br /> </font></p> <br /> <br /> <p id="TBPingURL">《{?/p> <img src ="http://www.aygfsteel.com/ec2008/aggbug/144572.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/ec2008/" target="_blank">月芽?/a> 2007-09-12 17:58 <a href="http://www.aygfsteel.com/ec2008/archive/2007/09/12/144572.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Generic Data Access Objects一关于泛型DAO的文?/title><link>http://www.aygfsteel.com/ec2008/archive/2007/09/12/144529.html</link><dc:creator>月芽?/dc:creator><author>月芽?/author><pubDate>Wed, 12 Sep 2007 08:22:00 GMT</pubDate><guid>http://www.aygfsteel.com/ec2008/archive/2007/09/12/144529.html</guid><wfw:comment>http://www.aygfsteel.com/ec2008/comments/144529.html</wfw:comment><comments>http://www.aygfsteel.com/ec2008/archive/2007/09/12/144529.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/ec2008/comments/commentRss/144529.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/ec2008/services/trackbacks/144529.html</trackback:ping><description><![CDATA[<p>This is a pattern for Data Access Objects with JDK 5.0, from the <a _base_>CaveatEmptor</a> example application. It is also explained in the book <a _base_>Java Persistence with Hibernate</a>. Two links you might find useful: <a _base_>Sessions and transactions</a> and <a _base_>Open Session in View</a>.</p> <p>This time I based the DAO example on interfaces. Tools like Hibernate already provide database portability, so persistence layer portability shouldn't be a driving motivation for interfaces. However, DAO interfaces make sense in more complex applications, when several persistence services are encapsulate in one persistence layer. I'd say that you should use Hibernate (or Java Persistence APIs) directly in most cases, the best reason to use an additional DAO layer is higher abstraction (e.g. methods like <tt>getMaximumBid()</tt> instead of <tt>session.createQuery(...)</tt> repeated a dozen times).</p> <h2>The DAO interfaces</h2> <p>I use one interface per persistent entity, with a super interface for common CRUDfunctionality:</p> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">interface</span><span style="color: rgb(0, 0, 0);"> GenericDAO</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">T, ID </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Serializable</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> {<br /> <br />     T findById(ID id, </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> lock);<br /> <br />     List</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> findAll();<br /> <br />     List</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> findByExample(T exampleInstance);<br /> <br />     T makePersistent(T entity);<br /> <br />     </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> makeTransient(T entity);<br /> }</span></div> <br /> You can already see that this is going to be a pattern for a state-oriented data access API, with methods such as <tt>makePersistent()</tt> and <tt>makeTransient()</tt>. Furthermore, to implement a DAO you have to provide a type and an identifier argument. As for most ORM solutions, identifier types have to be serializable. <p>The DAO interface for a particular entity extends the generic interface and provides the type arguments:</p> <br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">interface</span><span style="color: rgb(0, 0, 0);"> ItemDAO </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> GenericDAO</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Item, Long</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> {<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> String QUERY_MAXBID </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ItemDAO.QUERY_MAXBID</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> String QUERY_MINBID </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ItemDAO.QUERY_MINBID</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /> <br />     Bid getMaxBid(Long itemId);<br />     Bid getMinBid(Long itemId);<br /> <br /> }</span></div> <br /> <p>We basically separate generic CRUD operations and actual business-related data access operations from each other. (Ignore the named query constants for now, they are convenient if you use annotations.) However, even if only CRUD operations are needed for a particular entity, you should still write an interface for it, even it it is going to be empty. It is important to use a concrete DAO in your controller code, otherwise you will face some refactoring once you have to introduce specific data access operations for this entity.</p> <a style="width: 20px; height: 20px; text-indent: 20px; background-repeat: no-repeat; background-image: url(/CuteSoft_Client/CuteEditor/Load.ashx?type=image&file=anchor.gif);" name="A3" _base_></a> <h2>An implementation with Hibernate</h2> <p>An implementation of the interfaces could be done with any state-management capable persistence service. First, the generic CRUD implementation with Hibernate:</p> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> GenericHibernateDAO</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">T, ID </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Serializable</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />         </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> GenericDAO</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">T, ID</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> {<br /> <br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Class</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> persistentClass;<br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> Session session;<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> GenericHibernateDAO() {<br />         </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.persistentClass </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (Class</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);">) ((ParameterizedType) getClass()<br />                                 .getGenericSuperclass()).getActualTypeArguments()[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">];<br />      }<br /> <br />     @SuppressWarnings(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">unchecked</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> setSession(Session s) {<br />         </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.session </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> s;<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);"> Session getSession() {<br />         </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (session </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">)<br />             </span><span style="color: rgb(0, 0, 255);">throw</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> IllegalStateException(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Session has not been set on DAO before usage</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> session;<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Class</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> getPersistentClass() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> persistentClass;<br />     }<br /> <br />     @SuppressWarnings(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">unchecked</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> T findById(ID id, </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> lock) {<br />         T entity;<br />         </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (lock)<br />             entity </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE);<br />         </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />             entity </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (T) getSession().load(getPersistentClass(), id);<br /> <br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> entity;<br />     }<br /> <br />     @SuppressWarnings(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">unchecked</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> List</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> findAll() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> findByCriteria();<br />     }<br /> <br />     @SuppressWarnings(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">unchecked</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> List</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> findByExample(T exampleInstance, String[] excludeProperty) {<br />         Criteria crit </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> getSession().createCriteria(getPersistentClass());<br />         Example example </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">  Example.create(exampleInstance);<br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (String exclude : excludeProperty) {<br />             example.excludeProperty(exclude);<br />         }<br />         crit.add(example);<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> crit.list();<br />     }<br /> <br />     @SuppressWarnings(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">unchecked</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> T makePersistent(T entity) {<br />         getSession().saveOrUpdate(entity);<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> entity;<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> makeTransient(T entity) {<br />         getSession().delete(entity);<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> flush() {<br />         getSession().flush();<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> clear() {<br />         getSession().clear();<br />     }<br /> <br />     </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />      * Use this inside subclasses as a convenience method.<br />      </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />     @SuppressWarnings(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">unchecked</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)<br />     </span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);"> List</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> findByCriteria(Criterion<img src="http://www.aygfsteel.com/Images/dot.gif" alt="" /> criterion) {<br />         Criteria crit </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> getSession().createCriteria(getPersistentClass());<br />         </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> (Criterion c : criterion) {<br />             crit.add(c);<br />         }<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> crit.list();<br />    }<br /> <br /> }</span></div> <p>There are some interesting things in this implementation. First, it clearly needs a <tt>Session</tt> to work, provided with setter injection. You could also use constructor injection. How you set the <tt>Session</tt> and what scope this <tt>Session</tt> has is of no concern to the actual DAO implementation. A DAO should not control transactions or the <tt>Session</tt> scope. </p> <p>We need to suppress a few compile-time warnings about unchecked casts, because Hibernate's interfaces are JDK 1.4 only. What follows are the implementations of the generic CRUD operations, quite straightforward. The last method is quite nice, using another JDK 5.0 feature, <em>varargs</em>. It helps us to build <tt>Criteria</tt> queries in concrete entity DAOs. This is an example of a concrete DAO that extends the generic DAO implementation for Hibernate:</p> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> ItemDAOHibernate<br />         </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">     GenericHibernateDAO</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Item, Long</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />         </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);">  ItemDAO {<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Bid getMaxBid(Long itemId) {<br />         Query q </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> getSession().getNamedQuery(ItemDAO.QUERY_MAXBID);<br />         q.setParameter(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">itemid</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, itemId);<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (Bid) q.uniqueResult();<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Bid getMinBid(Long itemId) {<br />         Query q </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> getSession().getNamedQuery(ItemDAO.QUERY_MINBID);<br />         q.setParameter(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">itemid</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, itemId);<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (Bid) q.uniqueResult();<br />     }<br /> <br /> }</span></div> Another example which uses the <tt>findByCriteria()</tt> method of the superclass with variable arguments: <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> CategoryDAOHibernate<br />         </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">     GenericHibernateDAO</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Category, Long</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />         </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);">  CategoryDAO {<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> Collection</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Category</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> findAll(</span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> onlyRootCategories) {<br />         </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> (onlyRootCategories)<br />             </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> findByCriteria( Expression.isNull(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">parent</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">) );<br />         </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />             </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> findAll();<br />     }<br /> }</span></div> <br /> <h2>Preparing DAOs with factories</h2> <p>We could bring it all together in a DAO factory, which not only sets the <tt>Session</tt> when a DAO is constructed but also contains nested classes to implement CRUD-only DAOs with no business-related operations:</p> <br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> HibernateDAOFactory </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> DAOFactory {<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> ItemDAO getItemDAO() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (ItemDAO)instantiateDAO(ItemDAOHibernate.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> CategoryDAO getCategoryDAO() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (CategoryDAO)instantiateDAO(CategoryDAOHibernate.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> CommentDAO getCommentDAO() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (CommentDAO)instantiateDAO(CommentDAOHibernate.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> ShipmentDAO getShipmentDAO() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (ShipmentDAO)instantiateDAO(ShipmentDAOHibernate.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);<br />     }<br /> <br />     </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> GenericHibernateDAO instantiateDAO(Class daoClass) {<br />         </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />             GenericHibernateDAO dao </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (GenericHibernateDAO)daoClass.newInstance();<br />             dao.setSession(getCurrentSession());<br />             </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> dao;<br />         } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception ex) {<br />             </span><span style="color: rgb(0, 0, 255);">throw</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> RuntimeException(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Can not instantiate DAO: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> daoClass, ex);<br />         }<br />     }<br /> <br />     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> You could override this if you don't want HibernateUtil for lookup</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);"> Session getCurrentSession() {<br />         </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> HibernateUtil.getSessionFactory().getCurrentSession();<br />     }<br /> <br />     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Inline concrete DAO implementations with no business-related data access methods.<br />     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> If we use public static nested classes, we can centralize all of them in one source file.</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> CommentDAOHibernate<br />             </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> GenericHibernateDAO</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Comment, Long</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />             </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> CommentDAO {}<br /> <br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> ShipmentDAOHibernate<br />             </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> GenericHibernateDAO</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Shipment, Long</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />             </span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);"> ShipmentDAO {}<br /> <br /> }</span></div> This concrete factory for Hibernate DAOs extends the abstract factory, which is the interface we'll use in application code: <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> DAOFactory {<br /> <br />     </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />      * Creates a standalone DAOFactory that returns unmanaged DAO<br />      * beans for use in any environment Hibernate has been configured<br />      * for. Uses HibernateUtil/SessionFactory and Hibernate context<br />      * propagation (CurrentSessionContext), thread-bound or transaction-bound,<br />      * and transaction scoped.<br />      </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);"> Class HIBERNATE </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> org.hibernate.ce.auction.dao.hibernate.HibernateDAOFactory.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">;<br /> <br />     </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />      * Factory method for instantiation of concrete factories.<br />      </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);"> DAOFactory instance(Class factory) {<br />         </span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br />             </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> (DAOFactory)factory.newInstance();<br />         } </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);"> (Exception ex) {<br />             </span><span style="color: rgb(0, 0, 255);">throw</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> RuntimeException(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Couldn't create DAOFactory: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> factory);<br />         }<br />     }<br /> <br />     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Add your DAO interfaces here</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);"> ItemDAO getItemDAO();<br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);"> CategoryDAO getCategoryDAO();<br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);"> CommentDAO getCommentDAO();<br />     </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);"> ShipmentDAO getShipmentDAO();<br /> <br /> }</span></div> <br /> <p>Note that this factory example is suitable for persistence layers which are primarily implemented with a single persistence service, such as Hibernate or EJB 3.0 persistence. If you have to mix persistence APIs, for example, Hibernate and plain JDBC, the pattern changes slightly. Keep in mind that you can also call <tt>session.connection()</tt> <em>inside</em> a Hibernate-specific DAO, or use one of the many bulk operation/SQL support options in Hibernate 3.1 to avoid plain JDBC.</p> <p>Finally, this is how data access now looks like in controller/command handler code (pick whatever transaction demarcation strategy you like, the DAO code doesn't change):</p> <br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> EJB3 CMT: @TransactionAttribute(TransactionAttributeType.REQUIRED)</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> execute() {<br /> <br />     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> JTA: UserTransaction utx = jndiContext.lookup("UserTransaction");<br />     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> JTA: utx.begin();<br /> <br />     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Plain JDBC: HibernateUtil.getCurrentSession().beginTransaction();</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"><br />     DAOFactory factory </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> DAOFactory.instance(DAOFactory.HIBERNATE);<br />     ItemDAO itemDAO </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> factory.getItemDAO();<br />     UserDAO userDAO </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> factory.getUserDAO();<br /> <br />     Bid currentMaxBid </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> itemDAO.getMaxBid(itemId);<br />     Bid currentMinBid </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> itemDAO.getMinBid(itemId);<br /> <br />     Item item </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> itemDAO.findById(itemId, </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);<br /> <br />     newBid </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> item.placeBid(userDAO.findById(userId, </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">),<br />                             bidAmount,<br />                             currentMaxBid,<br />                             currentMinBid);<br /> <br />     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> JTA: utx.commit(); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Don't forget exception handling<br /> <br />     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Plain JDBC: HibernateUtil.getCurrentSession().getTransaction().commit(); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Don't forget exception handling</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"><br /> }</span></div> The database transaction, either JTA or direct JDBC, is started and committed in an interceptor that runs for every <tt>execute()</tt>, following the <a _base_>Open Session in View</a> pattern. You can use AOP for this or any kind of interceptor that can be wrapped around a method call, see <a _base_>Session handling with AOP</a>. <p>Qhttp://www.hibernate.org<br /> </p> <p><br /> </p> <p><br /> </p> <img src ="http://www.aygfsteel.com/ec2008/aggbug/144529.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/ec2008/" target="_blank">月芽?/a> 2007-09-12 16:22 <a href="http://www.aygfsteel.com/ec2008/archive/2007/09/12/144529.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">Ҧ</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>