struts2配置 常见异常处理
开发环境:JDK6+tomcat6+struts-2.1.6(最新)
(tomcat一定是5.5以上,jdk为1.5以上)
struts2下载地址:http://struts.apache.org/
最基本的小程序用到jar包,struts2-core.jar,commons-logging.jar,freemarker.jar,ognl.jar,xwork.jar
(注意了:struts2-core-2.1.6.jar时要附加上两个包,commons-fileupload.jar 和 commons-io.jar )
还有了在配置web.xml文件时记得不要在用这个过滤器了,
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>,
新版本中都是过时的了,采取该过滤器,
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>。
安装实例配置好以后,开始运行,可是出现一下错误:
严重: Exception starting filter struts2
Unable to load configuration. - bean - jar:file:/E:/struts2/struts2/WebRoot/WEB-INF/lib/struts2-core-2.1.6.jar!/struts-default.xml:46:178
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:360)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:403)
at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:190)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3709)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4363)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: Unable to load bean: type:org.apache.struts2.dispatcher.multipart.MultiPartRequest class:org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest - bean - jar:file:/E:/struts2/struts2/WebRoot/WEB-INF/lib/struts2-core-2.1.6.jar!/struts-default.xml:46:178
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:222)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:101)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:165)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
... 21 more
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getDeclaredConstructors(Class.java:1836)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:212)
... 24 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileUploadException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
... 28 more
2009-2-16 15:07:42 org.apache.catalina.core.StandardContext start
严重: Error filterStart
2009-2-16 15:07:42 org.apache.catalina.core.StandardContext start
严重: Context [/struts2] startup failed due to previous errors
错误中指明struts-default.xml:46:178,struts2-core.jar包中的struts-default文件的46行有问题,于是我打开struts-default.xml,46行为:
<bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="struts" scope="default"/>
发现要加载org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest。查看这个类的源文件
其中前面有这样几句 :
package org.apache.struts2.dispatcher.multipart;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.StrutsConstants;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
public class JakartaMultiPartRequest implements MultiPartRequest {
//下面略
在这个类中必须引入org.apache.commons.fileupload.*;
而错误也许没有加载这个文件上传包所以找不到类 。
于是我就把commons-fileupload-1.2.1.jar 和 commons-io-1.3.2.jar 导入,结果错误解决了。
我的异常网推荐解决方案:com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register,http://www.myexception.cn/j2ee/307924.html