java.lang.ClassNotFoundException:启动Simple Struts2应用程序时的org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter [英] java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter when starting Simple Struts2 Application
问题描述
我正在尝试运行一个简单的Struts2应用程序,但总是会抛出以下异常:
java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
我已经按照一步一步的教程在 http://struts.apache.org/development/2.x/docs/create-struts-2-web-application-using-maven-to-manage-artifacts-和-to-build-the-application.html ,但我无法使其工作。
我也设法找到其他使用maven但是都失败的Struts2教程。我尝试运行应用程序时会抛出同样的异常。
我使用了以下内容:
- Eclipse
- Maven
- Tomcat
- Struts2
根据其他讨论,依赖项jar文件(例如struts2-core)必须放在lib文件夹上,但对于我来说,我使用Maven,所以所有的依赖已经包含在我的Maven依赖关系下的构建路径中。
我甚至尝试运行Struts2网站中提供的示例项目( Basic_Struts_2_Maven_2_3_1_2.zip ),但我还是没有运气。构建它是成功的,但在服务器上运行它总是失败。
我是否缺少任何东西?
这里是 web.xml
:
<?xml version =1.0 encoding =UTF-8?>
< web-app id =WebApp_IDversion =2.4xmlns =http://java.sun.com/xml/ns/j2eexmlns:xsi =http://www.w3 .org / 2001 / XMLSchema-instancexsi:schemaLocation =http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd >
< display-name> Basic_Struts2_Maven< / display-name>
< welcome-file-list>
< welcome-file> index.jsp< / welcome-file>
< / welcome-file-list>
< 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> / *< / url-pattern>
< / filter-mapping>
< / web-app>
pom.xml
/ strong>
< project xmlns =http://maven.apache.org/ POM / 4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http://maven.apache.org/POM/4.0。 0 http://maven.apache.org/maven-v4_0_0.xsd\">
< modelVersion> 4.0.0< / modelVersion>
< groupId> manning< / groupId>
< artifactId> Basic_Struts2_Mvn< / artifactId>
< packaging> war< / packaging>
< version> 0.0.1-SNAPSHOT< / version>
< name> Basic Struts 2< / name>
<属性>
< project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding>
< / properties>
< build>
< finalName> Basic_Struts2_Mvn< / finalName>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-compiler-plugin< / artifactId>
< version> 2.3.2< / version>
< configuration>
< source> 1.6< / source>
< target> 1.6< / target>
< / configuration>
< / plugin>
< / plugins>
< / build>
<依赖关系>
<依赖关系>
< groupId> org.apache.struts< / groupId>
< artifactId> struts2-core< / artifactId>
< version> 2.3.1.2< / version>
< / dependency>
<依赖关系>
< groupId> log4j< / groupId>
< artifactId> log4j< / artifactId>
< version> 1.2.14< / version>
< / dependency>
< / dependencies>
< / project>
struts.xml
/ strong>
<?xml version =1.0encoding =UTF-8 ?>
<!DOCTYPE struts PUBLIC
- // Apache Software Foundation // DTD Struts Configuration 2.0 // EN
http://struts.apache.org/dtds/struts- 2.0.dtd>
< struts>
< constant name =struts.devModevalue =true/>
< package name =basicstruts2extends =struts-default>
< action name =index>
< result> /index.jsp< / result>
< / action>
< / package>
< / struts>
.classpath
strong>
<?xml version =1.0encoding =UTF-8? >
< classpath>
< classpathentry kind =srcoutput =target / classespath =src / main / java/>
< classpathentry exclud =**kind =srcoutput =target / classespath =src / main / resources/>
< classpathentry kind =srcoutput =target / test-classespath =src / test / java/>
< classpathentry exclud =**kind =srcoutput =target / test-classespath =src / test / resources/>
< classpathentry kind =conpath =org.eclipse.jdt.launching.JRE_CONTAINER / org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType / J2SE-1.5/>
< classpathentry kind =conpath =org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER/>
< classpathentry kind =conpath =org.eclipse.jst.j2ee.internal.web.container/>
< classpathentry kind =conpath =org.eclipse.jst.j2ee.internal.module.container/>
< classpathentry kind =outputpath =target / classes/>
< / classpath>
使用此命令将Maven项目转换为Dynamic Web Project对于Eclipse:
mvn eclipse:eclipse -Dwtpversion = 3.4.2
pre>
您可以更改任何使用的WTP版本
.classpath
应该是这样的< classpath>
< classpathentry kind =srcpath =src / main / javainclude =** / *。java/>
< classpathentry kind =srcpath =src / main / resourcesexclud =** / *。java/>
< classpathentry kind =outputpath =target / classes/>
< classpathentry kind =varpath =M2_REPO / asm / asm / 3.3 / asm-3.3.jar/>
< classpathentry kind =varpath =M2_REPO / asm / asm-commons / 3.3 / asm-commons-3.3.jar/>
< classpathentry kind =varpath =M2_REPO / asm / asm-tree / 3.3 / asm-tree-3.3.jar/>
< classpathentry kind =varpath =M2_REPO / commons-fileupload / commons-fileupload / 1.2.2 / commons-fileupload-1.2.2.jar/>
< classpathentry kind =varpath =M2_REPO / commons-io / commons-io / 2.0.1 / commons-io-2.0.1.jar/>
< classpathentry kind =varpath =M2_REPO / commons-lang / commons-lang / 2.5 / commons-lang-2.5.jar/>
< classpathentry kind =varpath =M2_REPO / org / freemarker / freemarker / 2.3.18 / freemarker-2.3.18.jar/>
< classpathentry kind =varpath =M2_REPO / javassist / javassist / 3.11.0.GA / javassist-3.11.0.GA.jar/>
< classpathentry kind =varpath =M2_REPO / junit / junit / 3.8.1 / junit-3.8.1.jar/>
< classpathentry kind =varpath =M2_REPO / ognl / ognl / 3.0.4 / ognl-3.0.4.jar/>
< classpathentry kind =varpath =M2_REPO / org / apache / struts / struts2-core / 2.3.1.2 / struts2-core-2.3.1.2.jar/>
< classpathentry kind =libpath =C:/ Program Files / Java / jdk1.6.0_32 / lib / tools.jar/>
< classpathentry kind =varpath =M2_REPO / org / apache / struts / xwork / xwork-core / 2.3.1.2 / xwork-core-2.3.1.2.jar/>
< classpathentry kind =conpath =org.eclipse.jdt.launching.JRE_CONTAINER/>
< / classpath>
构建WAR文件后,应部署到
%CATALINA_BASE%\webapps
。I am trying to run a simple Struts2 application but it always throw the following exception:
java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
I have followed the step by step tutorial in http://struts.apache.org/development/2.x/docs/create-struts-2-web-application-using-maven-to-manage-artifacts-and-to-build-the-application.html but I cannot make it work. I also managed to find other Struts2 tutorial which uses maven but all failed. It throws the same exception when I try to run the application.
I used the following:
- Eclipse
- Maven
- Tomcat
- Struts2
According to other discussions, the dependencies jar files(e.g. struts2-core) must be put on the lib folder but as for me, I am using Maven so all the dependencies are already included in my build path under Maven Dependencies.
I even tried running the sample project given in Struts2 website (Basic_Struts_2_Maven_2_3_1_2.zip) but I still have no luck. Building it is successful but running it on server always fail.
Am I missing anything?
Here's the web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Basic_Struts2_Maven</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>/*</url-pattern>
</filter-mapping>
</web-app>
pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>manning</groupId>
<artifactId>Basic_Struts2_Mvn</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Basic Struts 2</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>Basic_Struts2_Mvn</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
</project>
struts.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="basicstruts2" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
</package>
</struts>
.classpath
:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Use this command to convert Maven project to Dynamic Web Project for Eclipse:
mvn eclipse:eclipse -Dwtpversion=3.4.2
you may change whatever WTP version you use
.classpath
should be like this
<classpath>
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
<classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="var" path="M2_REPO/asm/asm/3.3/asm-3.3.jar"/>
<classpathentry kind="var" path="M2_REPO/asm/asm-commons/3.3/asm-commons-3.3.jar"/>
<classpathentry kind="var" path="M2_REPO/asm/asm-tree/3.3/asm-tree-3.3.jar"/>
<classpathentry kind="var" path="M2_REPO/commons-fileupload/commons-fileupload/1.2.2/commons-fileupload-1.2.2.jar" />
<classpathentry kind="var" path="M2_REPO/commons-io/commons-io/2.0.1/commons-io-2.0.1.jar"/>
<classpathentry kind="var" path="M2_REPO/commons-lang/commons-lang/2.5/commons-lang-2.5.jar"/>
<classpathentry kind="var" path="M2_REPO/org/freemarker/freemarker/2.3.18/freemarker-2.3.18.jar"/>
<classpathentry kind="var" path="M2_REPO/javassist/javassist/3.11.0.GA/javassist-3.11.0.GA.jar"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
<classpathentry kind="var" path="M2_REPO/ognl/ognl/3.0.4/ognl-3.0.4.jar"/>
<classpathentry kind="var" path="M2_REPO/org/apache/struts/struts2-core/2.3.1.2/struts2-core-2.3.1.2.jar"/>
<classpathentry kind="lib" path="C:/Program Files/Java/jdk1.6.0_32/lib/tools.jar"/>
<classpathentry kind="var" path="M2_REPO/org/apache/struts/xwork/xwork-core/2.3.1.2/xwork-core-2.3.1.2.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
</classpath>
After build the WAR file should deploy to %CATALINA_BASE%\webapps
.
这篇关于java.lang.ClassNotFoundException:启动Simple Struts2应用程序时的org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!