解析MVC模式 (轉(zhuǎn)載)
摘要: MVC 模式概述 MVC 是三個(gè)單詞的縮寫(xiě) , 分別為: 模型 (Model), 視圖 (View) 和控制 Controller) 。 ... 閱讀全文posted @ 2006-06-12 11:55 nbt 閱讀(430) | 評(píng)論 (0) | 編輯 收藏
posted @ 2006-06-12 11:55 nbt 閱讀(430) | 評(píng)論 (0) | 編輯 收藏
在讀我自己的認(rèn)識(shí)之前
,
我們先來(lái)看一下
servet
的結(jié)構(gòu)圖
:
以下是我自己的一點(diǎn)淺見(jiàn):
① Servlet 在初始化的時(shí)候 , 是通過(guò) init(ServletConfig? config) 或 init() 來(lái)執(zhí)行的。
ServletConfig 是一個(gè)接口,它怎樣傳遞給他一格對(duì)象來(lái)進(jìn)行初始化呢?其實(shí),是這個(gè)對(duì)象是由 servlet 容器來(lái)實(shí)例化的,由容器產(chǎn)生一格 ServletConfig 的實(shí)現(xiàn)類的對(duì)象,然后傳遞給 Servlet
結(jié)論: ServletConfig 由容器實(shí)例化
② 我們有些時(shí)候可能在 Servlet 初始化時(shí)給它一些固定的配置參數(shù),那么這些參數(shù)是怎樣傳遞到 Servlet 呢?
其實(shí),我們?cè)?/span> web.xml 中給 servlet 配置啟動(dòng)參數(shù),在容器對(duì) servlet 進(jìn)行初始化的時(shí)候,會(huì)收集你所配置的參數(shù),記錄在 ServletConfig 的實(shí)現(xiàn)類中,所以你才可以通過(guò) ServletConfig 對(duì)象的
??? public String getInitParameter(String name); 或
??? public Enumeration getInitParameterNames();
方法來(lái)取得你已經(jīng)配置好的參數(shù),也就是說(shuō),你對(duì) servlet 的配置都已經(jīng)記錄在 ServletConfig 對(duì)象中了。
結(jié)論:你對(duì) Servlet 的配置,在 Servlet 的初始化時(shí)都由容器來(lái)收集并且記錄到 ServletConfig 的實(shí)現(xiàn)類中。
?
③ 我們來(lái)看一個(gè) Servlet 的配置
? <servlet>
??? <servlet-name>index</servlet-name>
??? <servlet-class>org.zy.pro.sw.servlet.IndexServlet</servlet-class>
??? <init-param>
????? <param-name>dbconfig</param-name>
????? <param-value>/WEB-INF/dbconfig.xml</param-value>
??? </init-param>
? </servlet>
在此,我們實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的配置文件的加載。
當(dāng) Servlet 初始化完成后,我們可以通過(guò)
String? dbconf=this.getServletConfig().getInitParameter("dbconfig")
來(lái)取得我們的配置的參數(shù)的值。
但是,我們僅能得到一個(gè)配置的字符串。之后我們可以通過(guò)配置文件取得我們的數(shù)據(jù)庫(kù)的配置參數(shù),然后對(duì)數(shù)據(jù)庫(kù)進(jìn)行初始化。
其實(shí)我們也可以通過(guò)傳遞一個(gè)類的名字串,然后再實(shí)例化。
??? <init-param>
????? <param-name>dbconfig</param-name>
????? <param-value>org.zy.util.db.DBUtil</param-value>
??
?</init-param>
我們先取得配置參數(shù):
String? dbconf=this.getServletConfig().getInitParameter("dbconfig") ;
然后通過(guò)
Class.forName(dbconf).getInstance();
來(lái)實(shí)例化對(duì)象,就可以實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的調(diào)用了。
結(jié)論:在 web.xml 中對(duì) Servlet 的初始化,只能傳遞字符串類型的數(shù)據(jù)
④ ServletContext
ServletContext 是負(fù)責(zé)和 Servlet 的上文和下文交互,上面和 Servlet 容器交互,下面和 Servlet 中的請(qǐng)求和相應(yīng)進(jìn)行交互。
在 ServletConfig 中, ???
public ServletContext getServletContext(); 方法實(shí)現(xiàn)取得當(dāng)前 ServletContext 的對(duì)象。
你可能要問(wèn), ServletContext 是一個(gè)接口,那么你如何取得他的對(duì)象呢?
其實(shí)這個(gè)問(wèn)題和 ServletConfig 相同,都是在 Servlet 進(jìn)行初始化的時(shí)候產(chǎn)生的對(duì)象,是由容器來(lái)初始化的。
posted @ 2006-06-12 11:53 nbt 閱讀(314) | 評(píng)論 (0) | 編輯 收藏
cactus.sysproperties=cactus.contextURL
#cactus-sample-servlet-cactified就是你的測(cè)試應(yīng)用所在路徑,8080是端口號(hào)
cactus.contextURL = http://localhost:8080/cactus-sample-servlet-cactified
cactus.servletRedirectorName = ServletRedirector
cactus.jspRedirectorName = JspRedirector
cactus.filterRedirectorName = FilterRedirector
<path id="project.classpath">
????????<fileset dir="${lib.dir}">
?????????? <include name="*.jar"/>
????????</fileset>
????????<!-- cactus.properties文件就需要放在lib.dir所對(duì)應(yīng)的路徑中 -->
????????<pathelement location="${lib.dir}"/>
????????<pathelement location="${tomcat.home}/common/lib/jsp-api.jar"/>
????????<pathelement location="${tomcat.home}/common/lib/servlet-api.jar"/>
????</path>
<taskdef resource="cactus.tasks" classpathref="project.classpath"/>
?? <taskdef name="runservertests" classname="org.apache.cactus.integration.ant.RunServerTestsTask">
????????????<classpath>
????????????????<path refid="project.classpath"/>
????????????</classpath>
????????</taskdef>
<target name="war" depends="compile.java"
????????????description="Generate the runtime war">
????????<war warfile="${target.dir}/${project.name}.war"
???????????? webxml="${src.webapp.dir}/WEB-INF/web.xml">
????????????<fileset dir="${src.webapp.dir}">
????????????????<exclude name="cactus-report.xsl"/>
????????????????<exclude name="WEB-INF/cactus-web.xml"/>
????????????????<exclude name="WEB-INF/web.xml"/>
????????????</fileset>
????????????<classes dir="${target.classes.java.dir}"/>
????????????<!-- 別忘了打包測(cè)試類 -->
????????????<classes dir="${target.classes.test.dir}"/>
????????????<!-- 別忘了打包各種相關(guān)的jar文件 -->
????????????< lib dir="project.classpath"/>
????????</war>
????</target>
<target name="test.prepare"
????????????depends="war, compile.cactus, test.prepare.logging">
????????<!-- Cactify the web-app archive -->
????????<cactifywar srcfile="${target.dir}/${project.name}.war"
????????????????????destfile="${tomcat.home}/webapps/${project.name}-cactified.war"
????????????????>
????????????<classes dir="${target.classes.java.dir}"/>
????????????<classes dir="${target.classes.test.dir}"/>
????????????<lib dir="project.classpath"/>
?????? </cactifywar>
</target>
<target name="test" depends="test.prepare"
???????????? description="Run tests on Tomcat ">
????????<!-- Start the servlet engine, wait for it to be started, run the
???????????? unit tests, stop the servlet engine, wait for it to be stopped.
???????????? The servlet engine is stopped if the tests fail for any reason -->
????????<!-- 8080是服務(wù)器的端口號(hào),${project.name}-cactified是項(xiàng)目的路徑,和上一步的cactifywar 的destfile相對(duì)應(yīng) -->
????????<runservertests
????????????????testURL="http://localhost:8080/${project.name}-cactified/ServletRedirector?Cactus_Service=RUN_TEST"
????????????????startTarget="_StartTomcat"
????????????????stopTarget="_StopTomcat"
????????????????testTarget="_Test"/>
????</target>
<!-- _Test就是一個(gè)普通的junit任務(wù) -->
????<target name="_Test">
????????<junit printsummary="yes" fork="yes">
????????????<classpath>
????????????????<path refid="project.classpath"/>
????????????????<pathelement location="${target.classes.java.dir}"/>
????????????????<pathelement location="${target.classes.test.dir}"/>
????????????</classpath>
????????????<formatter type="brief" usefile="false"/>
????????????<formatter type="xml"/>
????????????<batchtest>
????????????????<fileset dir="${src.test.dir}">
????????????????????<!-- Due to some Cactus synchronization bug, the 'unit' tests need
??????????????to run before the 'sample' tests -->
????????????????????<include name="**/Test*.java"/>
????????????????????<exclude name="**/Test*All.java"/>
????????????????</fileset>
????????????</batchtest>
????????</junit>
????</target>
posted @ 2006-06-12 11:48 nbt 閱讀(232) | 評(píng)論 (0) | 編輯 收藏
posted @ 2006-06-12 09:39 nbt 閱讀(323) | 評(píng)論 (0) | 編輯 收藏
|
Cactus簡(jiǎn)介 . 簡(jiǎn)介 Cactus實(shí)現(xiàn)了對(duì)JUnit測(cè)試框架的無(wú)縫擴(kuò)展,可以方便地測(cè)試服務(wù)端應(yīng)用程序。Cactus可以在下面幾種情況下使用:
Cactus的使用也是非常簡(jiǎn)單的,你寫(xiě)的測(cè)試類只需繼承ServletTestCase或者JspTestCase、FilterTestCase(它們都繼承了JUnit的TestCase)。寫(xiě)好測(cè)試代碼后需要啟動(dòng)web容器,然后執(zhí)行測(cè)試代碼。在下面的章節(jié)中我們將通過(guò)例子向你詳細(xì)講解。 Cactus項(xiàng)目Apache Jakarta Commons的一個(gè)子項(xiàng)目,網(wǎng)址是:http://jakarta.apache.org/commons/cactus/。 . TestCase框架 在Cactus下,我們寫(xiě)的TestCase與JUnit有所不同,先看一段代碼,如下: public class TestSample extendsServletTestCase/JspTestCase/FilterTestCase {public TestSample (String testName) { super(testName); } public void setUp() { } public void tearDown() { } public void beginXXX(WebRequest theRequest) { } public void testXXX() { } public void endXXX(WebResponse theResponse) { } 上面是一個(gè)Cactus測(cè)試類的完整代碼框架,其中的extends部分需要按你所測(cè)試的不同目標(biāo)來(lái)繼承不同的類(簡(jiǎn)介中有所描述)。 另外我們注意到兩個(gè)新的方法beginXXX和endXXX的,這兩個(gè)方法分別會(huì)在testXXX執(zhí)行前和執(zhí)行后執(zhí)行,它們和setUp、tearDown不同的是beginXXX和endXXX會(huì)在相應(yīng)的testXXX前執(zhí)行,而setUp和tearDown則在每個(gè)testXXX方法前都會(huì)執(zhí)行。另外beginXXX和endXXX是客戶端代碼,所以在這兩個(gè)方法里是無(wú)法使用request這樣的服務(wù)端對(duì)象的。 對(duì)于endXXX方法需要另加說(shuō)明的是,在Cactus v1.1前(包括v1.1),它的形式是這樣的public void endXXX(HttpURLConnection theConnection),而在Cactus v1.2開(kāi)始它的形式有兩種可能:
可以看到區(qū)別在于引用的包不同,為什么會(huì)這樣的呢?因?yàn)樵趘1.2開(kāi)始Cactus集成了HttpUnit這個(gè)組件。如果你熟悉HttpUnit這個(gè)組件,我想應(yīng)該明白為什么要集成HttpUnit。下面我們來(lái)看一段代碼開(kāi)比較一下兩者的區(qū)別: public void endXXX(org.apache.cactus.WebResponse theResponse) { String content = theResponse.getText(); assertEquals(content, "<html><body><h1>Hello world!</h1></body></html>"); } public void endXXX(com.meterware.httpunit.WebResponse theResponse) { WebTable table = theResponse.getTables()[0]; assertEquals("rows", 4, table.getRowCount()); assertEquals("columns", 3, table.getColumnCount()); assertEquals("links", 1, table.getTableCell(0, 2).getLinks().length); } 當(dāng)然,在實(shí)際應(yīng)用中你需要根據(jù)不同的需要來(lái)選擇不同的endXXX。兩個(gè)WebResponse的差別可以參見(jiàn)兩者各自的API Doc,這里就不再多說(shuō)了。 如何在Cactus里寫(xiě)測(cè)試 . 寫(xiě)測(cè)試代碼 首先,我們給出被測(cè)類的代碼,是一個(gè)Servlet: public class SampleServlet extends HttpServlet { public void doGet(HttpServletRequest theRequest, HttpServletResponse theResponse) throws IOException { PrintWriter pw = theResponse.getWriter(); theResponse.setContentType("text/html"); pw.print("<html><head/><body>"); pw.print("A GET request"); pw.print("</body></html>"); } public String checkMethod(HttpServletRequest theRequest) { return theRequest.getMethod(); } } Cactus中的測(cè)試類框架已經(jīng)在上面給出。下面來(lái)看一下例子,例子是從中Cactus自帶的實(shí)例中抽取的一部分,如下: public class TestSampleServlet extends ServletTestCase { public void testReadServletOutputStream() throws IOException { SampleServlet servlet = new SampleServlet(); servlet.doGet(request, response); } public void endReadServletOutputStream(WebResponse theResponse) throws IOException { String expected = "<html><head/><body>A GET request</body></html>"; String result = theResponse.getText(); assertEquals(expected, result); } public void beginPostMethod(WebRequest theRequest) { theRequest.addParameter("param", "value", WebRequest.POST_METHOD); } public void testPostMethod() { SampleServlet servlet = new SampleServlet(); assertEquals("POST", servlet.checkMethod(request)); assertEquals("value", request.getParameter("param")); } } 第一個(gè)方法testReadServletOutputStream,調(diào)用doGet,相當(dāng)于在客戶端提交請(qǐng)求,然后在Servlet處理后會(huì)產(chǎn)生一個(gè)回饋,所以,在endReadServletOutputStream方法里,我們通過(guò)調(diào)用response的相應(yīng)方法判斷回饋是否符合預(yù)期結(jié)果。 第二個(gè)方法testPostMethod,在這之前有一個(gè)beginPostMethod,在這個(gè)方法里我們以POST方式往request里增加一個(gè)表單數(shù)據(jù)param,值為”value”。下面在testPostMethod我們就要驗(yàn)證表單數(shù)據(jù)是否以POST方式提交到了服務(wù)端的Servlet里,所以,我們看到了兩個(gè)assertEquals,分別進(jìn)行了判斷。在這里我們要注意到beginPostMethod方法中的theRequest和testPostMethod中的request的區(qū)別,在前面我們已經(jīng)提到過(guò),beginPostMethod是在客戶端執(zhí)行的,所以它方法內(nèi)的所有操作事實(shí)上是模擬頁(yè)面操作的,比如上面的設(shè)置表單數(shù)據(jù),而testPostMethod是服務(wù)端執(zhí)行的,其中的request也是服務(wù)端的。 配置cactus.properties和web.xmlcactus.properties
這個(gè)屬性是必須的,它指定了web應(yīng)用的訪問(wèn)地址 例:cactus.contextURL = http://localhost:8080/test
可選,當(dāng)測(cè)試類繼承ServletTestCase時(shí)用于指定Cactus Servlet Redirector的映射名稱。默認(rèn):ServletRedirector 例:cactus.servletRedirectorName = ServletRedirector
可選,當(dāng)測(cè)試類繼承ServletTestCase時(shí)用于指定Cactus Jsp Redirector的映射名稱。默認(rèn):ServletRedirector 例:cactus.jspRedirectorName = JspRedirector
可選,當(dāng)測(cè)試類繼承ServletTestCase時(shí)用于指定Cactus Filter Redirector的映射名稱。默認(rèn):ServletRedirector 例:cactus.filterRedirectorName = FilterRedirector Cactus.properties你可以放置在WEB-INF/classes/下。 web.xml 在web.xml里要為相應(yīng)的測(cè)試類指定相應(yīng)的Cactus Redirector。 ServletTestCase對(duì)應(yīng)org.apache.cactus.server.ServletTestRedirector JspTestCase對(duì)應(yīng)/jspRedirector.jsp FilterTestCase對(duì)應(yīng)org.apache.cactus.server.FilterTestRedirector <web-app> <filter> <filter-name>FilterRedirector</filter-name> <filter-class>org.apache.cactus.server.FilterTestRedirector</filter-class> </filter> <filter-mapping> <filter-name>FilterRedirector</filter-name> <url-pattern>/FilterRedirector</url-pattern> </filter-mapping> <servlet> <servlet-name>ServletRedirector</servlet-name> <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class> </servlet> <servlet> <servlet-name>JspRedirector</servlet-name> <jsp-file>/jspRedirector.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>ServletRedirector</servlet-name> <url-pattern>/ServletRedirector</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>JspRedirector</servlet-name> <url-pattern>/JspRedirector</url-pattern> </servlet-mapping> </web-app> 如果你的測(cè)試類繼承了JspTestCase則需要將jspRedirector.jsp文件放置到你在web.xml中指定的路徑里。 安裝說(shuō)明
如下:
junit.jar servlet.jar cactus.jar httpclient.jar commons-logging.jar httpunit.jar,Tidy.jar,xerces.jar(可選,如果你集成了httpunit的話就需要,也就是在endXXX中使用了httpunit)
cactus.jar junit.jar aspectjrt.jar commons-logging.jar
|
posted @ 2006-06-12 09:34 nbt 閱讀(276) | 評(píng)論 (0) | 編輯 收藏
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl為數(shù)據(jù)庫(kù)的SID String user="test"; String password="test"; Connection conn= DriverManager.getConnection(url,user,password); |
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance(); String url="jdbc:db2://localhost:5000/sample"; //sample為你的數(shù)據(jù)庫(kù)名 String user="admin"; String password=""; Connection conn= DriverManager.getConnection(url,user,password); |
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb"; //mydb為數(shù)據(jù)庫(kù) String user="sa"; String password=""; Connection conn= DriverManager.getConnection(url,user,password); |
Class.forName("com.sybase.jdbc.SybDriver").newInstance(); String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB為你的數(shù)據(jù)庫(kù)名 Properties sysProps = System.getProperties(); SysProps.put("user","userid"); SysProps.put("password","user_password"); Connection conn= DriverManager.getConnection(url, SysProps); |
Class.forName("com.informix.jdbc.IfxDriver").newInstance(); String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver; user=testuser;password=testpassword"; //myDB為數(shù)據(jù)庫(kù)名 Connection conn= DriverManager.getConnection(url); |
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1" //myDB為數(shù)據(jù)庫(kù)名 Connection conn= DriverManager.getConnection(url); |
Class.forName("org.postgresql.Driver").newInstance(); String url ="jdbc:postgresql://localhost/myDB" //myDB為數(shù)據(jù)庫(kù)名 String user="myuser"; String password="mypassword"; Connection conn= DriverManager.getConnection(url,user,password); |
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb"); Connection conn = DriverManager.getConnection(url,"",""); Statement stmtNew=conn.createStatement() ; |
try{ Class.forName(com.mysql.jdbc.Driver); System.out.println(Success loading Mysql Driver!); }catch(Exception e) { System.out.println(Error loading Mysql Driver!); e.printStackTrace(); } |
jdbc:mysql://localhost/databasename[?pa=va][&pa=va] |
PreparedStatement pstmt3D null; try { ((OraclePreparedStatement)pstmt).setExecuteBatch(30); ... pstmt.executeUpdate(); } |
posted @ 2006-06-12 09:11 nbt 閱讀(332) | 評(píng)論 (0) | 編輯 收藏
<?xml version="1.0" encoding="utf-8"?>
<project name="利用工具開(kāi)發(fā)Hibernate" default="help" basedir=".">
?<!-- ******? 環(huán)境設(shè)置,可以根據(jù)自己的實(shí)際配置自行更改 ***** -->
?<!-- ******? http://blog.csdn.net/fasttalk??? ***** -->
?<!-- ******? http://www.aygfsteel.com/asktalk? ***** -->
?<!-- 源文件目錄, 可以通過(guò) 項(xiàng)目->屬性->Java構(gòu)建路徑 更改 -->
?<property name="src.dir" value="./src" />
?<!-- 輸出的class文件目錄,可以通過(guò) 項(xiàng)目->屬性->Java構(gòu)建路徑 更改 -->
?<property name="class.dir" value="./bin" />
?<!-- 庫(kù)文件目錄? -->
?<property name="lib.dir" value="E:/workspace/java/hibernate3" />
?<!-- 定義類路徑 -->
?<path id="project.class.path">
??<fileset dir="${lib.dir}">
???<include name="*.jar"/>
??</fileset>
??<pathelement location="${class.dir}" />
?</path>
?<!-- ************************************************************** -->
?<!-- 使用說(shuō)明 -->
?<!-- ************************************************************** -->
?<target name="help">
??<echo message="利用工具開(kāi)發(fā)Hibernate" />
??<echo message="-----------------------------------" />
??<echo message="" />
??<echo message="提供以下任務(wù):" />
??<echo message="" />
??<echo message="generate-hbm???? --> 運(yùn)行HibernateDoclet,生成 Hibernate 類的映射文件" />
??<echo message="schemaexport???? --> 運(yùn)行SchemaExport,利用 hbm.xml 文件生成數(shù)據(jù)表" />
??<echo message="" />
?</target>
?<!-- ************************************************************** -->
?<!-- Hbm2Java 任務(wù) 在hibernate3中無(wú)法實(shí)現(xiàn) -->
?<!-- ************************************************************** -->
?<target name="generate-code" >
??<echo message="運(yùn)行 Hbm2Java 任務(wù), 利用 hbm.xml 文件生成Java類文件"/>
??<taskdef name="hbm2java"
?????? classname="org.hibernate.tool.instrument.InstrumentTask"
????????? classpathref="project.class.path">
??</taskdef>
??<hbm2java output="${src.dir}">
???<fileset dir="${src.dir}">
????<include name="**/*.hbm.xml"/>
???</fileset>
??</hbm2java>
?</target>
?<!-- ************************************************************** -->
?<!-- HibernateDoclet 任務(wù) -->
?<!-- ************************************************************** -->
?<target name="generate-hbm" >
??<echo message="運(yùn)行HibernateDoclet,生成 Hibernate 類的映射文件"/>
??<taskdef name="hibernatedoclet"
???classname="xdoclet.modules.hibernate.HibernateDocletTask"
???classpathref="project.class.path">
??</taskdef>
??????? <!--
??????? destdir???????? 輸出目錄;
??????? force,????????? 每次都強(qiáng)行執(zhí)行,覆蓋原有文件;
??????? -->
??<hibernatedoclet destdir="${src.dir}"
???excludedtags="@version,@author,@todo" force="true" encoding="GBK"
???verbose="true">
???<fileset dir="${src.dir}">
????<include name="**/*.java"/>
???</fileset>
???<hibernate version="3.0" xmlencoding="utf-8" />
??</hibernatedoclet>
?</target>
?<!-- ************************************************************** -->
?<!-- SchemaExport 任務(wù) -->
?<!-- ************************************************************** -->
?<target name="schemaexport">
??<echo message="運(yùn)行SchemaExport,利用 hbm.xml 文件生成數(shù)據(jù)表"/>
??<taskdef name="schemaexport"
???classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
???classpathref="project.class.path">
??</taskdef>
????? <!--
????? quiet=true?????????????????????? 不要把腳本輸出到stdout;
????? drop=true??????????????????????? 只進(jìn)行drop tables的步驟 ;
????? text=true??????????????????????? 不執(zhí)行在數(shù)據(jù)庫(kù)中運(yùn)行的步驟 ;
????? output=my_schema.ddl???????????? 把輸出的ddl腳本輸出到一個(gè)文件 ;
????? config=hibernate.cfg.xml???????? 從XML文件讀入Hibernate配置 ;
????? properties=hibernate.properties? 從文件讀入數(shù)據(jù)庫(kù)屬性 ;
????? format=true????????????????????? 把腳本中的SQL語(yǔ)句對(duì)齊和美化 ;
????? delimiter=x????????????????????? 為腳本設(shè)置行結(jié)束符
????? -->
??<schemaexport properties="src/hibernate.properties"
? quiet="no"?text="no" drop="no"? output="schema-export.sql" >
???????? <fileset dir="${src.dir}">
???????????? <include name="**/*.hbm.xml"/>?
???????? </fileset>
??</schemaexport>
?</target>
</project>
posted @ 2006-06-12 08:59 nbt 閱讀(477) | 評(píng)論 (0) | 編輯 收藏
?? Ajax目前是社區(qū)內(nèi)最熱門的話題之一了,最近在我們的項(xiàng)目中用了大量的Ajax,現(xiàn)在把我們的使用方法在這兒寫(xiě)出來(lái),希望大家能指教。
因?yàn)橐玫紸jax就肯定要用到XMLHttpRequest對(duì)象,但由于不同的瀏覽器版本,相應(yīng)的生成它的方法也有所不同,所以我們不得不對(duì)瀏覽器的版本進(jìn)行判斷,試想,如果我們要在很多地方都要寫(xiě)那些繁瑣的代碼會(huì)覺(jué)的很麻煩,代碼的重用也很低,所以我們寫(xiě)一個(gè)Ajax的對(duì)象。代碼如下:
//*********************************************************
// 目的:??? AJAX類
// 輸入:??? 無(wú)
// 返回:??? 返回XMLHttp對(duì)象
// 例子:??? var myConn = new XHConn();
//
//?????????? if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");
//
//?????????? var fnWhenDone = function (oXML) { alert(oXML.responseText); };
//
//?????????? myConn.connect("mypage.php", "POST", "foo=bar&baz=qux", fnWhenDone);
//
//*********************************************************
function XHConn()
{
? var xmlhttp = false, bComplete = false;
? try
? {
? ?xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
? }
? catch (e)
? {
? ?try
? ?{
? ??xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
? ?}
??? catch (e)
??? {
? ??try
? ??{
? ???xmlhttp = new XMLHttpRequest();
? ??}
? ??catch (e)
? ??{
? ???xmlhttp = false;
? ??}
? ?}
? }
? if (!xmlhttp) return null;
? this.connect = function(sURL, sMethod, sVars, fnDone)
? {
??? if (!xmlhttp) return false;
??? bComplete = false;
??? sVars = (sVars == '') ? Math.random() : sVars + "&" + Math.random();
??? sMethod = sMethod.toUpperCase();
??? try
??? {
????? if (sMethod == "GET")
????? {
??????? xmlhttp.open(sMethod, sURL+"?"+sVars, true);
??????? xmlhttp.setRequestHeader("Content-Type", "text/html;charset=GB2312");
??????? sVars = "";
????? }
????? else
????? {
??????? xmlhttp.open(sMethod, sURL, true);
??????? xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
??????? xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
????? }
????? xmlhttp.onreadystatechange = function()
????? {
??????? if (xmlhttp.readyState == 4 && !bComplete)
??????? {
????????? bComplete = true;
????????? fnDone(xmlhttp);
??????? }
????? };
?????
????? xmlhttp.send(sVars);
??? }
??? catch(z)
??? {
??? ?return false;
??? }
??? return true;
? };
?
? return this;
}
通過(guò)這個(gè)對(duì)象,我們把那些繁瑣的代碼都封裝到里面,這樣大大提高了代碼的重用性,每次要用Ajax時(shí)我們只需要在我們的頁(yè)面上 new一個(gè)XHConn()對(duì)象就行了,然后通過(guò)調(diào)用它的方法connect(sURL, sMethod, sVars, fnDone)就可以和服務(wù)器進(jìn)行異步交互了。
posted @ 2006-06-09 16:50 nbt 閱讀(296) | 評(píng)論 (0) | 編輯 收藏
Java虛擬機(jī)(JVM)是可運(yùn)行Java代碼的假想計(jì)算機(jī)。只要根據(jù)JVM規(guī)格描述將解釋器移植到特定的計(jì)算機(jī)上,就能保證經(jīng)過(guò)編譯的任何Java代碼能夠在該系統(tǒng)上運(yùn)行。本文首先簡(jiǎn)要介紹從Java文件的編譯到最終執(zhí)行的過(guò)程,隨后對(duì)JVM規(guī)格描述作一說(shuō)明。
一.Java源文件的編譯、下載、解釋和執(zhí)行 二.JVM規(guī)格描述 對(duì)比分析:如果把Java原程序想象成我們的C++原程序,Java原程序編譯后生成的字節(jié)碼就相當(dāng)于C++原程序編譯后的80x86的機(jī)器碼(二進(jìn)制程序文件),JVM虛擬機(jī)相當(dāng)于80x86計(jì)算機(jī)系統(tǒng),Java解釋器相當(dāng)于80x86CPU。在80x86CPU上運(yùn)行的是機(jī)器碼,在Java解釋器上運(yùn)行的是Java字節(jié)碼。 |
posted @ 2006-06-09 16:25 nbt 閱讀(228) | 評(píng)論 (0) | 編輯 收藏
Java 語(yǔ)言的Calendar(日歷),Date(日期), 和DateFormat(日期格式)組成了Java標(biāo)準(zhǔn)的一個(gè)基本但是非常重要的部分. 日期是商業(yè)邏輯計(jì)算一個(gè)關(guān)鍵的部分. 所有的開(kāi)發(fā)者都應(yīng)該能夠計(jì)算未來(lái)的日期, 定制日期的顯示格式, 并將文本數(shù)據(jù)解析成日期對(duì)象. 我們寫(xiě)了兩篇文章, 這是第一篇, 我們將大概的學(xué)習(xí)日期, 日期格式, 日期的解析和日期的計(jì)算.
我們將討論下面的類:
1、具體類(和抽象類相對(duì))java.util.Date
2、抽象類java.text.DateFormat 和它的一個(gè)具體子類,java.text.SimpleDateFormat
3、抽象類java.util.Calendar 和它的一個(gè)具體子類,java.util.GregorianCalendar
具體類可以被實(shí)例化, 但是抽象類卻不能. 你首先必須實(shí)現(xiàn)抽象類的一個(gè)具體子類.
Date 類從Java 開(kāi)發(fā)包(JDK) 1.0 就開(kāi)始進(jìn)化, 當(dāng)時(shí)它只包含了幾個(gè)取得或者設(shè)置一個(gè)日期數(shù)據(jù)的各個(gè)部分的方法, 比如說(shuō)月, 日, 和年. 這些方法現(xiàn)在遭到了批評(píng)并且已經(jīng)被轉(zhuǎn)移到了Calendar類里去了, 我們將在本文中進(jìn)一步討論它. 這種改進(jìn)旨在更好的處理日期數(shù)據(jù)的國(guó)際化格式. 就象在JDK 1.1中一樣, Date 類實(shí)際上只是一個(gè)包裹類, 它包含的是一個(gè)長(zhǎng)整型數(shù)據(jù), 表示的是從GMT(格林尼治標(biāo)準(zhǔn)時(shí)間)1970年, 1 月 1日00:00:00這一刻之前或者是之后經(jīng)歷的毫秒數(shù).
一、創(chuàng)建一個(gè)日期對(duì)象
讓我們看一個(gè)使用系統(tǒng)的當(dāng)前日期和時(shí)間創(chuàng)建一個(gè)日期對(duì)象并返回一個(gè)長(zhǎng)整數(shù)的簡(jiǎn)單例子. 這個(gè)時(shí)間通常被稱為Java 虛擬機(jī)(JVM)主機(jī)環(huán)境的系統(tǒng)時(shí)間.
//------------------------------------------------------
import java.util.Date;
public class DateExample1
{
public static void main(String[] args)
{
// Get the system date/time
Date date = new Date();
System.out.println(date.getTime());
}
}
//------------------------------------------------------
在星期六, 2001年9月29日, 下午大約是6:50的樣子, 上面的例子在系統(tǒng)輸出設(shè)備上顯示的結(jié)果是 1001803809710. 在這個(gè)例子中,值得注意的是我們使用了Date 構(gòu)造函數(shù)創(chuàng)建一個(gè)日期對(duì)象, 這個(gè)構(gòu)造函數(shù)沒(méi)有接受任何參數(shù). 而這個(gè)構(gòu)造函數(shù)在內(nèi)部使用了System.currentTimeMillis() 方法來(lái)從系統(tǒng)獲取日期.
那么, 現(xiàn)在我們已經(jīng)知道了如何獲取從1970年1月1日開(kāi)始經(jīng)歷的毫秒數(shù)了. 我們?nèi)绾尾拍芤砸环N用戶明白的格式來(lái)顯示這個(gè)日期呢? 在這里類java.text.SimpleDateFormat 和它的抽象基類 java.text.DateFormat 就派得上用場(chǎng)了.
二、日期數(shù)據(jù)的定制格式
假如我們希望定制日期數(shù)據(jù)的格式, 比方星期六-9月-29日-2001年. 下面的例子展示了如何完成這個(gè)工作:
//------------------------------------------------------
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateExample2
{
public static void main(String[] args)
{
SimpleDateFormat bartDateFormat =
new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
Date date = new Date();
System.out.println(bartDateFormat.format(date));
}
}
//------------------------------------------------------
只要通過(guò)向SimpleDateFormat 的構(gòu)造函數(shù)傳遞格式字符串"EEE-MMMM-dd-yyyy", 我們就能夠指明自己想要的格式. 你應(yīng)該可以看見(jiàn), 格式字符串中的ASCII 字符告訴格式化函數(shù)下面顯示日期數(shù)據(jù)的哪一個(gè)部分. EEEE是星期, MMMM是月, dd是日, yyyy是年. 字符的個(gè)數(shù)決定了日期是如何格式化的.傳遞"EE-MM-dd-yy"會(huì)顯示 Sat-09-29-01. 請(qǐng)察看Sun 公司的Web 站點(diǎn)獲取日期格式化選項(xiàng)的完整的指示.
三、將文本數(shù)據(jù)解析成日期對(duì)象
假設(shè)我們有一個(gè)文本字符串包含了一個(gè)格式化了的日期對(duì)象, 而我們希望解析這個(gè)字符串并從文本日期數(shù)據(jù)創(chuàng)建一個(gè)日期對(duì)象. 我們將再次以格式化字符串"MM-dd-yyyy" 調(diào)用SimpleDateFormat類, 但是這一次, 我們使用格式化解析而不是生成一個(gè)文本日期數(shù)據(jù). 我們的例子, 顯示在下面, 將解析文本字符串"9-29-2001"并創(chuàng)建一個(gè)值為001736000000 的日期對(duì)象.
//------------------------------------------------------
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateExample3
{
public static void main(String[] args)
{
// Create a date formatter that can parse dates of
// the form MM-dd-yyyy.
SimpleDateFormat bartDateFormat =
new SimpleDateFormat("MM-dd-yyyy");
// Create a string containing a text date to be parsed.
String dateStringToParse = "9-29-2001";
try {
// Parse the text version of the date.
// We have to perform the parse method in a
// try-catch construct in case dateStringToParse
// does not contain a date in the format we are expecting.
Date date = bartDateFormat.parse(dateStringToParse);
// Now send the parsed date as a long value
// to the system output.
System.out.println(date.getTime());
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
//------------------------------------------------------
四、使用標(biāo)準(zhǔn)的日期格式化過(guò)程
既然我們已經(jīng)可以生成和解析定制的日期格式了, 讓我們來(lái)看一看如何使用內(nèi)建的格式化過(guò)程. 方法 DateFormat.getDateTimeInstance() 讓我們得以用幾種不同的方法獲得標(biāo)準(zhǔn)的日期格式化過(guò)程. 在下面的例子中, 我們獲取了四個(gè)內(nèi)建的日期格式化過(guò)程. 它們包括一個(gè)短的, 中等的, 長(zhǎng)的, 和完整的日期格式.
//------------------------------------------------------
import java.text.DateFormat;
import java.util.Date;
public class DateExample4
{
public static void main(String[] args)
{
Date date = new Date();
DateFormat shortDateFormat =
DateFormat.getDateTimeInstance(
DateFormat.SHORT,
DateFormat.SHORT);
DateFormat mediumDateFormat =
DateFormat.getDateTimeInstance(
DateFormat.MEDIUM,
DateFormat.MEDIUM);
DateFormat longDateFormat =
DateFormat.getDateTimeInstance(
DateFormat.LONG,
DateFormat.LONG);
DateFormat fullDateFormat =
DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.FULL);
System.out.println(shortDateFormat.format(date));
System.out.println(mediumDateFormat.format(date));
System.out.println(longDateFormat.format(date));
System.out.println(fullDateFormat.format(date));
}
}
//------------------------------------------------------
注意我們?cè)趯?duì) getDateTimeInstance的每次調(diào)用中都傳遞了兩個(gè)值. 第一個(gè)參數(shù)是日期風(fēng)格, 而第二個(gè)參數(shù)是時(shí)間風(fēng)格. 它們都是基本數(shù)據(jù)類型int(整型). 考慮到可讀性, 我們使用了DateFormat 類提供的常量: SHORT, MEDIUM, LONG, 和 FULL. 要知道獲取時(shí)間和日期格式化過(guò)程的更多的方法和選項(xiàng), 請(qǐng)看Sun 公司W(wǎng)eb 站點(diǎn)上的解釋.
運(yùn)行我們的例子程序的時(shí)候, 它將向標(biāo)準(zhǔn)輸出設(shè)備輸出下面的內(nèi)容:
9/29/01 8:44 PM
Sep 29, 2001 8:44:45 PM
September 29, 2001 8:44:45 PM EDT
Saturday, September 29, 2001 8:44:45 PM EDT
五、Calendar 類
我們現(xiàn)在已經(jīng)能夠格式化并創(chuàng)建一個(gè)日期對(duì)象了, 但是我們?nèi)绾尾拍茉O(shè)置和獲取日期數(shù)據(jù)的特定部分呢, 比如說(shuō)小時(shí), 日, 或者分鐘? 我們又如何在日期的這些部分加上或者減去值呢? 答案是使用Calendar 類. 就如我們前面提到的那樣, Calendar 類中的方法替代了Date 類中被人唾罵的方法.
假設(shè)你想要設(shè)置, 獲取, 和操縱一個(gè)日期對(duì)象的各個(gè)部分, 比方一個(gè)月的一天或者是一個(gè)星期的一天. 為了演示這個(gè)過(guò)程, 我們將使用具體的子類 java.util.GregorianCalendar. 考慮下面的例子, 它計(jì)算得到下面的第十個(gè)星期五是13號(hào).
//------------------------------------------------------
import java.util.GregorianCalendar;
import java.util.Date;
import java.text.DateFormat;
public class DateExample5
{
public static void main(String[] args)
{
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
// Create our Gregorian Calendar.
GregorianCalendar cal = new GregorianCalendar();
// Set the date and time of our calendar
// to the system&s date and time
cal.setTime(new Date());
System.out.println("System Date: " +
dateFormat.format(cal.getTime()));
System.out.println("Befor Setting Day of Week to Friday: " +
dateFormat.format(cal.getTime())); ?
// Set the day of week to FRIDAY
cal.set(GregorianCalendar.DAY_OF_WEEK,
GregorianCalendar.FRIDAY);
System.out.println("After Setting Day of Week to Friday: " +
dateFormat.format(cal.getTime()));
int friday13Counter = 0;
while (friday13Counter <= 10)
{
// Go to the next Friday by adding 7 days.
cal.add(GregorianCalendar.DAY_OF_MONTH, 7);
// If the day of month is 13 we have
// another Friday the 13th.
if (cal.get(GregorianCalendar.DAY_OF_MONTH) == 13)
{
friday13Counter++;
System.out.println(dateFormat.format(cal.getTime()));
}
}
}
}
//------------------------------------------------------
在這個(gè)例子中我們作了有趣的函數(shù)調(diào)用:
cal.set(GregorianCalendar.DAY_OF_WEEK,
GregorianCalendar.FRIDAY);
和:
cal.add(GregorianCalendar.DAY_OF_MONTH, 7);
set 方法能夠讓我們通過(guò)簡(jiǎn)單的設(shè)置星期中的哪一天這個(gè)域來(lái)將我們的時(shí)間調(diào)整為星期五. 注意到這里我們使用了常量 DAY_OF_WEEK 和 FRIDAY來(lái)增強(qiáng)代碼的可讀性. add 方法讓我們能夠在日期上加上數(shù)值. 潤(rùn)年的所有復(fù)雜的計(jì)算都由這個(gè)方法自動(dòng)處理.
我們這個(gè)例子的輸出結(jié)果是:
System Date: Saturday, September 29, 2001
當(dāng)我們將它設(shè)置成星期五以后就成了: Friday, September 28, 2001
Friday, September 13, 2002
Friday, December 13, 2002
Friday, June 13, 2003
Friday, February 13, 2004
Friday, August 13, 2004
Friday, May 13, 2005
Friday, January 13, 2006
Friday, October 13, 2006
Friday, April 13, 2007
Friday, July 13, 2007
Friday, June 13, 2008
六、時(shí)間掌握在你的手里
有了這些Date 和Calendar 類的例子, 你應(yīng)該能夠使用 java.util.Date, java.text.SimpleDateFormat, 和 java.util.GregorianCalendar 創(chuàng)建許多方法了.
posted @ 2006-06-09 16:00 nbt 閱讀(477) | 評(píng)論 (1) | 編輯 收藏