Search code examples
javajsptomcatstruts2

Struts 2 Hello World Tutorial Gives 404


I have been following this tutorial on Struts.

This is my file structure:

This is my file structure

I thought I followed everything step by step, I did a similar tutorial earlier and was able to get it to work in another version, but I'm trying Struts 2.3.33 now. I am running this on an Apache Tomcat 8.5 server. I try to go to http://localhost:8080/HelloWorldStruts2/index.jsp but it gives a 404 error message and I can't figure out why. I have also ensured that the Struts files are both in my build path and my deployment assembly as well as in my WEB-INF/lib folder.

I am getting no errors in my console, this is my web.xml file:

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

My struts.xml is as below:

<?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="com.tutorialspoint.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>
   </package>
</struts>

Solution

  • I followed that tutorial with your environment (Eclipse + Struts 2.3.33 + Tomcat 8.5.20) and it worked on Windows 10 using Java 1.8.

    Like you, I had to deviate from the tutorial by adding commons-lang3-3.2.jar to WEB-INF/lib to avoid a NoClassDefFoundError at runtime when using Struts 2.3.33. However, I see you also made two changes to web.xml that deviated from the tutorial:

    • Firstly, you specified version="2.5" where it should be version="3.0".

    • Secondly, you specified org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter instead of org.apache.struts2.dispatcher.FilterDispatcher for the filter-class.

    Given that the tutorial worked fine for me without modifying their web.xml, I suggest you revert your two changes and try again.

    What prompted you to modify their web.xml file?


    Updated response based on the OP's comment below:

    OK. One other deviation from the tutorial I just noticed is that you named your project Struts2.3 with Tiles 2 rather than HelloWorldStruts2. I doubt if your project knows anything about HelloWorldStruts2, which would explain why you are getting a 404.

    By default your context root will be Struts2.3_with_Tiles_2, not HelloWorldStruts2, so your URL needs to be changed to http://localhost:8080/Struts2.3_with_Tiles_2/index.jsp.

    You can see your project's Context Root by selecting your project, right clicking, choosing Properties from the popup menu, and then selecting Web Project Settings. Whatever you see in the Context Root field is what should be specified in your URL.

    So to clarify:

    • What exactly is the context root for your project?
    • What exactly are you specifying for the URL?

    If that doesn't resolve the issue I suggest running the tutorial again, but follow their instructions precisely (apart from the specific Struts 2 files you need to copy, but you are already doing that correctly).