Action Mapping in Struts2

736 Views Asked by At

Currently migrating an app from Struts1 to Struts2. When I click my "submit" button, it gives me an error saying "There is no Action mapped for action name requestInput". What is wrong with my code that is the cause of this error?

web.xml

<web-app>
    <display-name>My Project</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>

    <!-- The Usual Welcome File List -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

struts.xml

<struts>
    <include file="struts-default.xml" />
    <constant name="struts.custom.i18n.resources" value="global" />
    
    <package name="default" namespace="/" extends="struts-default">
        <interceptors>
            <interceptor-stack name="uploadFile">
                <interceptor-ref name="fileUpload" />
                <interceptor-ref name="uploadForm" />
                <interceptor-ref name="modelDriven" />
                <interceptor-ref name="basicStack" />
            </interceptor-stack>
        </interceptors>
        

        <action name="requestInput" class="com.class.action.FileAddAction" method="execute">
            <result>/result.jsp</result>
        </action>


    </package>
</struts>

FileAdd.jsp

<s:form method="POST" action="requestInput" enctype="multipart/form-data">
...
<s:submit property="submit" style="background:#dccaa0" value="Submit" theme="simple"/>
...
</s:form>

FileAddAction.jsp

public final class FileAddAction extends ActionSupport implements SessionAware, ServletRequestAware {
...
public String execute() throws Exception{
...
}
}

And, my struts.xml is in the Java src folder and in the WEB-INF folder. What else can I do to fix this? Thanks..

1

There are 1 best solutions below

1
jumping_monkey On

Two issues, otherwise looks good.

First issue:

action name="requestInput" class="com.class.action.FileAddAction" method="execute"

You can't name your package with the identifier class, it is not a valid Java identifier


Second issue:

interceptor-ref name="uploadForm"

What is "uploadForm"? It's definitely not provided by Struts 2, see the list.

By naming the package differently, and removing interceptor-ref uploadForm, i am able to click the FileAdd.jsp Submit button without any issue and it is redirecting me successfully to result.jsp. I used v2.3.37.


Note:

  • struts.xml should be in the src/main/resources folder (struts.xml must be on the web application’s root class path). Check this out.
  • web.xml is to be under src/main/webapp/WEB-INF folder. Check this out.