简体   繁体   中英

struts2 - multiple submit buttons with separate action classes

I need to call different action classes for different submit buttons in a single form and get the value of a dropdown in struts2.

example for a schools student page:

  1. Submit Button 1: Show academic page
  2. Submit Button 2: Show sports page
  3. Submit Button 3: Show library page and so on.

I tried as follows but it does not work.

Code:

<s:submit value="academic" action="academic" />
<s:submit value="sports" action="sports" />
<s:submit value="library" action="library" />

Generated html code gets 
<input name="action:academic">
<input name="action:sports">

I am using theme="simple" in form tag.

I tried it by URL tag but the called action is not populating the dropdown fields value as roll no of student.

struts.xml:

`

<package name="default" extends="struts-default">
    <action name="academics" class="ViewAcademics" > 
        <result name="success">pages/ViewAcademics.jsp</result> 
    </action> 
    <action name="sports" class="ViewSports" > 
        <result name="success">pages/ViewSports.jsp</result> 
    </action> 
    <action name="library" class="ViewLibrary" > 
        <result name="success">pages/ViewLibrary.jsp</result> 
    </action>
</package>

`

JSP File:

<s:form name="inquiryForm" method="post" theme="simple" > 
   Select roll no : 
   <s:select style="width: 200px;" 
      list="#{'1':'Student 01', '2':'Student 02', '3':'Student 03', '4':'Student 04'}"
      id="projectName" 
      name="studentRollNo" 
      readonly="false" 
      headerKey="-1" 
      headerValue="--- Select ---" 
   /> 
   <s:submit cssClass="page" action="academics" value="academics" /> 
   <s:submit cssClass="page" name="sports" value="sports" /> 
   <s:submit cssClass="page" name="library" value="library" /> 
</s:form>

Use action attribute in your submit declarations:

<s:submit cssClass="page" action="academics" value="academics" /> 
<s:submit cssClass="page" action="sports" value="sports" /> 
<s:submit cssClass="page" action="library" value="library" />

And all your actions should have variable studentRollNo with getters/setters.

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