题记:以前用MyEclipse+Tomcat进行过struts2的学习,最后学习计划不了了之。现在因为工作需用到struts2 进行开发,所以打算从新开始学习struts2。但是,在ubuntu上进行开发时,我喜欢用vim,所以就想要不干脆放弃IDE,直接用vim进行开发。然后,就是查找相关不用IDE进行web开发的指导教程。最后,被我发现在《struts2权威指南》有对该方面的介绍,于是开始了无IDE的web开发之旅。
ubuntu安装JDK:
从sun网站上http://www.oracle.com/technetwork/java/javase/downloads/index.html下载JDK。在这里我下载的版本是jdk-7u45-linux-x64.tar.gz。下载完毕后,直接tar命令解压到任意目录下(此处解压到/usr/lib/jdk),然后在.profile中添加JAVA_HOME环境变量,具体配置如下。
export JAVA_HOME=/usr/lib/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
最后,使用java -version命令判断JDK是否安装成功。若出现如下提示,则表明JDK安装成功。
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
ubuntu上安装Tomcat:
众所周知,Tomcat是开源的web容器,可直接到网站http://tomcat.apache.org/上下载。我下载的是apache-tomcat-6.0.37.tar.gz文件,该文件无需安装,直接解压即可使用。当然,在此之前需安装好jdk。
下载struts2源码:
struts2也是apache的一个开源项目,可从网站http://struts.apache.org/development/2.x/上下载。目前,最新的版本是2.3.16,我下载的是完全版struts-2.3.15.3-all.zip。在下载目录解压该文件,可看到如下文件:
ANTLR-LICENSE.txt lib SITEMESH-LICENSE.txt
apps LICENSE.txt src
CLASSWORLDS-LICENSE.txt NOTICE.txt XPP3-LICENSE.txt
docs OGNL-LICENSE.txt XSTREAM-LICENSE.txt
FREEMARKER-LICENSE.txt OVAL-LICENSE.txt
第一个web应用:
在tomcat主目录下的webapps下创建一个自己的web应用根目录,例:myapps。并在根目录下创建两个目录:WEB-INF,lib。
进入到struts的apps目录,解压该目录下的struts2-blank.war文件,进入WEB-INF/lib,并其中的jar包拷贝到我们创建的lib目录下,需要拷贝的jar包如下所示:
commons-fileupload-1.3.jar javax.servlet.jsp.jstl.jar
commons-io-2.0.1.jar jstl-impl.jar
commons-lang3-3.1.jar log4j-1.2.17.jar
commons-logging-1.1.3.jar ognl-3.0.6.jar
freemarker-2.3.19.jar struts2-core-2.3.15.3.jar
javassist-3.11.0.GA.jar xwork-core-2.3.15.3.jar
如果没有拷贝javassist-3.11.0.GA.jar到lib目录下,将会出现以下错误:
java.lang.reflect.InvocationTargetException - Class: com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector
File: ContainerImpl.java
Method: inject
Line: 301 - com/opensymphony/xwork2/inject/ContainerImpl.java:301:-1
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:502)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:57)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:295)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:424)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4072)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4726)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:301)
at com.opensymphony.xwork2.inject.ContainerImpl$ConstructorInjector.construct(ContainerImpl.java:438)
at com.opensymphony.xwork2.inject.ContainerBuilder$5.create(ContainerBuilder.java:207)
at com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:51)
at com.opensymphony.xwork2.inject.ContainerBuilder$3.create(ContainerBuilder.java:93)
at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:487)
at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:484)
at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:584)
at com.opensymphony.xwork2.inject.ContainerBuilder.create(ContainerBuilder.java:484)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.createBootstrapContainer(DefaultConfiguration.java:324)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:221)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:67)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:446)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:490)
... 29 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:299)
... 42 more
Caused by: java.lang.ExceptionInInitializerError
at com.opensymphony.xwork2.ognl.OgnlValueStackFactory.setContainer(OgnlValueStackFactory.java:84)
... 47 more
Caused by: java.lang.IllegalArgumentException: Javassist library is missing in classpath! Please add missed dependency!
at ognl.OgnlRuntime.<clinit>(OgnlRuntime.java:168)
... 48 more
Caused by: java.lang.ClassNotFoundException: javassist.ClassPool
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at ognl.OgnlRuntime.<clinit>(OgnlRuntime.java:165)
... 48 more
Dec 07, 2013 10:37:06 PM org.apache.catalina.core.StandardContext start
SEVERE: Error filterStart
Dec 07, 2013 10:37:06 PM org.apache.catalina.core.StandardContext start
SEVERE: Context [/Hello] startup failed due to previous errors
最后,把struts.xml文件从myapps/WEB-INF/classes目录下,web.xml文件拷贝到myapps/WEB-INF目录下,并在myapps目录下添加属于自己的jsp文件,例如:login.jsp。根据实际情况配置好这些文件之后,启动tomcat测试用例的正确性。