严重:异常启动过滤器 struts2 java.lang.ClassNotFoundException:org.apache.struts2.dispatcher.FilterDispatcher

2022-01-16 00:00:00 java struts2

我正在尝试在 struts 2 中制作一个小型登录应用程序.我的 web.xml:

I am trying to make a small login application in struts 2. My web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

    <display-name>StrutsPrj</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>Login.jsp</welcome-file>
    </welcome-file-list>
    </web-app>

Struts.xml:

Struts.xml:

<struts>
    <package name="default" extends="struts-default" namespace="/">
        <action name="login" method="execute"
            class="struts2.LoginAction">
            <result name="success">Welcome.jsp</result>
            <result name="error">Login.jsp</result>
        </action>
    </package>
</struts>    

login.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Screen - Struts 2</title>
</head>
<body>

<s:actionerror />
    <s:form action="login.action" method="post">
        <s:textfield name="username" value="UserName" size="20" />
        <s:password name="password" value="Password" size="20" />
        <s:submit method="execute" value="Login" align="center" />
    </s:form>
</body>
</html>

Welcome.jsp:

Welcome.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Screen - Struts 2</title>
</head>
<body>
<h2>Congrates, <s:property value="username" />...!</h2>
</body>
</html>

LoginAction.java:

LoginAction.java:

package struts2;

public class LoginAction {
    private String username;
    private String password;

    public String execute() {
        if (this.username.equals("admin")
            && this.password.equals("admin123")) {
            return "success";
        } else {
        return "error";
        }
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

我已将这些库添加到我的项目中:

And i have added these libraries to my project:

  • commons-logging-1.0.4.jar
  • struts2-core-2.1.8.1.jar
  • ognl-2.6.11.jar
  • xwork-2.1.0.jar
  • freemarker-2.3.9.jar

当我尝试在 Tomcat 6 上运行它时,出现以下错误:

When I try to run this on Tomcat 6 I got following error:

Mar 10, 2011 1:17:59 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:Javajre6in;.;C:WINDOWSSunJavain;C:WINDOWSsystem32;C:WINDOWS;C:/Java/jre6/bin/client;C:/Java/jre6/bin;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:Javajdk1.5in;C:Program FilesTortoiseSVNin;C:Program FilesLiquid TechnologiesLiquid XML Studio 2009XmlDataBinder7Redist7cppwin32in;C:Program FilesQuickTimeQTSystem;C:Program FilesMySQLMySQL Server 5.0in
Mar 10, 2011 1:17:59 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:newPrj' did not find a matching property.
Mar 10, 2011 1:17:59 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context/Loader} Setting property 'useSystemClassLoaderAsParent' to 'false' did not find a matching property.
Mar 10, 2011 1:17:59 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:StrutsPrj' did not find a matching property.
Mar 10, 2011 1:17:59 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context/Loader} Setting property 'useSystemClassLoaderAsParent' to 'false' did not find a matching property.
Mar 10, 2011 1:18:00 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Mar 10, 2011 1:18:00 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 2037 ms
Mar 10, 2011 1:18:00 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Mar 10, 2011 1:18:00 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.32
Mar 10, 2011 1:18:01 PM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter struts2
java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.FilterDispatcher
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:269)
    at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4071)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4725)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    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(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Mar 10, 2011 1:18:01 PM org.apache.catalina.core.StandardContext start
SEVERE: Error filterStart
Mar 10, 2011 1:18:01 PM org.apache.catalina.core.StandardContext start
SEVERE: Context [/StrutsPrj] startup failed due to previous errors
Mar 10, 2011 1:18:01 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Mar 10, 2011 1:18:01 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Mar 10, 2011 1:18:01 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/97  config=null
Mar 10, 2011 1:18:01 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1756 ms

推荐答案

确保您的类路径上有以下内容:

Make sure the following are on your class path:

  • commons-fileupload-X.X.X.jar
  • commons-io-X.X.X.jar
  • commons-logging-X.X.X.jar
  • commons-logging-api.X.X.jar
  • freemarker-X.X.X.jar
  • ognl-X.X.X.jar
  • struts2-core-X.X.X.X.jar
  • xwork-core-X.X.X.jar
  • javassist-3.7.ga.jar(Struts 2.2.1 及更高版本的新功能)
  • commons-lang3-x.x

您是否关注过 https://struts.apache.org/getting-started/how-to-create-a-struts2-web-application.html 来设置你的项目?

Have you followed https://struts.apache.org/getting-started/how-to-create-a-struts2-web-application.html to set up your project?

试试:

        <filter>
            <filter-name>action</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>action</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

相关文章