目前网上流行的struts2教程说struts2需要5个基本包,其实是不对的,这是解决这个问题时候找到的参考资料,解决了这个异常:
【搜索到的解决这个问题的原文】
这是apache的失误。按照官方文档说明,运行Struts2必须加载5个核心jar包,也就是咱们一开始拷贝的5个jar包,如果你用的是 Struts2之前的版本是没有问题的,但是如果去下载这个新版本,就会报如上错误,因为还需要加载另外两个jar包,可是官方的说明文档没有更新
解决方案:在Struts2的解压缩文件夹内的lib文件夹里, 我们找到下列2个jar包
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
可能遇到的错误2:如果由Spring托管Action类(即struts.xml中引用Spring注入的Action),那么可能出现如下异常:Unable to load bean: type:com.opensymphony.xwork2.ObjectFactory class:org.apache.struts2.spring.StrutsSpringObjectFactory - bean - jar:file:/C:/workspace/apache-tomcat-6.0.18/webapps/myweb/WEB-INF/lib/struts2-spring-plugin-2.1.8.1.jar!/struts-plugin.xml:29:132
如果出现这个异常,需要添加struts2-spring-plugin-2.1.8.1.jar
可能遇到的错误3:Bean type class com.opensymphony.xwork2.ObjectFactory with the name spring has already been loaded by bean(struts2)
出现这个错误时请仔细检查是否有包重复加载,如果存在,请删除重复的包,我在搭建过程中因为部分是ECLIPSE自动导入的,还一部分是手动导入的,导致部分包重复,清除重复包就解决了问题 。
此外还可能遇到的问题:如果你是使用的myeclipse自动导入的Struts2包,那么可能出现如下问题:
404错误: No result defined for action zqq.action.LoginAction and result xxx
这是我在网上搜索到的错误原因和解决办法:
这是因为struts2中有一个struts2-convention-plugin-2.1.8.1.jar包,这个包是一个插件。
作用是根据配置(默认)自动加载项目中使用action,actions,struts,struts2包下的类,将有execute方法的类映射成action.
不是用自动加载的:在struts.xml加入
<constant name="struts.convention.package.locators.disable" value="true"/>
使用自动加载的:
在convention中有以下配置
<constant name="struts.convention.result.path" value="/WEB-INF/content/"/>
所以根据配置在/WEB-INF/content/ 创建jsp了。文件名为:actionName-resultString
好了,下面我把我的配置文件和包发出来以供以后参考:
1.web.xml文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/applicationContext.xml <!--我的applicationContext.xml文件放在项目src根目录下-->
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
2.struts.xml文件
<package name="test" namespace="/" extends="struts-default">
<action name="test" class="testAction">
<result name="succ">/success.jsp</result>
</action>
< /package>
3.applicationContext.xml
<bean id="testAction" class="com.zy.action.TestAction"/>
4.TestAction类
package com.zy.action;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
public TestAction()
{
System.out.print("spring完成了初始化!");
}
public String execute(){
return "succ";
}
}
5.index.jsp的body部分
<body>
<form action="test.action" method="post">
<input type="submit" value="确定"/>
</form>
</body>
然后自己随便写个success.jsp文件