Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SEVERE: Exception starting filter struts2 java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.FilterDispatcher

Tags:

java

struts2

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>
    <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:

<%@ 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:

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

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:\Java\jre6\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Java/jre6/bin/client;C:/Java/jre6/bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Java\jdk1.5\bin\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Liquid Technologies\Liquid XML Studio 2009\XmlDataBinder7\Redist7\cpp\win32\bin;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\MySQL\MySQL Server 5.0\bin
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
like image 895
Sanju Avatar asked Mar 10 '11 08:03

Sanju


2 Answers

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 (new for Struts versions 2.2.1 and higher)
  • commons-lang3-x.x

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

Try:

        <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>
like image 57
Quaternion Avatar answered Oct 22 '22 20:10

Quaternion


If you are using Struts2 version 2.5 you need to change from org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter to org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter. See:

java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter in web.xml

like image 44
yasin Avatar answered Oct 22 '22 20:10

yasin