﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-神秘的 J2ee 殿堂-随笔分类-J2EE学习摘录</title><link>http://www.blogjava.net/ec2008/category/26384.html</link><description>·古之学者必有师·做学者亦要做师者·FIGHTING·</description><language>zh-cn</language><lastBuildDate>Fri, 25 Apr 2008 14:34:42 GMT</lastBuildDate><pubDate>Fri, 25 Apr 2008 14:34:42 GMT</pubDate><ttl>60</ttl><item><title>连接各种数据库方式速查表</title><link>http://www.blogjava.net/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.blogjava.net/ec2008/archive/2008/04/25/196000.html</guid><wfw:comment>http://www.blogjava.net/ec2008/comments/196000.html</wfw:comment><comments>http://www.blogjava.net/ec2008/archive/2008/04/25/196000.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ec2008/comments/commentRss/196000.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ec2008/services/trackbacks/196000.html</trackback:ping><description><![CDATA[连接各种数据库方式速查表<br />
<br />
下面罗列了各种数据库使用JDBC连接的方式，可以作为一个手册使用。 <br />
<br />
1、Oracle8/8i/9i数据库（thin模式） <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();&nbsp;<br />
String&nbsp;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;">;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">orcl为数据库的SID&nbsp;</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">String&nbsp;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;">;&nbsp;<br />
String&nbsp;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;">;&nbsp;<br />
Connection&nbsp;conn</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;DriverManager.getConnection(url,user,password);&nbsp; <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&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">).newInstance();&nbsp;<br />
String&nbsp;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;">;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">sample为你的数据库名&nbsp;</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">String&nbsp;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;">;&nbsp;<br />
String&nbsp;password</span><span style="color: #000000;">=</span><span style="color: #000000;">""</span><span style="color: #000000;">;&nbsp;<br />
Connection&nbsp;conn</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;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();&nbsp;<br />
String&nbsp;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;">;&nbsp;<br />
</span><span style="color: #008000;">//</span><span style="color: #008000;">mydb为数据库&nbsp;</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">String&nbsp;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;">;&nbsp;<br />
String&nbsp;password</span><span style="color: #000000;">=</span><span style="color: #000000;">""</span><span style="color: #000000;">;&nbsp;<br />
Connection&nbsp;conn</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;DriverManager.getConnection(url,user,password);&nbsp; <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();&nbsp;<br />
String&nbsp;url&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;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;">myDB为你的数据库名&nbsp;</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">Properties&nbsp;sysProps&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;System.getProperties();&nbsp;<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;">);&nbsp;<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;">);&nbsp;<br />
Connection&nbsp;conn</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;DriverManager.getConnection(url,&nbsp;SysProps);&nbsp; <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();&nbsp;<br />
String&nbsp;url&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;&nbsp;</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;">;&nbsp;//myDB为数据库名&nbsp;</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">Connection&nbsp;conn</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;DriverManager.getConnection(url);&nbsp; <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();&nbsp;<br />
String&nbsp;url&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">jdbc:mysql://localhost/myDB?user=soft&amp;password=soft1234&amp;useUnicode=true&amp;characterEncoding=8859_1</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;<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&nbsp;conn</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;DriverManager.getConnection(url);&nbsp; <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();&nbsp;<br />
String&nbsp;url&nbsp;</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;">&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">myDB为数据库名&nbsp;</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">String&nbsp;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;">;&nbsp;<br />
String&nbsp;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;">;&nbsp;<br />
Connection&nbsp;conn</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;DriverManager.getConnection(url,user,password);&nbsp; <br />
</span></div>
8、access数据库直连用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;">)&nbsp;;<br />
String&nbsp;url</span><span style="color: #000000;">=</span><span style="color: #000000;">"</span><span style="color: #000000;">jdbc:odbc:Driver={MicroSoft&nbsp;Access&nbsp;Driver&nbsp;(*.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&nbsp;conn&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;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&nbsp;stmtNew</span><span style="color: #000000;">=</span><span style="color: #000000;">conn.createStatement()&nbsp;; <br />
</span></div>
<div style="text-align: right;"><span style="font-size: 8pt;">《转》</span><br />
<br />
</div>
<img src ="http://www.blogjava.net/ec2008/aggbug/196000.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ec2008/" target="_blank">月芽儿</a> 2008-04-25 16:07 <a href="http://www.blogjava.net/ec2008/archive/2008/04/25/196000.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java基础（持续更新中）</title><link>http://www.blogjava.net/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.blogjava.net/ec2008/archive/2007/11/04/158125.html</guid><wfw:comment>http://www.blogjava.net/ec2008/comments/158125.html</wfw:comment><comments>http://www.blogjava.net/ec2008/archive/2007/11/04/158125.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ec2008/comments/commentRss/158125.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ec2008/services/trackbacks/158125.html</trackback:ping><description><![CDATA[<span style="color: #6494a0;">资料收集于互联网（如有不妥，请告之）、个人心得。</span><br />
<br />
1.默认的浮点类型是双精度（double），要想要一个float必须在浮点数后面加F或者f。如：float pi = 3.14;是错误的<br />
<br />
2.浮点型转化为整型时，不进行四舍五入，直接截断小数点后面的数.Java的类型检查很严格，从低精度转换到高精度是无须显式转换的，double d = 123;。但是反过来，进行窄化转换，由高精度向低精度，或者一种类型到另一种类型，则必须使用强制类型转化。<br />
<br />
3.在数组声明中包含数组长度永远是不合法的！如：int[5] arr; 。因为，声明的时候并没有实例化任何对象，只有在实例化数组对象时，JVM才分配空间，这时才与长度有关。<br />
<br />
4.对象类型的数组虽然被默认初始化了，但是并没有调用其构造函数。也就是说：Car[] myCar = new Car[10];只创建了一个myCar数组对象！并没有创建Car对象的任何实例！这与单纯的new一个对象不同！单个的new对象时，就会调用相应的构造函数。<br />
<br />
5.数组有length属性，字符串有length（）方法<br />
<br />
6.String中的每个字符都是一个16位的Unicode字符，用Unicode很容易表达丰富的国际化字符集，比如很好的中文支持。char中可以保存一个中文汉字，应为char也是一个16位的Unicode字符度为0。<br />
<br />
8.String对象是不变的！可以变化的是String对象的引用。<br />
<br />
9.String的不变性的机制显然会在String常量内有大量的冗余。如："1" + "2" + "3" +......+ "n" 产生了n+(n+1)个String对象！因此Java为了更有效地使用内存，JVM留出一块特殊的内存区域，被称为&#8220;String常量池&#8221;。对 String多么照顾啊！当编译器遇见String常量的时候，它检查该池内是否已经存在相同的String常量。如果找到，就把新常量的引用指向现有的 String，不创建任何新的String常量对象。<br />
<br />
10.java中的对象是在堆中被创建的（new），但常量类的String有点特别！像&#8220;haha&#8221;这样的字符串根据第9条是存放在String常量池的，而String s=new String("haha");确实在堆中被创建的。<br />
<br />
11.throw和throws关键字。throw用在方法体中抛出异常，后面是一个具体的异常对象。throws用在方法参数列表括号的后面，用来声明此方法会抛出的异常种类，后面跟着一个异常类。<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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">try</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">没上</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.setOut(</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;PrintStream(</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;FileOutputStream(</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;File(&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">c:/test.txt&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">))));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">haha&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">catch</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;(Exception&nbsp;&nbsp;&nbsp;e)&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></div>
err的吧setOut改为setErr就可以了。<br />
<br />
<br />
<img src ="http://www.blogjava.net/ec2008/aggbug/158125.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ec2008/" target="_blank">月芽儿</a> 2007-11-04 22:03 <a href="http://www.blogjava.net/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.blogjava.net/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.blogjava.net/ec2008/archive/2007/10/11/152063.html</guid><wfw:comment>http://www.blogjava.net/ec2008/comments/152063.html</wfw:comment><comments>http://www.blogjava.net/ec2008/archive/2007/10/11/152063.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ec2008/comments/commentRss/152063.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ec2008/services/trackbacks/152063.html</trackback:ping><description><![CDATA[一、在web.xml中配置Spring容器的启动&nbsp;（通用的方法）<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;">&lt;</span><span style="color: #000000;">context</span><span style="color: #000000;">-</span><span style="color: #000000;">param</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">param</span><span style="color: #000000;">-</span><span style="color: #000000;">name</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">contextConfigLocation</span><span style="color: #000000;">&lt;/</span><span style="color: #000000;">param</span><span style="color: #000000;">-</span><span style="color: #000000;">name</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">param</span><span style="color: #000000;">-</span><span style="color: #000000;">value</span><span style="color: #000000;">&gt;/</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;">&lt;/</span><span style="color: #000000;">param</span><span style="color: #000000;">-</span><span style="color: #000000;">value</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">如有多个spring配置文件用逗号隔开</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;/</span><span style="color: #000000;">context</span><span style="color: #000000;">-</span><span style="color: #000000;">param</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">&lt;!--</span><span style="color: #000000;">&nbsp;通过Listener配置Spring容器随Web应用的启动而初始化&nbsp;</span><span style="color: #000000;">--&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">listener</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">listener</span><span style="color: #000000;">-</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">org.springframework.web.context.ContextLoaderListener</span><span style="color: #000000;">&lt;/</span><span style="color: #000000;">listener</span><span style="color: #000000;">-</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">&lt;/</span><span style="color: #000000;">listener</span><span style="color: #000000;">&gt;</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;">&lt;</span><span style="color: #000000;">plug</span><span style="color: #000000;">-</span><span style="color: #000000;">in&nbsp;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;">&gt;</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">set</span><span style="color: #000000;">-</span><span style="color: #000000;">property&nbsp;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;">&nbsp;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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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;">&nbsp;/&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">&lt;/</span><span style="color: #000000;">plug</span><span style="color: #000000;">-</span><span style="color: #000000;">in</span><span style="color: #000000;">&gt;</span></div>
&lt;转&gt;<br />
<img src ="http://www.blogjava.net/ec2008/aggbug/152063.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ec2008/" target="_blank">月芽儿</a> 2007-10-11 15:50 <a href="http://www.blogjava.net/ec2008/archive/2007/10/11/152063.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ActionServlet类</title><link>http://www.blogjava.net/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.blogjava.net/ec2008/archive/2007/10/11/151961.html</guid><wfw:comment>http://www.blogjava.net/ec2008/comments/151961.html</wfw:comment><comments>http://www.blogjava.net/ec2008/archive/2007/10/11/151961.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ec2008/comments/commentRss/151961.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ec2008/services/trackbacks/151961.html</trackback:ping><description><![CDATA[&nbsp; &nbsp;
<p class="MsoNormal" style="text-indent: 18pt;"><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">org.apche.struts.action.ActionServlet</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: 宋体;">框架的核心控制器组件，所有用户的请求都先由</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: 宋体;">把请求转发给其他组件。</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: 宋体;">，但是可以将</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: 宋体;">容器在启动时，或者用户首次请求</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: 宋体;">类，在上述情况下，</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: 宋体;">过程如下：</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: 宋体;">方法，初始化</span><span style="font-size: 9pt;" lang="EN-US">struts</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">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: 宋体;">，从</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">ActionServlet</span><span style="font-size: 9pt; font-family: 宋体;">的初始化参数，如</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: 宋体;">文件中加载</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: 宋体;">配置文件所使用的</span><span style="font-size: 9pt;" lang="EN-US">DTD</span><span style="font-size: 9pt; font-family: 宋体;">文件，这些</span><span style="font-size: 9pt;" lang="EN-US">DTD</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">struts</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">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: 宋体;">，加载并解析默认子应用模块的</span><span style="font-size: 9pt;" lang="EN-US">struts</span><span style="font-size: 9pt; font-family: 宋体;">配置文件；创建</span><span style="font-size: 9pt;" lang="EN-US">ModuleConfig</span><span style="font-size: 9pt; font-family: 宋体;">对象，把它存在</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">&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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: 宋体;">，加载并初始化默认子应用模块的消息资源，创建</span><span style="font-size: 9pt;" lang="EN-US"><br />
MessageResources</span><span style="font-size: 9pt; font-family: 宋体;">对象，把它存在</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 />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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: 宋体;">方法，加载并初始化默认子应用模块的数据源。如果没有在</span><span style="font-size: 9pt;" lang="EN-US">struts</span><span style="font-size: 9pt; font-family: 宋体;">配置文件中没有定义</span><span style="font-size: 9pt;" lang="EN-US">&lt;data-sources&gt;</span><span style="font-size: 9pt; font-family: 宋体;">元素，就忽略这一流程</span><span style="font-size: 9pt;" lang="EN-US"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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: 宋体;">加载并初始化默认子应用模块的所有插件</span><span style="font-size: 9pt;" lang="EN-US"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8.</span><span style="font-size: 9pt; font-family: 宋体;">当默认子应用模块被成功初始化后，如果应用还包括其他子应用模块，将重复流程</span><span style="font-size: 9pt;" lang="EN-US">4-7</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"><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;">&nbsp;sample;<br />
<br />
<br />
</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;ExtendedActionServlet&nbsp;</span><span style="color: #0000ff;">extends</span><span style="color: #000000;">&nbsp;ActionServlet&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;init()&nbsp;</span><span style="color: #0000ff;">throws</span><span style="color: #000000;">&nbsp;ServletException&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">super</span><span style="color: #000000;">.init();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">do&nbsp;some&nbsp;operations</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8230;&#8230;&#8230;&#8230;&#8230;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
<br />
<br />
</span></div>
</span></span></span></span></span><span style="font-size: 9pt; font-family: 宋体;">扩展完类后，还应该在</span><span style="font-size: 9pt; font-family: &quot;Times New Roman&quot;;" lang="EN-US">web.xml</span><span style="font-size: 9pt; font-family: 宋体;">文件中如下配置：</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;">&lt;</span><span style="color: #000000;">servlet</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">servlet</span><span style="color: #000000;">-</span><span style="color: #000000;">name</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">sample</span><span style="color: #000000;">&lt;/</span><span style="color: #000000;">servlet</span><span style="color: #000000;">-</span><span style="color: #000000;">name</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> </span><span style="color: #000000;"><br />
&lt;</span><span style="color: #000000;">servlet</span><span style="color: #000000;">-</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">sample.ExtendedActionServlet</span><span style="color: #000000;">&lt;/</span><span style="color: #000000;">servlet</span><span style="color: #000000;">-</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">&lt;/</span><span style="color: #000000;">servlet</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">servlet</span><span style="color: #000000;">-</span><span style="color: #000000;">mapping</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">servlet</span><span style="color: #000000;">-</span><span style="color: #000000;">name</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">sample</span><span style="color: #000000;">&lt;/</span><span style="color: #000000;">servlet</span><span style="color: #000000;">-</span><span style="color: #000000;">name</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">url</span><span style="color: #000000;">-</span><span style="color: #000000;">pattern</span><span style="color: #000000;">&gt;/</span><span style="color: #000000;">action</span><span style="color: #008000;">/*</span><span style="color: #008000;">&lt;url-pattern&gt;<br />
//上面的/action/*表示负责处理所有以/action为前缀的URL,后面的/表示转义<br />
&lt;servlet-mapping&gt;<br />
<br />
</span></div>
<span class="01">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><br />
<span class="01"><span style="font-family: 宋体;"><font size="3"></font></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span class="01"></span><span style="font-size: 9pt; font-family: &quot;Times New Roman&quot;;" lang="EN-US">&lt;</span><span style="font-size: 9pt; font-family: 宋体;">转</span><span style="font-size: 9pt; font-family: &quot;Times New Roman&quot;;" lang="EN-US">&gt;</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.blogjava.net/ec2008/aggbug/151961.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ec2008/" target="_blank">月芽儿</a> 2007-10-11 10:19 <a href="http://www.blogjava.net/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.blogjava.net/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.blogjava.net/ec2008/archive/2007/10/10/151799.html</guid><wfw:comment>http://www.blogjava.net/ec2008/comments/151799.html</wfw:comment><comments>http://www.blogjava.net/ec2008/archive/2007/10/10/151799.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ec2008/comments/commentRss/151799.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ec2008/services/trackbacks/151799.html</trackback:ping><description><![CDATA[当一个复杂的对象被构造时,它的构造函数按下面的顺序被调用(that the order of constructor calls for a complex object is as follows)
<p>&nbsp;&nbsp;&nbsp;
1.其基类(base-class)的构造函数被调用,这个步骤以递归的方式重复,所以最底层(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>&nbsp;&nbsp;&nbsp; 2.如果有包含关系(composition),那么它的成员对象按照声明的顺序被构造.</p>
<p>Member initializers are called in the order of declaration.</p>
<p>&nbsp;&nbsp;&nbsp; 3.派生类构造函数的内容(body)被执行.</p>
<p>The body of the derived-class constructor is called.</p>
<p>&nbsp;&nbsp;&nbsp; 一个实例:</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;">&nbsp;Cake{<br />
&nbsp;&nbsp;&nbsp;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;">&nbsp;Meal&nbsp;{<br />
&nbsp;&nbsp;Meal()&nbsp;{&nbsp;System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">Meal()</span><span style="color: #000000;">"</span><span style="color: #000000;">);&nbsp;}<br />
}<br />
<br />
</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;Bread&nbsp;{<br />
&nbsp;&nbsp;Bread()&nbsp;{&nbsp;System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">Bread()</span><span style="color: #000000;">"</span><span style="color: #000000;">);&nbsp;}<br />
}<br />
<br />
</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;Cheese&nbsp;{<br />
&nbsp;&nbsp;Cheese()&nbsp;{&nbsp;System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">Cheese()</span><span style="color: #000000;">"</span><span style="color: #000000;">);&nbsp;}<br />
}<br />
<br />
</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;Lettuce&nbsp;{<br />
&nbsp;&nbsp;Lettuce()&nbsp;{&nbsp;System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">Lettuce()</span><span style="color: #000000;">"</span><span style="color: #000000;">);&nbsp;}<br />
}<br />
<br />
</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;Lunch&nbsp;</span><span style="color: #0000ff;">extends</span><span style="color: #000000;">&nbsp;Meal&nbsp;{<br />
&nbsp;&nbsp;Lunch()&nbsp;{&nbsp;System.out.println(</span><span style="color: #000000;">"</span><span style="color: #000000;">Lunch()</span><span style="color: #000000;">"</span><span style="color: #000000;">);&nbsp;}<br />
}<br />
<br />
</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;PortableLunch&nbsp;</span><span style="color: #0000ff;">extends</span><span style="color: #000000;">&nbsp;Lunch&nbsp;{<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">if&nbsp;make&nbsp;derived-class&nbsp;object&nbsp;as&nbsp;the&nbsp;menber&nbsp;of&nbsp;the&nbsp;base-class&nbsp;will&nbsp;lead&nbsp;a&nbsp;infinite<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">loop&nbsp;and&nbsp;program&nbsp;will&nbsp;stop&nbsp;because&nbsp;of&nbsp;the&nbsp;memory&nbsp;consumed<br />
&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">private&nbsp;Sandwich&nbsp;s=new&nbsp;Sandwich();&nbsp;</span><span style="color: #008000;"><br />
</span><span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;Cake&nbsp;a</span><span style="color: #000000;">=</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Cake();<br />
&nbsp;&nbsp;PortableLunch()&nbsp;{&nbsp;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;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;Sandwich&nbsp;</span><span style="color: #0000ff;">extends</span><span style="color: #000000;">&nbsp;PortableLunch<br />
{<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;Bread&nbsp;b&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Bread();<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;Cheese&nbsp;c&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Cheese();<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;Lettuce&nbsp;l&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Lettuce();<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;Sandwich()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;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 />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;main(String[]&nbsp;args)&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;Sandwich();<br />
&nbsp;&nbsp;}<br />
}</span></div>
<p>输出:</p>
<p>Meal()<br />
Lunch()<br />
Cake()<br />
PortableLunch()<br />
Bread()<br />
Cheese()<br />
Lettuce()<br />
Sandwich()</p>
<p>&nbsp;&nbsp;
main()函数中要构造一个Sandwich的对象,调用(并不是执行)它基类PortableLunch的构造函数,PortableLunch又递
归的调用,然后是Meal,Meal是继承的最底层的基类(不算Object)所以它的构造函数首先被执行,然后按次序返回到Lunch,
PortableLunch,但在PortableLunch的构造函数被执行之前,它的成员对象Cake
a先按照声明的顺序被构造.然后执行PortableLunch(),接着是Sandwich的成员对象,最后是Sandwich().</p>
<p>注:被注释掉的代码,将base-class的对象作为derive-class的成员对象,这样会递归无法结束,最后程序因堆栈耗尽而结束(Exception in&nbsp; 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.blogjava.net/ec2008/aggbug/151799.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ec2008/" target="_blank">月芽儿</a> 2007-10-10 16:46 <a href="http://www.blogjava.net/ec2008/archive/2007/10/10/151799.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Subversion(SVN)版本控制安装使用指南</title><link>http://www.blogjava.net/ec2008/archive/2007/09/17/145881.html</link><dc:creator>月芽儿</dc:creator><author>月芽儿</author><pubDate>Mon, 17 Sep 2007 07:49:00 GMT</pubDate><guid>http://www.blogjava.net/ec2008/archive/2007/09/17/145881.html</guid><wfw:comment>http://www.blogjava.net/ec2008/comments/145881.html</wfw:comment><comments>http://www.blogjava.net/ec2008/archive/2007/09/17/145881.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ec2008/comments/commentRss/145881.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ec2008/services/trackbacks/145881.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;Subversion是近期在开源社区中非常流行的一款版本控制软件，它是CVS的替代产物。&nbsp;它的主要特征有：&#183;&nbsp;&nbsp;&nbsp; CVS大部分的功能。&#183;&nbsp;&nbsp;&nbsp; 支持&#8220;目录&#8221;级别的版本管理，并且解决了CVS中迟迟未能解决的文件重命名和移动等问题。甚至对文件或文件夹的...&nbsp;&nbsp;<a href='http://www.blogjava.net/ec2008/archive/2007/09/17/145881.html'>阅读全文</a><img src ="http://www.blogjava.net/ec2008/aggbug/145881.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ec2008/" target="_blank">月芽儿</a> 2007-09-17 15:49 <a href="http://www.blogjava.net/ec2008/archive/2007/09/17/145881.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>What is Struts? Intro to a framework for web applications介绍Struts的一篇文章</title><link>http://www.blogjava.net/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.blogjava.net/ec2008/archive/2007/09/12/144656.html</guid><wfw:comment>http://www.blogjava.net/ec2008/comments/144656.html</wfw:comment><comments>http://www.blogjava.net/ec2008/archive/2007/09/12/144656.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ec2008/comments/commentRss/144656.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/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);">&lt;</span><span style="color: rgb(0, 0, 0);">html</span><span style="color: rgb(0, 0, 0);">&gt;&lt;</span><span style="color: rgb(0, 0, 0);">head</span><span style="color: rgb(0, 0, 0);">&gt;&lt;</span><span style="color: rgb(0, 0, 0);">title</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">Important&nbsp;title</span><span style="color: rgb(0, 0, 0);">&lt;/</span><span style="color: rgb(0, 0, 0);">title</span><span style="color: rgb(0, 0, 0);">&gt;&lt;/</span><span style="color: rgb(0, 0, 0);">head</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">body</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">someScript</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
dbConnection&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;openDBConnection(someDB)<br />
resultSet&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;dbConnection.executeQuery(</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">select&nbsp;bookName&nbsp;from&nbsp;books</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">)<br />
loop&nbsp;over&nbsp;resultSet{<br />
&nbsp;print&nbsp;(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);">)&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">&lt;br&gt;</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);">&lt;/</span><span style="color: rgb(0, 0, 0);">someScript</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&lt;/</span><span style="color: rgb(0, 0, 0);">body</span><span style="color: rgb(0, 0, 0);">&gt;&lt;/</span><span style="color: rgb(0, 0, 0);">html</span><span style="color: rgb(0, 0, 0);">&gt;</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);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">html:form&nbsp;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);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;Please&nbsp;give&nbsp;your&nbsp;name&nbsp;and&nbsp;the&nbsp;book&nbsp;title&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">br</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name:<br />
&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">html:text&nbsp;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);">/&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">br</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Title:<br />
&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">html:text&nbsp;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);">/&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">br</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">html:submit</span><span style="color: rgb(0, 0, 0);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;/</span><span style="color: rgb(0, 0, 0);">html:form</span><span style="color: rgb(0, 0, 0);">&gt;</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&nbsp;give&nbsp;your&nbsp;name&nbsp;and&nbsp;the&nbsp;title&nbsp;of&nbsp;the&nbsp;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);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">html:form&nbsp;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);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">bean:message&nbsp;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);">/&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">br</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">bean:message&nbsp;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);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">html:text&nbsp;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);">/&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">br</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">bean:message&nbsp;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);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">html:text&nbsp;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);">/&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">br</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">html:submit</span><span style="color: rgb(0, 0, 0);">/&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;/</span><span style="color: rgb(0, 0, 0);">html:form</span><span style="color: rgb(0, 0, 0);">&gt;</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&#180;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">转自：http://www.laliluna.de<br />
</p>
<img src ="http://www.blogjava.net/ec2008/aggbug/144656.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ec2008/" target="_blank">月芽儿</a> 2007-09-12 23:58 <a href="http://www.blogjava.net/ec2008/archive/2007/09/12/144656.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate对象的三种状态</title><link>http://www.blogjava.net/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.blogjava.net/ec2008/archive/2007/09/12/144572.html</guid><wfw:comment>http://www.blogjava.net/ec2008/comments/144572.html</wfw:comment><comments>http://www.blogjava.net/ec2008/archive/2007/09/12/144572.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ec2008/comments/commentRss/144572.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ec2008/services/trackbacks/144572.html</trackback:ping><description><![CDATA[<p><font style="font-size: 14px;" face="Courier New">Hibernate的对象有3种状态,分别为:瞬时态(Transient)、持久态(Persistent)、脱管态(Detached).处于持久态的对象也称为PO(PersistenceObject),瞬时对象和脱管对象也称为VO(ValueObject).</font></p>
<p><font style="font-size: 14px;" face="Courier New"><strong>瞬时态</strong><br />
由new命令开辟内存空间的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">如果没有变量对该对象进行引用,它将被java虚拟机回收.</font></p>
<p><font style="font-size: 14px;" face="Courier New">瞬时对象在内存孤立存在,它是携带信息的载体,不和数据库的数据有任何关联关系,在Hibernate中,可通过session的save()或saveOrUpdate()方法将瞬时对象与数据库相关联,并将数据对应的插入数据库中,此时该瞬时对象转变成持久化对象.</font></p>
<p><font style="font-size: 14px;" face="Courier New"><strong>持久态</strong><br />
处于该状态的对象在数据库中具有对应的记录,并拥有一个持久化标识.如果是用hibernate的delete()方法,对应的持久对象就变成瞬时对象,因数据库中的对应数据已被删除,该对象不再与数据库的记录关联.</font></p>
<p><font style="font-size: 14px;" face="Courier New">当一个session执行close()或clear()、evict()之后,持久对象变成脱管对象,此时持久对象会变成脱管对象,此时该对象虽然具有数据库识别值,但它已不在HIbernate持久层的管理之下.</font></p>
<p><font style="font-size: 14px;" face="Courier New">持久对象具有如下特点:</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>脱管态<br />
</strong>当与某持久对象关联的session被关闭后,该持久对象转变为脱管对象.当脱管对象被重新关联到session上时,并再次转变成持久对象.</font></p>
<p><font style="font-size: 14px;" face="Courier New">脱管对象拥有数据库的识别值,可通过update()、saveOrUpdate()等方法,转变成持久对象.</font></p>
<p><font style="font-size: 14px;" face="Courier New">脱管对象具有如下特点:</font></p>
<p><font style="font-size: 14px;" face="Courier New">1.本质上与瞬时对象相同,在没有任何变量引用它时,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.blogjava.net/ec2008/aggbug/144572.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ec2008/" target="_blank">月芽儿</a> 2007-09-12 17:58 <a href="http://www.blogjava.net/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.blogjava.net/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.blogjava.net/ec2008/archive/2007/09/12/144529.html</guid><wfw:comment>http://www.blogjava.net/ec2008/comments/144529.html</wfw:comment><comments>http://www.blogjava.net/ec2008/archive/2007/09/12/144529.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ec2008/comments/commentRss/144529.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/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 href="http://caveatemptor.hibernate.org/" _base_href="http://www.hibernate.org/">CaveatEmptor</a> example application. It is also explained in the book <a href="http://www.manning.com/bauer2" _base_href="http://www.hibernate.org/">Java Persistence with Hibernate</a>. Two links you might find useful: <a href="http://www.hibernate.org/42.html" _base_href="http://www.hibernate.org/">Sessions and transactions</a> and <a href="http://www.hibernate.org/43.html" _base_href="http://www.hibernate.org/">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);">&nbsp;</span><span style="color: rgb(0, 0, 255);">interface</span><span style="color: rgb(0, 0, 0);">&nbsp;GenericDAO</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T,&nbsp;ID&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;Serializable</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;findById(ID&nbsp;id,&nbsp;</span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);">&nbsp;lock);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;List</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;findAll();<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;List</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;findByExample(T&nbsp;exampleInstance);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;makePersistent(T&nbsp;entity);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;makeTransient(T&nbsp;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);">&nbsp;</span><span style="color: rgb(0, 0, 255);">interface</span><span style="color: rgb(0, 0, 0);">&nbsp;ItemDAO&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;GenericDAO</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">Item,&nbsp;Long</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);">&nbsp;String&nbsp;QUERY_MAXBID&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</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 />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);">&nbsp;String&nbsp;QUERY_MINBID&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</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 />
&nbsp;&nbsp;&nbsp;&nbsp;Bid&nbsp;getMaxBid(Long&nbsp;itemId);<br />
&nbsp;&nbsp;&nbsp;&nbsp;Bid&nbsp;getMinBid(Long&nbsp;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&amp;file=anchor.gif);" name="A3" _base_href="http://www.hibernate.org/"></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);">&nbsp;</span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;GenericHibernateDAO</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T,&nbsp;ID&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;Serializable</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);">&nbsp;GenericDAO</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T,&nbsp;ID</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;Class</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;persistentClass;<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;Session&nbsp;session;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;GenericHibernateDAO()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.persistentClass&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(Class</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">)&nbsp;((ParameterizedType)&nbsp;getClass()<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getGenericSuperclass()).getActualTypeArguments()[</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;@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 />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;setSession(Session&nbsp;s)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.session&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;s;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);">&nbsp;Session&nbsp;getSession()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(session&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">throw</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;IllegalStateException(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Session&nbsp;has&nbsp;not&nbsp;been&nbsp;set&nbsp;on&nbsp;DAO&nbsp;before&nbsp;usage</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;session;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;Class</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;getPersistentClass()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;persistentClass;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;@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 />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;T&nbsp;findById(ID&nbsp;id,&nbsp;</span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);">&nbsp;lock)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;entity;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(lock)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entity&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(T)&nbsp;getSession().load(getPersistentClass(),&nbsp;id,&nbsp;LockMode.UPGRADE);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entity&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(T)&nbsp;getSession().load(getPersistentClass(),&nbsp;id);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;entity;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;@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 />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;List</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;findAll()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;findByCriteria();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;@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 />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;List</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;findByExample(T&nbsp;exampleInstance,&nbsp;String[]&nbsp;excludeProperty)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Criteria&nbsp;crit&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;getSession().createCriteria(getPersistentClass());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Example&nbsp;example&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;Example.create(exampleInstance);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;(String&nbsp;exclude&nbsp;:&nbsp;excludeProperty)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;example.excludeProperty(exclude);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;crit.add(example);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;crit.list();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;@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 />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;T&nbsp;makePersistent(T&nbsp;entity)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSession().saveOrUpdate(entity);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;entity;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;makeTransient(T&nbsp;entity)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSession().delete(entity);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;flush()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSession().flush();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;clear()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSession().clear();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Use&nbsp;this&nbsp;inside&nbsp;subclasses&nbsp;as&nbsp;a&nbsp;convenience&nbsp;method.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;@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 />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);">&nbsp;List</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;findByCriteria(Criterion<img src="http://www.blogjava.net/Images/dot.gif" alt="" />&nbsp;criterion)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Criteria&nbsp;crit&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;getSession().createCriteria(getPersistentClass());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;(Criterion&nbsp;c&nbsp;:&nbsp;criterion)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;crit.add(c);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;crit.list();<br />
&nbsp;&nbsp;&nbsp;}<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);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;ItemDAOHibernate<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GenericHibernateDAO</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">Item,&nbsp;Long</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;ItemDAO&nbsp;{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;Bid&nbsp;getMaxBid(Long&nbsp;itemId)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Query&nbsp;q&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;getSession().getNamedQuery(ItemDAO.QUERY_MAXBID);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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);">,&nbsp;itemId);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;(Bid)&nbsp;q.uniqueResult();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;Bid&nbsp;getMinBid(Long&nbsp;itemId)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Query&nbsp;q&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;getSession().getNamedQuery(ItemDAO.QUERY_MINBID);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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);">,&nbsp;itemId);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;(Bid)&nbsp;q.uniqueResult();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<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);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;CategoryDAOHibernate<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GenericHibernateDAO</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">Category,&nbsp;Long</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;CategoryDAO&nbsp;{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;Collection</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">Category</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;findAll(</span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);">&nbsp;onlyRootCategories)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(onlyRootCategories)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;findByCriteria(&nbsp;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);">)&nbsp;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;findAll();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<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);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;HibernateDAOFactory&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;DAOFactory&nbsp;{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;ItemDAO&nbsp;getItemDAO()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;(ItemDAO)instantiateDAO(ItemDAOHibernate.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;CategoryDAO&nbsp;getCategoryDAO()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;(CategoryDAO)instantiateDAO(CategoryDAOHibernate.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;CommentDAO&nbsp;getCommentDAO()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;(CommentDAO)instantiateDAO(CommentDAOHibernate.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;ShipmentDAO&nbsp;getShipmentDAO()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;(ShipmentDAO)instantiateDAO(ShipmentDAOHibernate.</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;GenericHibernateDAO&nbsp;instantiateDAO(Class&nbsp;daoClass)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GenericHibernateDAO&nbsp;dao&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(GenericHibernateDAO)daoClass.newInstance();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dao.setSession(getCurrentSession());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;dao;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">&nbsp;(Exception&nbsp;ex)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">throw</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;RuntimeException(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Can&nbsp;not&nbsp;instantiate&nbsp;DAO:&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;daoClass,&nbsp;ex);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;You&nbsp;could&nbsp;override&nbsp;this&nbsp;if&nbsp;you&nbsp;don't&nbsp;want&nbsp;HibernateUtil&nbsp;for&nbsp;lookup</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);">&nbsp;Session&nbsp;getCurrentSession()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;HibernateUtil.getSessionFactory().getCurrentSession();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;Inline&nbsp;concrete&nbsp;DAO&nbsp;implementations&nbsp;with&nbsp;no&nbsp;business-related&nbsp;data&nbsp;access&nbsp;methods.<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;If&nbsp;we&nbsp;use&nbsp;public&nbsp;static&nbsp;nested&nbsp;classes,&nbsp;we&nbsp;can&nbsp;centralize&nbsp;all&nbsp;of&nbsp;them&nbsp;in&nbsp;one&nbsp;source&nbsp;file.</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;CommentDAOHibernate<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;GenericHibernateDAO</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">Comment,&nbsp;Long</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);">&nbsp;CommentDAO&nbsp;{}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;ShipmentDAOHibernate<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;GenericHibernateDAO</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">Shipment,&nbsp;Long</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);">&nbsp;ShipmentDAO&nbsp;{}<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);">&nbsp;</span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;DAOFactory&nbsp;{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Creates&nbsp;a&nbsp;standalone&nbsp;DAOFactory&nbsp;that&nbsp;returns&nbsp;unmanaged&nbsp;DAO<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;beans&nbsp;for&nbsp;use&nbsp;in&nbsp;any&nbsp;environment&nbsp;Hibernate&nbsp;has&nbsp;been&nbsp;configured<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;for.&nbsp;Uses&nbsp;HibernateUtil/SessionFactory&nbsp;and&nbsp;Hibernate&nbsp;context<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;propagation&nbsp;(CurrentSessionContext),&nbsp;thread-bound&nbsp;or&nbsp;transaction-bound,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;and&nbsp;transaction&nbsp;scoped.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">final</span><span style="color: rgb(0, 0, 0);">&nbsp;Class&nbsp;HIBERNATE&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;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 />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Factory&nbsp;method&nbsp;for&nbsp;instantiation&nbsp;of&nbsp;concrete&nbsp;factories.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;DAOFactory&nbsp;instance(Class&nbsp;factory)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);">&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;(DAOFactory)factory.newInstance();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">&nbsp;(Exception&nbsp;ex)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">throw</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;RuntimeException(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Couldn't&nbsp;create&nbsp;DAOFactory:&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;factory);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;Add&nbsp;your&nbsp;DAO&nbsp;interfaces&nbsp;here</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);">&nbsp;ItemDAO&nbsp;getItemDAO();<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);">&nbsp;CategoryDAO&nbsp;getCategoryDAO();<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);">&nbsp;CommentDAO&nbsp;getCommentDAO();<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">abstract</span><span style="color: rgb(0, 0, 0);">&nbsp;ShipmentDAO&nbsp;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);">&nbsp;EJB3&nbsp;CMT:&nbsp;@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);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;execute()&nbsp;{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;JTA:&nbsp;UserTransaction&nbsp;utx&nbsp;=&nbsp;jndiContext.lookup("UserTransaction");<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;JTA:&nbsp;utx.begin();<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;Plain&nbsp;JDBC:&nbsp;HibernateUtil.getCurrentSession().beginTransaction();</span><span style="color: rgb(0, 128, 0);"><br />
</span><span style="color: rgb(0, 0, 0);"><br />
&nbsp;&nbsp;&nbsp;&nbsp;DAOFactory&nbsp;factory&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;DAOFactory.instance(DAOFactory.HIBERNATE);<br />
&nbsp;&nbsp;&nbsp;&nbsp;ItemDAO&nbsp;itemDAO&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;factory.getItemDAO();<br />
&nbsp;&nbsp;&nbsp;&nbsp;UserDAO&nbsp;userDAO&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;factory.getUserDAO();<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;Bid&nbsp;currentMaxBid&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;itemDAO.getMaxBid(itemId);<br />
&nbsp;&nbsp;&nbsp;&nbsp;Bid&nbsp;currentMinBid&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;itemDAO.getMinBid(itemId);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;item&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;itemDAO.findById(itemId,&nbsp;</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;newBid&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;item.placeBid(userDAO.findById(userId,&nbsp;</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bidAmount,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currentMaxBid,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currentMinBid);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;JTA:&nbsp;utx.commit();&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;Don't&nbsp;forget&nbsp;exception&nbsp;handling<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;Plain&nbsp;JDBC:&nbsp;HibernateUtil.getCurrentSession().getTransaction().commit();&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;Don't&nbsp;forget&nbsp;exception&nbsp;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 href="http://www.hibernate.org/43.html" _base_href="http://www.hibernate.org/">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 href="http://www.hibernate.org/391.html" _base_href="http://www.hibernate.org/">Session handling with AOP</a>.
<p>出自：http://www.hibernate.org<br />
</p>
<p><br />
</p>
<p><br />
</p>
<img src ="http://www.blogjava.net/ec2008/aggbug/144529.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ec2008/" target="_blank">月芽儿</a> 2007-09-12 16:22 <a href="http://www.blogjava.net/ec2008/archive/2007/09/12/144529.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>