【IT168 技术文档】
首先利用官方 seam-gen 生成项目骨架
测试环境:
jboss 4.2.2 GA
jboss - seam : 2.0.1
ant : 1.7
设置好系统变量
A、命令行模式下进入 d:\java\seam
d:\java\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.2.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://localhost:3306/dataTest]
[input] Enter database username [sa]
dateUser
[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
- 80 -
[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
B、在Eclipse workspace 目录中创建一个新的项目,只需输入:
seam new-project
这组操作复制了Seam jar 文件及相应的jar 文件与JDBC 驱动jar 到新建的Eclipse 项目中,并生成了所需的源文件及其配置文件、一个模板文件和样式文件,及相应的Eclipse 元数据及Ant 构建脚本。 只要你依此操作 New -> Project... -> General -> Project -> Next,输入Project name (此例为helloworld),并接着点击 Finish,就可将Eclipse 项目自动部署到JBoss AS分解式的(exploded)目录结构中,请不要在新项目向导中选择 Java Project。此时你会在你的项目目录中发现已经生成了很多文件
C、输入 seam explode 来部署项目。
在 http://localhost:8080/helloworld 中查看此应用的首页。view/home.xhtml 是个使用 view/layout/template.xhtml 模板生成的Facelets 页面,试着在Eclipse 中编辑此页面或该模板,并立即在浏览器中刷新页面,看看有什么结果。
别被在项目中的如此多的XML 配置文件给吓晕了。那都是些标准的Java EE 的东西,它们只需生成一次就不用再去理会了。 在所有的Seam 项目中,90%的配置内容都是一样的(这些可由seam-gen 来帮我们完成)。
D、创建新动作 :seam new-action
创建有动作的表单:seam new-form
新增Seam 组件后,我们需要重启分解式目录部署(exploded directory deployment)。 输入seam restart,或在Eclipse 中已生成项目的 build.xml中运行 restart target 就可完成。 另一种方式是在Eclipse 中通过编辑resources/META-INF/application.xml 文件来强制重启。 请注意,在每次修改应用程序时并不需要重启JBoss。
E、从现有数据库生成应用程序 :seam generate-entities
接着重新部署,并在浏览器中输入 http://localhost:8080/helloworld 就可看到结果了。