以前用的是tapestry3.0.4版,现在看看tapestry最新版都5.3.2了,那就在本机搭个环境看看吧。话不多说,先确保本机装了 eclipse和maven。然后大家和我一起go on...
下面我们搭建一个tapestry5.3.2的小例子:
在这个例子中,我们使用 com.example 作为group id,使用 quickstart 作为artifact id, 版本使用1.0-SNAPSHOT ,使用com.example.app 作为基础源码包。
上面四个值,除了版本不要改,其他的都可以自己命名。
1、先下载代码
先确定要开始的工作目录,假设是E:\workspace。
到这个目录下面输入命令:
- mvn archetype:generate -DarchetypeCatalog=http://tapestry.apache.org
这个命令帮助我们从网上下载例子。
在执行过程中需要根据提示进行选择和输入:依次输入
提示选择例子类型,输入 1;
提示选择tapestry的版本号,输入6,或者直接回车
提示输入groupId,输入com.example
提示输入artifact id,输入quickstart
提示输入版本,回车
提示输入包,输入com.example.app
确认信息,输入Y或者直接回车
静静的等待,当界面出现BUILD SUCCESS,那么就ok了,例子就下载下来了,在E:\workspace\quickstart目录下面。
操作过程如下面:
- E:\workspace> mvn archetype:generate -DarchetypeCatalog=http://tapestry.apache.org
- [INFO] Scanning for projects...
- [INFO] Searching repository for plugin with prefix: 'archetype'.
- [INFO] ------------------------------------------------------------------------
- [INFO] Building Maven Default Project
- [INFO] task-segment: [archetype:generate] (aggregator-style)
- [INFO] ------------------------------------------------------------------------
- [INFO] Preparing archetype:generate
- [INFO] No goals needed for project - skipping
- [INFO] [archetype:generate {execution: default-cli}]
- [INFO] Generating project in Interactive mode
- [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
- Choose archetype:
- 1: http://tapestry.apache.org -> org.apache.tapestry:quickstart (Tapestry 5 Quickstart Project)
- 2: http://tapestry.apache.org -> org.apache.tapestry:tapestry-archetype (Tapestry 4.1.6 Archetype)
- Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
- Choose version:
- 1: 5.0.19
- 2: 5.1.0.5
- 3: 5.2.6
- 4: 5.3
- 5: 5.3.1
- 6: 5.3.2
- Choose a number: 6: 6
- Define value for property 'groupId': : com.example
- Define value for property 'artifactId': : quickstart
- Define value for property 'version': 1.0-SNAPSHOT: :
- Define value for property 'package': com.example: : com.example.app
- Confirm properties configuration:
- groupId: com.example
- artifactId: quickstart
- version: 1.0-SNAPSHOT
- package: com.example.app
- Y: :
- [INFO] ----------------------------------------------------------------------------
- [INFO] Using following parameters for creating project from Archetype: quickstart:5.3.2
- [INFO] ----------------------------------------------------------------------------
在上面运行过程中,如果在选择例子类型之前就出现执行异常。如下:
- E:\workspace>mvn archetype:generate -DarchetypeCatalog=http://tapestry.apache.or
- g
- [INFO] Scanning for projects...
- [INFO] Searching repository for plugin with prefix: 'archetype'.
- [INFO] ------------------------------------------------------------------------
- [INFO] Building Maven Default Project
- [INFO] task-segment: [archetype:generate] (aggregator-style)
- [INFO] ------------------------------------------------------------------------
- [INFO] Preparing archetype:generate
- [INFO] No goals needed for project - skipping
- [INFO] [archetype:generate {execution: default-cli}]
- [INFO] Generating project in Interactive mode
- [WARNING] Error reading archetype catalog http://tapestry.apache.org
- java.lang.RuntimeException: java.lang.NullPointerException
- at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1006)
- at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:2203)
- at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:382)
- at org.apache.maven.wagon.providers.http.LightweightHttpWagon.fillInputData(LightweightHttpWagon.java:115)
这个是因为你的网络有问题,无法访问它的网站下载,没关系,给maven配置一个代理就行了。
配置文件位置:<<maven安装目录>>\conf\settings.xml
修改代理信息如下:
- <proxy>
- <id>optional</id>
- <active>true</active>
- <protocol>http</protocol>
- <username></username>
- <password></password>
- <host>218.189.26.158</host>
- <port>8080</port>
- <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
- </proxy>
这个配置里面的ip:port是我从网上找的,可能在一段时期内有效,如果不能用,你也可以找其他的代理ip。
2、运行例子
例子都下载下来了,有点迫不及待了吧,那我们开始运行吧。
为了本机开发方便,官方推荐使用jetty运行程序。很简单:
进入E:\workspace\quickstart目录。敲入命令:
- E:\workspace\quickstart>mvn jetty:run
- TranslatorSource: DEFINED
- TypeCoercer: REAL
- URLEncoder: DEFINED
- UpdateListenerHub: DEFINED
- ValidateBindingFactory: DEFINED
- ValidationConstraintGenerator: DEFINED
- ValidationDecoratorFactory: DEFINED
- ValidatorMacro: DEFINED
- ValueEncoderSource: DEFINED
- 86.77% unrealized services (164/189)
- Application 'app' (version 1.0-SNAPSHOT-DEV) startup time: 703 ms to build IoC R
- egistry, 2,109 ms overall.
- ______ __ ____
- /_ __/__ ____ ___ ___ / /_______ __ / __/
- / / / _ `/ _ \/ -_|_-</ __/ __/ // / /__ \
- /_/ \_,_/ .__/\__/___/\__/_/ \_, / /____/
- /_/ /___/ 5.3.2 (development mode)
- 2012-04-19 14:17:57.484::INFO: Started SelectChannelConnector@0.0.0.0:8080
- [INFO] Started Jetty Server
那么就说明服务启起来了,可以通过
http://localhost:8080/quickstart/进行访问了。哈哈。打开的页面是不是很漂亮。。。哈哈
对了,如果要停止jetty执行,那么按ctrl+c吧
PS:执行的过程中会下载很多jar包,如果网络有问题,提示出错,某文件没下载下来,那就重新执行上面的命令,重新下载。
3、在eclipse里面运行例子
怎么在eclipse里面浏览例子或者运行呢?分两步:
步骤一:先让maven帮你生成eclipse所需要的工程配置文件吧(.classpath and .project)。
输入命令,如下:
- E:\workspace\quickstart>mvn eclipse:eclipse -DdownloadSources=true
静静等待,当界面出现:
- [INFO] ------------------------------------------------------------------------
- [INFO] BUILD SUCCESSFUL
- [INFO] ------------------------------------------------------------------------
- [INFO] Total time: 4 seconds
- [INFO] Finished at: Thu Apr 19 11:20:59 CST 2012
- [INFO] Final Memory: 17M/30M
- [INFO] ------------------------------------------------------------------------
那就ok了。
你就可以从eclipse里面导入(import)一个已经存在的java工程了。这个大家都知道了吧。
步骤二:在eclipse里面运行
先给eclipse装一个插件RunJettyRun插件。如下:
下载地址:http://run-jetty-run.googlecode.com/svn/trunk/updatesite
选择1.3
插件装完之后,继续配置工程的运行方式
配完之后点击Run,就可以运行了。
(开发环境中为了得到更好的运行性能和错误报告:可以在第二页参数页Arguments,添加虚拟机的运行参数 -XX:MaxPermSize=256M -Xmx600m -Dtapestry.execution-mode=development )
4、在tomcat里面运行例子
为了能在tomcat里面运行,我是这么做的:
如上图,在E:\workspace\quickstart\src\main\webapp\WEB-INF下面建两个目录classes和lib
classes里面放置E:\workspace\quickstart\src\main\java编译后的class和E:\workspace\quickstart\src\main\resources下面的资源。保留层次结构不变。
lib里面放置工程依赖的jar包,多一点没关系,进测试可以如下:
- E:\workspace\quickstart\src\main\webapp\WEB-INF\lib>dir
- 驱动器 E 中的卷是 Work
- 卷的序列号是 9027-D637
- E:\workspace\quickstart\src\main\webapp\WEB-INF\lib 的目录
- 2012-04-18 16:59 <DIR> .
- 2012-04-18 16:59 <DIR> ..
- 2012-04-17 00:52 199,418 antlr-runtime-3.3-sources.jar
- 2012-04-17 00:37 163,650 antlr-runtime-3.3.jar
- 2012-04-17 00:52 102,254 commons-codec-1.5-sources.jar
- 2012-04-17 00:37 73,098 commons-codec-1.5.jar
- 2012-04-17 00:53 495,186 javassist-3.12.1.GA-sources.jar
- 2012-04-17 00:39 644,148 javassist-3.12.1.GA.jar
- 2012-04-17 00:53 10,928 javax.inject-1-sources.jar
- 2012-04-17 00:38 2,497 javax.inject-1.jar
- 2012-04-17 00:51 813,521 js-1.6R7.jar
- 2012-04-17 00:53 343,300 log4j-1.2.14-sources.jar
- 2010-07-07 14:11 367,444 log4j-1.2.14.jar
- 2012-04-17 00:53 739,203 plastic-5.3.2-sources.jar
- 2012-04-17 00:38 489,767 plastic-5.3.2.jar
- 2012-04-17 00:53 45,038 slf4j-api-1.6.1-sources.jar
- 2012-04-17 00:38 25,496 slf4j-api-1.6.1.jar
- 2012-04-17 00:53 9,549 slf4j-log4j12-1.6.1-sources.jar
- 2012-04-17 00:38 9,753 slf4j-log4j12-1.6.1.jar
- 2012-04-17 00:52 2,464,253 tapestry-core-5.3.2-sources.jar
- 2012-04-17 00:37 1,843,136 tapestry-core-5.3.2.jar
- 2012-04-17 00:53 36,782 tapestry-func-5.3.2-sources.jar
- 2012-04-17 00:38 60,203 tapestry-func-5.3.2.jar
- 2012-04-17 00:52 415,402 tapestry-ioc-5.3.2-sources.jar
- 2012-04-17 00:38 521,902 tapestry-ioc-5.3.2.jar
- 2012-04-17 00:52 24,466 tapestry-json-5.3.2-sources.jar
- 2012-04-17 00:37 20,449 tapestry-json-5.3.2.jar
- 2012-04-17 01:01 6,695 tapestry-yuicompressor-5.3.2-sources.jar
- 2012-04-17 00:50 10,640 tapestry-yuicompressor-5.3.2.jar
- 2012-04-17 00:53 20,050 tapestry5-annotations-5.3.2-sources.jar
- 2012-04-17 00:38 11,146 tapestry5-annotations-5.3.2.jar
- 2011-10-03 13:28 853,404 yuicompressor-2.4.7.jar
- 31 个文件 12,799,446 字节
- 2 个目录 17,060,093,952 可用字节
那么在tomcat的控制台里面配置:
Context Path (required): /quickstart
WAR or Directory URL: E:\workspace\quickstart\src\main\webapp
或者到tomcat的配置文件<<tomcat安装目录>>\conf\server.xml里面配置:
<Context path="/quickstart" docBase="E:\workspace\quickstart\src\main\webapp"></Context>
好了,到这里tomcat里面也运行起来了。
到这里,结束了。
不过截止到今天(4月19日)上面的步骤还不是尽善尽美,因为上面工程依赖的一个jar包不是最新,运行的时候会报错,可能你现在阅读这篇文章的时候已经是更新后不报错了。
怎么解决报错?
首先报错部分信息如下:
- [ERROR] AssetsModule.ResourceMinimizer Exception minimizing 'core' JavaScript stack, for locale en, resources=classpath:org/apache/tapestry5/underscore_1_1_7.js, classpath:org/apache/tapestry5/scriptaculous_1_9_0/prototype.js, classpath:org/apache/tapestry5/scriptaculous_1_9_0/scriptaculous.js, classpath:org/apache/tapestry5/scriptaculous_1_9_0/effects.js, classpath:org/apache/tapestry5/t5-core.js, classpath:org/apache/tapestry5/t5-spi.js, classpath:org/apache/tapestry5/t5-prototype.js, classpath:org/apache/tapestry5/t5-init.js, classpath:org/apache/tapestry5/t5-pubsub.js, classpath:org/apache/tapestry5/t5-events.js, classpath:org/apache/tapestry5/t5-dom.js, classpath:org/apache/tapestry5/t5-console.js, classpath:org/apache/tapestry5/t5-ajax.js, classpath:org/apache/tapestry5/t5-formfragment.js, classpath:org/apache/tapestry5/t5-alerts.js, classpath:org/apache/tapestry5/tapestry.js, classpath:org/apache/tapestry5/tapestry-console.js, classpath:org/apache/tapestry5/tree.js, classpath:org/apache/tapestry5/tapestry-messages.js: java.util.EmptyStackException
- java.util.EmptyStackException
- at java.util.Stack.peek(Stack.java:85)
- at com.yahoo.platform.yui.compressor.JavaScriptCompressor.getCurrentScope(JavaScriptCompressor.java:559)
- at com.yahoo.platform.yui.compressor.JavaScriptCompressor.printSymbolTree(JavaScriptCompressor.java:1105)
- at com.yahoo.platform.yui.compressor.JavaScriptCompressor.compress(JavaScriptCompressor.java:553)
- at org.apache.tapestry5.internal.yuicompressor.JavaScriptResourceMinimizer.doMinimize(JavaScriptResourceMinimizer.java:99)
- at org.apache.tapestry5.internal.yuicompressor.AbstractMinimizer$1.perform(AbstractMinimizer.java:67)
- at org.apache.tapestry5.internal.TapestryInternalUtils$5.run(TapestryInternalUtils.java:582)
- at org.apache.tapestry5.ioc.internal.OperationTrackerImpl$1.invoke(OperationTrackerImpl.java:51)
- at org.apache.tapestry5.ioc.internal.OperationTrackerImpl$1.invoke(OperationTrackerImpl.java:48)
- at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:74)
- at org.apache.tapestry5.ioc.internal.OperationTrackerImpl.run(OperationTrackerImpl.java:47)
到网上下载 yuicompressor-2.4.7.jar更新现有的yuicompressor-2.4.6.jar就可以解决上面的报错了。
哈哈,试试吧,祝顺利!
本文档内容,参考了官方文档,有不当之处请指正!谢谢!