首先,我們需要根據(jù)現(xiàn)有的開發(fā)環(huán)境對seam-gen進行配置:JBoss AS安裝目錄、Eclipse workspace及數(shù)據(jù)庫連接。這些都很容易,只要敲入:
cd jboss-seam-2.0.x
seam setup
根據(jù)彈出的提示輸入開發(fā)環(huán)境的相關(guān)信息:
C:\Projects\jboss-seam>seam setup
Buildfile: build.xml
setup:
[echo] Welcome to seam-gen :-)
[input] Enter your Java project workspace [C:/Projects]
[input] Enter your JBoss home directory [C:/Program Files/jboss-4.2.0.GA]
[input] Enter the project name [myproject]
helloworld
[input] Is this project deployed as an EAR (with EJB components) or a WAR (with no EJB support) [ear] (ear,war,)
[input] Enter the Java package name for your session beans [com.mydomain.helloworld]
org.jboss.helloworld
[input] Enter the Java package name for your entity beans [org.jboss.helloworld]
[input] Enter the Java package name for your test cases [org.jboss.helloworld.test]
[input] What kind of database are you using? [hsql] (hsql,mysql,oracle,postgres,mssql,db2,sybase,)
mysql
[input] Enter the Hibernate dialect for your database [org.hibernate.dialect.MySQLDialect]
[input] Enter the filesystem path to the JDBC driver jar [lib/hsqldb.jar]
../../mysql-connector.jar
[input] Enter JDBC driver class for your database [com.mysql.jdbc.Driver]
[input] Enter the JDBC URL for your database [jdbc:mysql:///test]
[input] Enter database username [sa]
gavin
[input] Enter database password []
[input] skipping input as property hibernate.default_schema.new has already been set.
[input] Enter the database catalog name (it is OK to leave this blank) []
[input] Are you working with tables that already exist in the database? [n] (y,n,)
y
[input] Do you want to drop and recreate the database tables and data in import.sql each time you deploy? [n] (y,n,)
n
[propertyfile] Creating new property file: C:\Projects\jboss-seam\seam-gen\build.properties
[echo] Installing JDBC driver jar to JBoss server
[echo] Type 'seam new-project' to create the new project
BUILD SUCCESSFUL
Total time: 1 minute 17 seconds
C:\Projects\jboss-seam>
該工具提供了相應(yīng)的默認值,因此你可以直接按Enter鍵。
最重要的是你要對EAR部署還是WAR部署進行選擇。EAR項目支持EJB 3.0 并需要Java EE 5。而WAR包不支持EJB 3.0,但可在J2EE環(huán)境中部署。 另外WAR也更較簡單,便于理解。假若你已安裝了EJB3 profile,那你就用ear好了,否則,就只好用 war。 在此假設(shè)我們選擇了EAR部署,當然此教程也適用于WAR部署。
如果你手上有現(xiàn)成的數(shù)據(jù)模型,請確保你已輸入現(xiàn)有數(shù)據(jù)庫的表名。
這些設(shè)置保存在 seam-gen/build.properties 文件中,但你可通過運行 seam setup 來再次修改。
現(xiàn)在我們就可以在Eclipse workspace目錄中創(chuàng)建一個新的項目,只需輸入:
seam new-project
C:\Projects\jboss-seam>seam new-project
Buildfile: build.xml
validate-workspace:
validate-project:
copy-lib:
[echo] Copying project jars ...
[copy] Copying 58 files to C:\Projects\helloworld\lib
[copy] Copying 9 files to C:\Projects\helloworld\embedded-ejb
file-copy-war:
file-copy-ear:
[echo] Copying resources needed for EAR deployment to the C:\Projects\helloworld/resources directory...
new-project:
[echo] A new Seam project named 'helloworld' was created in the C:\Projects directory
[echo] Type 'seam explode' and go to http://localhost:8080/helloworld
[echo] Eclipse Users: Add the project into Eclipse using File > New > Project and select General > Project (not Java Project)
[echo] NetBeans Users: Open the project in NetBeans
BUILD SUCCESSFUL
Total time: 7 seconds
C:\Projects\jboss-seam>
這組操作復(fù)制了Seam jar文件及相應(yīng)的jar文件與JDBC驅(qū)動jar到新建的Eclipse項目中,并生成了所需的源文件及其配置文件、一個模板文件和樣式文件,及相應(yīng)的Eclipse元數(shù)據(jù)及Ant構(gòu)建腳本。 只要你依此操作 New -> Project... -> General -> Project -> Next,輸入Project name (此例為helloworld),并接著點擊 Finish,就可將Eclipse項目自動部署到JBoss AS分解式的(exploded)目錄結(jié)構(gòu)中,請不要在新項目向?qū)е羞x擇 Java Project。
若Eclipse中的默認的JDK不是Java SE 5 或Java SE 6,你就得通過 Project -> Properties -> Java Compiler 來選擇與Java SE 5 兼容的JDK。
另外,可在Eclipse之外輸入 seam explode 來部署項目。
在 http://localhost:8080/helloworld 中查看此應(yīng)用的首頁。 view/home.xhtml 是個使用 view/layout/template.xhtml 模板生成的Facelets 頁面,試著在Eclipse中編輯此頁面或該模板,并在瀏覽器中刷新頁面,立即看到結(jié)果。
別被在項目目錄中的如此多的XML配置文件給嚇暈了。那都是些標準的Java EE的東西,它們只需生成一次就不用再去理會了。 在所有的Seam項目中,90%的配置內(nèi)容都是一樣的(這些可由seam-gen來幫我們完成)。
新生成的項目包含了三個數(shù)據(jù)庫及持久化配置文件。jboss-beans.xml、persistence-test.xml 及 import-test.sql 文件是用在當TestNG對HSQLDB進行單元測試時。 在 import-test.sql 中的數(shù)據(jù)庫Schema及其測試數(shù)據(jù)總是在測試前就已轉(zhuǎn)入數(shù)據(jù)庫中。 myproject-dev-ds.xml、persistence-dev.xml 及 import-dev.sql 文件是在部署應(yīng)用到開發(fā)數(shù)據(jù)庫時使用的。 數(shù)據(jù)庫schema是否可在部署時自動導出,取決于你是否在設(shè)置seam-gen環(huán)境時配置了已存在的數(shù)據(jù)庫。 myproject-prod-ds.xml、persistence-prod.xml 及 import-prod.sql 文件是在部署應(yīng)用到生產(chǎn)數(shù)據(jù)庫時使用的。在部署時數(shù)據(jù)庫schema并不自動導出。
若你熟知傳統(tǒng)的action-style Web框架,你或許想知道在Java中如何來創(chuàng)建無狀態(tài)action方法的簡單Web頁面。如果你輸入:
seam new-action
則Seam將彈出一些信息并為你的項目生成新的Facelets頁面及Seam組件。
C:\Projects\jboss-seam>seam new-action
Buildfile: build.xml
validate-workspace:
validate-project:
action-input:
[input] Enter the Seam component name
ping
[input] Enter the local interface name [Ping]
[input] Enter the bean class name [PingBean]
[input] Enter the action method name [ping]
[input] Enter the page name [ping]
setup-filters:
new-action:
[echo] Creating a new stateless session bean component with an action method
[copy] Copying 1 file to C:\Projects\helloworld\src\action\org\jboss\helloworld
[copy] Copying 1 file to C:\Projects\helloworld\src\action\org\jboss\helloworld
[copy] Copying 1 file to C:\Projects\helloworld\src\action\org\jboss\helloworld\test
[copy] Copying 1 file to C:\Projects\helloworld\src\action\org\jboss\helloworld\test
[copy] Copying 1 file to C:\Projects\helloworld\view
[echo] Type 'seam restart' and go to http://localhost:8080/helloworld/ping.seam
BUILD SUCCESSFUL
Total time: 13 seconds
C:\Projects\jboss-seam>
新增Seam組件后,我們需要重啟分解式目錄部署(exploded directory deployment)。 輸入seam restart,或在Eclipse中已生成項目的 build.xml 中運行 restart target就可完成。 另一種方式是在Eclipse中通過編輯 resources/META-INF/application.xml 文件來強制重啟。 請注意,在每次修改應(yīng)用程序時并不需要重啟JBoss。
試著在瀏覽器中輸入 http://localhost:8080/helloworld/ping.seam 地址并點擊按鈕,看看發(fā)生了什么。 在項目的 src 目錄中可看到完成此動作的源代碼。 試著在 ping() 方法中設(shè)置個斷點,再次點擊按鈕,又發(fā)生了什么?
最后,在測試包中找到 PingTest.xml 文件,并用Eclipse的TestNG插件來運行測試。 此外,還可用 seam test 或生成的build文件中的 test target來運行測試。
2.4. 創(chuàng)建有動作的表單(form)
下一步就是來創(chuàng)建表單了。請輸入:
seam new-form
C:\Projects\jboss-seam>seam new-form
Buildfile: C:\Projects\jboss-seam\seam-gen\build.xml
validate-workspace:
validate-project:
action-input:
[input] Enter the Seam component name
hello
[input] Enter the local interface name [Hello]
[input] Enter the bean class name [HelloBean]
[input] Enter the action method name [hello]
[input] Enter the page name [hello]
setup-filters:
new-form:
[echo] Creating a new stateful session bean component with an action method
[copy] Copying 1 file to C:\Projects\hello\src\com\hello
[copy] Copying 1 file to C:\Projects\hello\src\com\hello
[copy] Copying 1 file to C:\Projects\hello\src\com\hello\test
[copy] Copying 1 file to C:\Projects\hello\view
[copy] Copying 1 file to C:\Projects\hello\src\com\hello\test
[echo] Type 'seam restart' and go to http://localhost:8080/hello/hello.seam
BUILD SUCCESSFUL
Total time: 5 seconds
C:\Projects\jboss-seam>
再次重啟應(yīng)用程序,并在瀏覽器中輸入 http://localhost:8080/helloworld/hello.seam 就可看到結(jié)果了。 接著看下所生成的代碼,并運行測試。試著給表單加入一些字段及Seam組件(記著在每次更改Java代碼時重新部署)。
2.5. 從現(xiàn)有數(shù)據(jù)庫生成應(yīng)用程序
在數(shù)據(jù)庫中手工創(chuàng)建一些表。(如果你需要切換不同的數(shù)據(jù)庫,只需再次運行 seam setup 即可。)現(xiàn)請輸入:
seam generate-entities
接著重新部署,并在瀏覽器中輸入 http://localhost:8080/helloworld 就可看到結(jié)果了。 你可以試著瀏覽數(shù)據(jù)庫,編輯現(xiàn)有的對象,并創(chuàng)建新的對象。如果你看下所生成的代碼,你可能會對如此簡單的代碼感到驚訝。 讓開發(fā)人員,尤其是那些不甘于受Seam-gen擺布的開發(fā)人員,簡單地手工編寫數(shù)據(jù)訪問代碼,是Seam的設(shè)計目標之一。
最后,我們想知道能否用標準的Java EE包來部署應(yīng)用。首先,通過運行 seam unexplode 來移走分解式目錄(exploded directory)。 在命令行中輸入 seam deploy 或運行生成的Build腳本文件中的 deploy target就可完成EAR的部署,用 seam undeploy 命令或運行 undeploy 目標可卸下EAR。
默認情況下,應(yīng)用程序會用 dev profile 來部署,EAR將包含persistence-dev.xml及import-dev.sql文件,myproject-dev-ds.xml 文件也會被部署。 通過輸入以下的命令你就可以更改profile, 并可使用 prod profile:
seam -Dprofile=prod deploy
你甚至可以給你的應(yīng)用程序定義新的部署profile,只需在項目中加入合適的文件, 例如:persistence-staging.xml、import-staging.sql 及 myproject-staging-ds.xml — 并選擇使用了 -Dprofile=staging 名字的profile。
將Seam應(yīng)用部署成exploded目錄的好處是,你能在開發(fā)時得到增量熱部署的支持。 你只需在 components.xml 中添加這一行來啟用Seam和Facelet中的debug模式即可:
<core:init debug="true"/>
這樣一來,重新部署以下文件時就不一定要完全地重啟web應(yīng)用了:
-
任意Facelet頁面
-
任意 pages.xml 文件
若想對Java代碼進行變更,就需要完全的應(yīng)用重啟。 (在JBoss中,對于EAR部署,這需要用touch命令改變頂層的部署描述文件:對于EAR部署,則是application.xml,而對于WAR部署,則是web.xml 。)
但你真正想加快編輯/編譯/測試的流程,Seam支持對JavaBean組件進行增量式重部署。 為了用上此功能,你必須把JavaBean組件部署到 WEB-INF/dev 目錄中,以便它們能被特殊的Seam類加載器加載,而不是WAR或EAR類加載器。
請注意以下的限制:
-
必須是JavaBean組件,而不能是EJB3 Beans(此限制正在解決中)
-
實體Bean不可熱部署
-
通過 components.xml 部署的組件可能無法熱部署
-
在 WEB-INF/dev 之外部署的任何類都無法訪問可熱部署的組件
-
須啟用Seam的debug模式
如果你用Seam-gen創(chuàng)建WAR項目,增量熱部署對于src/action目錄下的類是直接可用的,但是對于EAR項目不行。
柴油發(fā)電機
發(fā)電機
柴油機
柴油發(fā)電機
13636374743(上海)
13291526067(嘉興)