You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Tuan H. Le" <tu...@phsadc.com> on 2002/12/03 02:42:00 UTC

having a problem of passing the selected items to an action

Hi,

I'm having a problem of getting the selected checkbox values in my Struts action class. In the view, it has two multiboxes (approve and reject) and in my Struts form class I declared two String[] attributes to hold the selected checkbox values. But, when it gets to the action via html form submit, it returns empty String array. The checkbox selection works fine on the view.

Below is my code: View, ActionForm, Action class, and strut-config.xml.

Thanks for your time!

Tuan

------------- View (JSP) -----------------------------------------
<head>
  <title>Online Focal Tool Application</title>
  <link rel="stylesheet" type="text/css" href="common/styles/styleMid.css">
  <SCRIPT LANGUAGE="JavaScript">
   function check( e ) {
      e.checked = true;
   }

   function clear( e ) {
      e.checked = false;
   }

   function checkAll( selectedChkBoxName ) {
      // clear the other selections
      if ( selectedChkBoxName == 'approveList' ) {
        // clear reject check box list
        clearAll( 'rejectList' );
      } else {
        // clear approve check box list
        clearAll( 'approveList' );
      }
      // now, set the user's selections
      var listForm = top.frames['body'].approveSubmitForm;
      var len = listForm.elements.length;
      for ( var i = 0; i < len; i++ ) {
         var e = listForm.elements[i];
         if ( e.name == selectedChkBoxName ) {
            check( e );
         }
      }
   }

   function isNumeric (content) {
     if (((content/content) != 1) && (content != 0)) {
       return false;
     } else {
       return true;
     }
   }

   function clearAll( selectedChkBoxName ) {
      // now, un-select the user's selections
      var listForm = top.frames['body'].approveSubmitForm;
      var len = listForm.elements.length;
      for ( var i = 0; i < len; i++ ) {
         var e = listForm.elements[i];
         if ( e.name == selectedChkBoxName ) {
            clear( e );
         }
      }
   }

   function selectChoice( theForm, theField ) {
     var len = theForm.elements.length;
     for ( var i = 0; i < len; i++ ) {
       if ( theForm.elements[i].type == 'checkbox' ) {
         if ( theForm.elements[i].name != theField.name ) {
           if (( theForm.elements[i].value == theField.value) && theForm.elements[i].checked ) {
             clear( theForm.elements[i] );
             break;
           } else if ( theForm.elements[i].value == theField.value ) {
             // found a match, but the other checkbox is not selected, so
             // break out this loop
             break;
           }
         }
       }
     } // end for
   }
   </SCRIPT>
</head>


<html:form action="/approveSubmit"
           name="approveSubmitForm"
           type="com.phs.ezhr.presentation.form.ApproveSubmitForm"
           method="POST"
           target="_parent">

...

<logic:present name="employeeList" scope="session">
  <logic:iterate id="employee" name="employeeList" scope="session" type="com.phs.ezhr.business.vo.EmployeeVO" indexId="idx" offset="offset" length="25" >                
                <td>
                  <html:multibox property="approveList" value="<%=employeeId %>" onclick="selectChoice( this.form, this )">
                  </html:multibox>
                </td>
                <td>
                  <html:multibox property="rejectList" value="<%=employeeId %>" onclick="selectChoice( this.form, this )">
                  </html:multibox>
                 </td>

...
  </logic:iterate>
</logic:present>

...
</html:form>

---------------------- User Form class -----------------------------------------

public final class ApproveSubmitForm extends ActionForm {

  private String[] approveList;
  private String[] rejectList;

  /**
   * Set the approve checkbox list array
   * @param anApproveList
   */
  public void setApproveList( String[] anApproveList ) {
    this.approveList = anApproveList;
  }

  public String[] getApproveList() {
    return this.approveList;
  }

  /**
   * Set the reject checkbox list array
   * @param aRejectList
   */
  public void setRejectList( String[] aRejectList ) {
    this.rejectList = aRejectList;
  }

  public String[] getRejectList() {
    return this.rejectList;
  }

  /**
   * Reset method is run after every HTML submit and before the action
   * class takes over.
   * @param mapping
   * @param request
   */
  public void reset( ActionMapping mapping,
                     HttpServletRequest request ) {
  }

  /**
   * Validate method is run after every HTML submit and before the actio
   * class takes over.
   * @param mapping
   * @param request
   * @return
   */
  public ActionErrors validate( ActionMapping mapping,
                                HttpServletRequest request ) {
    ActionErrors errors = new ActionErrors();

    return errors;
  }
}



-------------- User Action class ----------------------


...

  public ActionForward perform( ActionMapping mapping,
                                ActionForm form,
		                    HttpServletRequest request,
                                HttpServletResponse response )
    throws IOException, ServletException {


          /**
           * get the user's approval selection list and process the request
           */
          String[] approveList = (String[])approveSubmitForm.getApproveList();
          if ( approveList != null && approveList.length > 0 ) {
           

          }

          /**
           * get the user's reject selection list and process the request
           */
          String[] rejectList = (String[])approveSubmitForm.getRejectList();
          if ( rejectList != null && rejectList.length > 0 ) {


          }
   ...
   }

}


------------------------struts-config.xml -----------------------------

...

  <form-beans>
    <form-bean name="approveSubmitForm" type="com.phs.ezhr.presentation.form.ApproveSubmitForm" />
  </form-beans>


    <!-- Process workflow status tab -->
    <action path="/approveSubmit"
            type="com.phs.ezhr.presentation.action.ApproveSubmitAction"
            validate="false"
            input="/MainMenu.jsp"
            name="approveSubmitForm"
            scope="request" >
            <set-property property="loginRequired" value="true" />
            <forward name="success" path="/MainMenu.jsp" />
            <forward name="failure" path="/login.do" />
    </action>

...