Struts2 Hello World jsp example error
Solution 1
Just add the following Jars only in the WEB-INF/lib. Remove all related jars if you don't have need.
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javaassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.7.jar
xwork-core-2.3.7.jar
If you dont have these jars then download it.
Here org.apache.struts2.dispatcher.FilterDispatcher is now deprecated .So Edit your web.xml from
<?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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Struts 2</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.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
to
<?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>Struts2 Application</display-name>
<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>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
I guess this could do the work for you.
Solution 2
It seems the version of xwork-core jar that you are using is not compatible with the versions of other jars.
Try using xwork-2.1.2.jar. I have used it earlier successfully.
If it still does not work, be sure you use the same versions of each and every jar as in the sample application.
EDIT:
As per your code, try changing
public String execute() throws Exception {
return "success";
}
to
public String execute() throws Exception {
setName("Honey Bunny");
return "success";
}
It seems that since you have not called the setter, Struts is searching for the property name in the session.
lulu88
Girl developer! From beautiful Cape Town in South Africa :)
Updated on June 04, 2022Comments
-
lulu88 almost 2 years
I am trying to run a Hello World Struts2 project in Tomcat 6.
I keep getting:
root cause: java.lang.ClassNotFoundException: org.apache.struts2.interceptor.SessionAware java.lang.NoClassDefFoundError: org/apache/struts2/interceptor/SessionAware at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:621) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124) at java.net.URLClassLoader.defineClass(URLClassLoader.java:260) at java.net.URLClassLoader.access$000(URLClassLoader.java:56) at java.net.URLClassLoader$1.run(URLClassLoader.java:195) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1374) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233) at com.opensymphony.xwork2.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:138) at com.opensymphony.xwork2.ObjectFactory.getClassInstance(ObjectFactory.java:96) at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:398) at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:355) at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:460) at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:265) at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111) at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:189) at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55) 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)
However I do have the required jars for struts2 in the WEB-INF/lib folder for the project.
Jars:
struts-core xwork ognl freemarker commons-fileupload commons-io commons-lang
Any ideas?
I followed an online Struts2 HelloWorld example:
HelloWorld.action class:
package za.co.vine.tutorialspoint.struts2; public class HelloWorldAction{ private String name; public String execute() throws Exception { return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
HelloWorld.jsp:
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Hello World</title> </head> <body> Hello World, <s:property value="name"/> </body> </html>
index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Hello World</title> </head> <body> <h1>Hello World From Struts2</h1> <form action="hello"> <label for="name">Please enter your name</label><br/> <input type="text" name="name"/> <input type="submit" value="Say Hello"/> </form> </body> </html>
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="helloworld" extends="struts-default"> <action name="hello" class="za.co.vine.tutorialspoint.struts2.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>
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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts 2</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.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
-
lulu88 over 11 yearsThanks for your reply :) I downloaded the full struts 2.3.8 package from Apache, but still don't work. I get a aused by: java.lang.ClassNotFoundException: org.apache.struts2.views.gxp.inject.InjectedObjectContainer at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387) at com.opensymphony.xwork2.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:152)at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:216) struts-core 2.3.8.jar xwork-core-2.3.8.jar
-
TechSpellBound over 11 yearsEdit the question to add the code from struts.xml, view.jsp, action class. Then we'll be able to tell.
-
TechSpellBound over 11 yearsSince you are trying to use the latest version of Struts, there might be dependency issues. Start afresh as try this app: dzone.com/tutorials/java/struts-2/struts-2-example/…
-
lulu88 over 11 yearsFollowed that D-Zone example and in my tomcat logs i still get an error:
code
java.lang.ClassNotFoundException: org.apache.struts2.interceptor.SessionAware -
lulu88 over 11 yearsDid that. I get this error in my tomcat logs: java.lang.ClassNotFoundException: org.apache.struts2.interceptor.SessionAware
-
TechSpellBound over 11 yearsDid you take this example with all the jars inside it: www.dzone.com/sites/all/files/Example1_1.zip Also, how do you add the jars to the project. If you are using Eclipse, select all jars -> right click -> Add to build path..
-
TechSpellBound over 11 yearsProbably, you have to run the Dzone example on tomcat 5.5. If that still does not work, try arvin_codeHunk's example below
-
lulu88 over 11 yearsYeah i did, i finally got it to work on tomcat 6! But I had to remove the lib folder from the project itself and put the jars i need in tomcat lib folder.
-
TechSpellBound over 11 yearsIf you don't realize the proper location of jars, "select all jars -> right click -> Add to build path" always helps
-
Java Nerd almost 9 yearsThanks for helping us! we are beginners and guys like you are really helping us. I am learning Struts2 by my own and facing the same problem as OP