由于struts2框架在eclipse中还没有像struts1.x那样的被很好支持。我们使用该框架的时候都需要手工搭建开发环境,其实想想这何尝不是一种好事,特别对于初学者。在手工搭建Struts2开发环境的时候,可以按如下步骤进行(个人观点):
1、新建一个WEB项目。
2、往“WEB项目根目录/WEB-INF/LIB/”中导入struts2所需的jar包,如下所示(版本号可能不同),在此有一个注意事项,等下我会提到。
commons
-
logging
-
1.0
.
4
.jar
freemarker - 2.3 . 8 .jar
ognl - 2.6 . 11 .jar
struts2 - core - 2.0 . 9 .jar
xwork - 2.0 . 4 .jar
freemarker - 2.3 . 8 .jar
ognl - 2.6 . 11 .jar
struts2 - core - 2.0 . 9 .jar
xwork - 2.0 . 4 .jar
3、往“WEB项目根目录/WEB-INF/classes/”中新建struts2所需的struts.xml文件(如下所示:)。
1
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
2 <! DOCTYPE struts PUBLIC
3 " -//Apache Software Foundation//DTD Struts Configuration 2.0//EN "
4 " http://struts.apache.org/dtds/struts-2.0.dtd " >
5
6 < struts >
7 </ struts >
8
2 <! DOCTYPE struts PUBLIC
3 " -//Apache Software Foundation//DTD Struts Configuration 2.0//EN "
4 " http://struts.apache.org/dtds/struts-2.0.dtd " >
5
6 < struts >
7 </ struts >
8
4、修改web.xml文件,所下所示(就是增加一个filter):
1
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
2 < web - app version = " 2.4 "
3 xmlns = " http://java.sun.com/xml/ns/j2ee "
4 xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance "
5 xsi:schemaLocation = " http://java.sun.com/xml/ns/j2ee
6 http: // java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
7 < filter >
8 < filter - name > struts2 </ filter - name >
9 < filter - class > org.apache.struts2.dispatcher.FilterDispatcher </ filter - class >
10 </ filter >
11 < filter - mapping >
12 < filter - name > struts2 </ filter - name >
13 < url - pattern > /*</url-pattern>
14 </filter-mapping>
15</web-app>
16 5、到此,开发环境就搭建好了,余下工作就看你自己的了。你可以在上面尽情挥洒你的舞姿。
注意事项:也许初学struts2朋友们经常会碰到struts2导包问题,因为初学者不是很了解到底哪些包是必须的,哪些是可选的,所以盲目的把全部JAR包导入进来,但实际你这样做会出现问题,全部导入后,会发生冲突,部署项目时,将会出现如下错误,项目将无法启动:
2 < web - app version = " 2.4 "
3 xmlns = " http://java.sun.com/xml/ns/j2ee "
4 xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance "
5 xsi:schemaLocation = " http://java.sun.com/xml/ns/j2ee
6 http: // java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
7 < filter >
8 < filter - name > struts2 </ filter - name >
9 < filter - class > org.apache.struts2.dispatcher.FilterDispatcher </ filter - class >
10 </ filter >
11 < filter - mapping >
12 < filter - name > struts2 </ filter - name >
13 < url - pattern > /*</url-pattern>
14 </filter-mapping>
15</web-app>
16 5、到此,开发环境就搭建好了,余下工作就看你自己的了。你可以在上面尽情挥洒你的舞姿。
注意事项:也许初学struts2朋友们经常会碰到struts2导包问题,因为初学者不是很了解到底哪些包是必须的,哪些是可选的,所以盲目的把全部JAR包导入进来,但实际你这样做会出现问题,全部导入后,会发生冲突,部署项目时,将会出现如下错误,项目将无法启动:
1
信息: Parsing configuration file [struts
-
plugin.xml]
2 2007 - 3 - 21 17 : 37 : 29 org.apache.catalina.core.StandardContext filterStart
3 严重: Exception starting filter struts2
4 Unable to load bean: type:com.opensymphony.xwork2.ObjectFactory class :org.apache.struts2.spring.StrutsSpringObjectFactory - bean - jar:file: / C: / Program % 20Files / tomcat5. 5.23 / webapps / Hello / WEB - INF / lib / struts2 - spring - plugin - 2.0 . 6 .jar !/ struts - plugin.xml: 8 : 132
5 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java: 209 )
6 at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java: 101 )
7 ..
8 Caused by: java.lang.NoClassDefFoundError: org / springframework / context / ApplicationContextAware
9 at java.lang.ClassLoader.defineClass1(Native Method)
10 at java.lang.ClassLoader.defineClass(ClassLoader.java: 620 )
11
12 2007 - 3 - 21 17 : 37 : 30 org.apache.catalina.core.StandardContext start
13 严重: Error filterStart
14 2007 - 3 - 21 17 : 37 : 30 org.apache.catalina.core.StandardContext start
15 严重: Context [ / Hello] startup failed due to previous errors
16 2007 - 3 - 21 17 : 37 : 30 com.opensymphony.xwork2.util.ObjectTypeDeterminerFactory
17
2 2007 - 3 - 21 17 : 37 : 29 org.apache.catalina.core.StandardContext filterStart
3 严重: Exception starting filter struts2
4 Unable to load bean: type:com.opensymphony.xwork2.ObjectFactory class :org.apache.struts2.spring.StrutsSpringObjectFactory - bean - jar:file: / C: / Program % 20Files / tomcat5. 5.23 / webapps / Hello / WEB - INF / lib / struts2 - spring - plugin - 2.0 . 6 .jar !/ struts - plugin.xml: 8 : 132
5 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java: 209 )
6 at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java: 101 )
7 ..
8 Caused by: java.lang.NoClassDefFoundError: org / springframework / context / ApplicationContextAware
9 at java.lang.ClassLoader.defineClass1(Native Method)
10 at java.lang.ClassLoader.defineClass(ClassLoader.java: 620 )
11
12 2007 - 3 - 21 17 : 37 : 30 org.apache.catalina.core.StandardContext start
13 严重: Error filterStart
14 2007 - 3 - 21 17 : 37 : 30 org.apache.catalina.core.StandardContext start
15 严重: Context [ / Hello] startup failed due to previous errors
16 2007 - 3 - 21 17 : 37 : 30 com.opensymphony.xwork2.util.ObjectTypeDeterminerFactory
17
解决的办法是:我们开始只需导入上面提到的五个包则可,后面在实际的项目中如果要用到别的包时再加入,并在配置上做相应的调整。