开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜