The requested resource is not available in struts2 when action class is configured in struts.xml

14,919

Actions in Struts2 are mapped to the URLs that are built from the configuration. When the url of the request match the action name and namespace then it is executed, otherwise error code 404 is returned by the dispatcher. Struts2 is implemented as a filter and it's looking for the requests that are mapped in it's configuration. For example if I want it to filter all URLs I will write in web.xml

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

If you say the error 404 on index page itself, then you should ask yourself which URL corresponds to it and what action is mapped via configuration to that URL. If you need more detailed picture of what configuration is and what URLs you could enter to execute an action install the config-browser plugin.

Share:
14,919
Pravin
Author by

Pravin

Updated on June 04, 2022

Comments

  • Pravin
    Pravin almost 2 years

    I've tried to create a new action class and add it to struts.xml, the action class successfully get compiled but when I run tomcat it shows The requested resource is not available 404 error on index page itself.

    CreateUser.jsp

    <s:form action = "CreateUserAction" >
        <s:textfield name = "name" label = "Name" />
        <s:textfield name = "userName" label = "User Name" />
        <s:submit value = "Register" />
    </s:form>
    

    CreateUserAction.java

    public String execute() {
        setMessage("Hello " + getUserName());
        return "SUCCESS";
    }
    

    Struts.xml

    <package name="default" extends="struts-default">
        <action name="CreateUserAction" class="com.ecommerce.action.CreateUserAction">
        <result name="SUCCESS">/success.jsp</result>
        </action>
    </package>