Search code examples
struts2http-status-code-404web.xml

Why index page is giving 404 error in Struts 2


If I am not including web.xml file then index file is opening properly but result page HelloWorld.jsp is giving 404 error, and when including web.xml index page is giving 404 error.

I have index.jsp file. localhost:8080 is working correctly but after that it is giving error.

See here:


enter image description here


Solution

  • In the first case the index file is opened by the default servlet, that is available on the server.

    In the second case you use FilterDispatcher that is deprecated.

    You should upgrade Struts to the latest version and use

    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
    

    See how to write web application descriptor web.xml.

    Simple Example

    Configuring web.xml for the framework is a matter of adding a filter and filter-mapping.

    FilterDispatcher Example (web.xml):

    <web-app id="WebApp_9" 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">
    
        <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>