2011-06-13 66 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"); 
相关问题