简体   繁体   中英

Struts2 submit button method call doesn't fire

I am having problems with the below code, can anyone explain why the method may not be being fired on the jobListAction? 'Setup' is being called twice upon submission of the form. In short, I can't seem to get the struts button to call multiple methods. Any pointers / things to check?

public class JobListAction {
    public String execute() {
        System.out.println("setup");
    }

    public String deactivate() {
        System.out.println("called");
    }
   public String callonme()
   {
   }
} 

JSP:

<s:form id="recordsListForm" method="post" action="jobList">
 <s:submit type="button" action="deactivate" value="Deactivate Selected Jobs" method="deactivate" />
 <s:submit type="button" action="callonme" value="CallonMe" method="callonme" />

</s:form>

Struts.xml

    <!-- Job List -->
    <action name="jobList" class="JobListAction">
        <result name="input">/jsp/admin/jobList.jsp</result>
        <result name="success">/jsp/admin/jobList.jsp</result>
    </action>

    <!-- Job List - Deactivate Job -->
    <action name="deactivate" class="JobListAction" method="deactivate">
        <result name="input">/jsp/admin/jobList.jsp</result>
        <result name="success">/jsp/admin/jobList.jsp</result>
    </action>

    <action name="callonme" class="JobListAction" method="callonme">
        <result name="input">/jsp/admin/jobList.jsp</result>
        <result name="success">/jsp/admin/jobList.jsp</result>
    </action>

I guess in struts 2 u need to tell the method name in Struts.xml file, try that out, I hope it works...

<action name="jobList" class="JobListAction" method = "deactivate">
  <result name="input">/jsp/admin/jobList.jsp</result>
  <result name="success">/jsp/admin/jobList.jsp</result>
</action>

If you want to have a single action declaration that can call multiple methods in the same action class, look into using wilcard mappings :

View

<s:form id="recordsListForm" method="post" action="jobList">
   <s:submit type="button" action="jobList_deactivate" value="Deactivate Jobs" />
   <s:submit type="button" action="jobList_callonme" value="CallonMe" />
</s:form>

struts.xml

<!-- Job List -->
<action name="jobList_*" method="{1}" class="JobListAction">
    <result name="input">/jsp/admin/jobList.jsp</result>
    <result name="success">/jsp/admin/jobList.jsp</result>
</action>

The above mapping will match any action that starts with jobList_ and then use the rest of the match as the method to call in the JobListAction class.

Works fine for me; what version? Is dynamic method invocation enabled (it is by default)?

What do you mean by "call multiple methods?" You can only call a single method per-request.

My stdout:

setup // On initial form display
called // Clicking submit

Cut-and-pasted your code (more or less) verbatim.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM