2011-06-13 67 views
0

美好的一天!如何將JSP列表傳遞給ACtion類

我想將我的代碼轉換爲STRUTS ..並且我嘗試不使用我的Action類中的getParameter .. 但是我無法將信息從JSP傳遞到Action類而不使用getParameter。

JSP:

<html:form action="EditExam"> 
       <input type = "hidden" name = applicantNumber value="${applicantForm.applicantNumber}" > 

       <table> 
        <c:forEach var="exam" items="${examList}"> 
         <input type = "hidden" name ="examId" value="${exam.examId}" > 
         <tr> 
          <td>Exam Type: &nbsp</td>  <td><input type="text" value="${exam.examName}" name="examType" readonly ="true"></td> 
         </tr> 
         <tr> 
          <td>Date: </td>     <td><input type="text" value="${exam.examDate}" name="examDate" class="date"></td> 
         </tr> 
         <tr> 
          <td>Result: </td>    
          <td> 
           <select name = examResult> 
            <option value="Pass" ${exam.examResult == 'Pass' ? 'selected' : ''}>Pass</option> 
            <option value="Fail" ${exam.examResult == 'Fail' ? 'selected' : ''}>Fail</option> 
            <option value="" ${exam.examResult == '' ? 'selected' : ''}></option> 
           </select> 
          </td> 
         </tr> 
         <tr><td>&nbsp</td><td> &nbsp</td></tr> 
        </c:forEach> 
       </table> 

       <input type="submit" class="saveButton" value="SAVE"> 

      </html:form> 

Action類:

public ActionForward execute(ActionMapping mapping, ActionForm form, 
      HttpServletRequest request, HttpServletResponse response) 
      throws Exception { 
     // TODO Auto-generated method stub 

     String forward = "success"; 

     ApplicantForm applicantForm = (ApplicantForm)form; 
     int applicantNumber = applicantForm.getApplicantNumber(); 

     String examDate[] = request.getParameterValues("examDate"); 
     String examResult[] = request.getParameterValues("examResult"); 
     String examId[] = request.getParameterValues("examId"); 
      //MORE CODES AFTER... 

我的問題是: 我怎樣才能通過從JSP到Action類的數據,而無需使用的getParameter。

需要考慮:

  1. 我的考試是一個列表...
  2. ,編輯按鈕外循環......如此循環內的所有改變應該被捕獲。(我需要通過ArrayList?我怎樣才能趕上它的行動FOrm?)

你的答覆將不勝感激。謝謝。

+0

我的建議回答你的問題? – 2011-06-13 18:21:32

回答

2

你不能。您可以將數據從瀏覽器(html,jsp的結果)傳輸到使用HTTP協議的服務器,該協議僅傳輸文本請求參數。因此你必須使用request.getParameter[Values](..)。如果您需要List,則可以使用Arrays.asList(array)

我認爲struts應該有某種形式的綁定,所以無論你指定輸入參數,你都可以嘗試指定一個List,也許struts會填充它。 (但它仍然會在引擎蓋下使用request.getParameterValues(..)

2

HTML/JSP不理解Java對象(如列表)。它們只處理純字符串/數字或字節流。

所以,你必須使用

request.getParameter("paramName"); 

,或者如果你需要一張地圖,你可以使用

Map < String, String[] > queryParamsMap = (Map < String, String[] >)request.getParameterMap(); 

從地圖上看,你可以直接得到你的具體參數的數組。通過使用例如

String[] paramArray = queryParamsMap.get("myParam"); 
相關問題