2013-03-07 83 views
-1

我在我的searchUser方法中有arrayLists(personalList,c1List,c2List)的arrayList(mainList)。使用JSTL迭代ArrayList的ArrayList

public List<List<UserApplication>> searchUser(EdsStudentForm eds) throws Exception { 

    //My arraylist constructors  
    List<List<UserApplication>> mainList = new ArrayList<List<UserApplication>>(); 
    List<UserApplication> personalList = new ArrayList<UserApplication>(); 
    List<UserApplication> c1List = new ArrayList<UserApplication>(); 
    List<UserApplication> c2List = new ArrayList<UserApplication>(); 
    List<UserApplication> c3List = new ArrayList<UserApplication>(); 

    String searchPersonalQuery = "SELECT studentCode,title,firstName,lastName," 
      + "streetNumber,streetName,suite,city,province,country,postalCode,email," 
      + "workPhone,cellPhone,homePhone,sin " 
      + "FROM studtregister_personal WHERE studentCode=?"; 
    String searchCourse1Query = "SELECT programCode1,lang1,schDays1,schTime1,schType1," 
      + "totalHours1,startDate1,pricetype1,price1 " 
      + "FROM studtregister_course1 WHERE studentCode=?"; 
    String searchCourse2Query = "SELECT programCode2,lang2,schDays2,schTime2,schType2," 
      + "totalHours2,startDate2,pricetype2,price2 " 
      + "FROM studtregister_course2 WHERE studentCode=?"; 
    String searchCourse3Query = "SELECT programCode3,lang3,schDays3,schTime3,schType3," 
      + "totalHours3,startDate3,pricetype3,price3 " 
      + "FROM studtregister_course3 WHERE studentCode=?"; 

    int stCode = eds.getStudentCode(); 

    try { 
     currentCon = ConnectionManager.getDBConnection(); 
     PreparedStatement pst1 = currentCon.prepareStatement(searchPersonalQuery); 
     pst1.setInt(1, stCode); 
     rs = pst1.executeQuery(); 

     while (rs.next()) { 
      UserApplication personBean = new UserApplication(); 
      personBean.setStudentCode(rs.getInt(1)); 
      personBean.setTitle(rs.getString(2)); 
      personBean.setFirstName(rs.getString(3)); 
      personBean.setLastName(rs.getString(4)); 
      personBean.setStreetNumber(rs.getString(5)); 
      personBean.setStreetName(rs.getString(6)); 
      personBean.setSuite(rs.getString(7)); 
      personBean.setCity(rs.getString(8)); 
      personBean.setProvince(rs.getString(9)); 
      personBean.setCountry(rs.getString(10)); 
      personBean.setPostalCode(rs.getString(11)); 
      personBean.setEmail(rs.getString(12)); 
      personBean.setWorkPhone(rs.getString(13)); 
      personBean.setCellPhone(rs.getString(14)); 
      personBean.setHomePhone(rs.getString(15)); 
      personBean.setSin(rs.getString(16)); 
      personalList.add(personBean); 
     } 

     pst1.close(); 

     currentCon = ConnectionManager.getDBConnection(); 
     PreparedStatement pst2 = currentCon.prepareStatement(searchCourse1Query); 
     pst2.setInt(1, stCode); 
     rs = pst2.executeQuery(); 

     while (rs.next()) { 
      UserApplication C1Bean = new UserApplication(); 
      C1Bean.setProgramCode1(rs.getString(1)); 
      C1Bean.setLanguage1(rs.getString(2)); 
      C1Bean.setScheduleDay1(rs.getString(3)); 
      C1Bean.setScheduleTime1(rs.getString(4)); 
      C1Bean.setScheduleType1(rs.getString(5)); 
      C1Bean.setTotalHours1(rs.getString(6)); 
      C1Bean.setStartDate1(rs.getString(7)); 
      C1Bean.setPriceType1(rs.getString(8)); 
      C1Bean.setPrice1(rs.getString(9)); 
      c1List.add(C1Bean); 
     } 

     pst2.close(); 

     currentCon = ConnectionManager.getDBConnection(); 
     PreparedStatement pst3 = currentCon.prepareStatement(searchCourse2Query); 
     pst3.setInt(1, stCode); 
     rs = pst3.executeQuery(); 

     while (rs.next()) { 
      UserApplication C2Bean = new UserApplication(); 
      C2Bean.setProgramCode2(rs.getString(1)); 
      C2Bean.setLanguage2(rs.getString(2)); 
      C2Bean.setScheduleDay2(rs.getString(3)); 
      C2Bean.setScheduleTime2(rs.getString(4)); 
      C2Bean.setScheduleType2(rs.getString(5)); 
      C2Bean.setTotalHours2(rs.getString(6)); 
      C2Bean.setStartDate2(rs.getString(7)); 
      C2Bean.setPriceType2(rs.getString(8)); 
      C2Bean.setPrice2(rs.getString(9)); 
      c2List.add(C2Bean); 
     } 

     pst3.close(); 

     currentCon = ConnectionManager.getDBConnection(); 
     PreparedStatement pst4 = currentCon.prepareStatement(searchCourse3Query); 
     pst4.setInt(1, stCode); 
     rs = pst4.executeQuery(); 

     while (rs.next()) { 
      UserApplication C3Bean = new UserApplication(); 
      C3Bean.setProgramCode3(rs.getString(1)); 
      C3Bean.setLanguage3(rs.getString(2)); 
      C3Bean.setScheduleDay3(rs.getString(3)); 
      C3Bean.setScheduleTime3(rs.getString(4)); 
      C3Bean.setScheduleType3(rs.getString(5)); 
      C3Bean.setTotalHours3(rs.getString(6)); 
      C3Bean.setStartDate3(rs.getString(7)); 
      C3Bean.setPriceType3(rs.getString(8)); 
      C3Bean.setPrice3(rs.getString(9)); 
      c3List.add(C3Bean); 
     } 

     pst4.close(); 

     if(personalList != null && !personalList.isEmpty()) { 
      mainList.add(personalList); 
     } 
     if(c1List != null && !c1List.isEmpty()) { 
      mainList.add(c1List); 
     } 
     if(c2List != null && !c2List.isEmpty()) { 
      mainList.add(c2List); 
     } 
     if(c3List != null && !c3List.isEmpty()) { 
      mainList.add(c3List); 
     } 
    } catch (SQLException ee) { 
     System.out.println("Unable to connect to database:student_registration"); 
     ee.printStackTrace(); 
    } finally { 
     try { 
      if (rs != null) { 
       rs.close(); 
      } 
      if (currentCon != null) { 
       currentCon.close(); 
      } 
     } catch (SQLException sqle) { 
      sqle.printStackTrace(); 
     } 
    } 
    return mainList; 
} 

在我的支柱actionclass,我把mainList的ArrayList會話:

public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,    HttpServletResponse response) 
     throws Exception { 

    HttpSession session = request.getSession(true); 

    if (null == session.getAttribute("username")) { 
     response.sendRedirect("login.jsp"); 
    } else { 
     EdsStudentForm eds = (EdsStudentForm) form; 
     try { 
      List<List<UserApplication>> mainList = uaDAO.searchUser(eds); 
      if (!mainList.isEmpty()) { 
       request.getSession().setAttribute("mainList", mainList); 
       action_forward = EDITSUCCESS; 
      } else { 
       action_forward = EDITFAILURE; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    return mapping.findForward(action_forward); 
} 

然而,當我嘗試遍歷與下面的代碼我的JSP文件中的ArrayList:

<html:form method="post"> 
     <table> 
      <c:forEach items="${mainList}" var ="uList"> 
       <c:forEach items="${uList}" var="pList"> 
       <tr> 
        <td><label>Student Code : </label></td> 
        <td><a href="editStudt.jsp"><c:out value="${pList.studentCode}"></c:out></a></td> 
        </tr> 
        <tr> 
         <td><label>First Name : </label></td> 
         <td><c:out value="${pList.firstName}"></c:out></td> 
        </tr> 
        <tr> 
         <td><label>Last Name : </label></td> 
         <td><c:out value="${pList.lastName}"></c:out></td> 

        </tr> 
       </c:forEach> 
      </c:forEach> 
     </table> 
     <input type="button" class="button" value="Main Menu" onclick="doMenu()"/> 
    </html:form> 

我得到正確的信息打印出來... 3 - 4倍(取決於我是否有3或4 arrayList在mainList中。

The output : 

    Student code : 900 
    First Name : XYZ 
    Last Name : XYZ 

    Student code : 900 
    First Name : XYZ 
    Last Name : XYZ 

    Student code : 900 
    First Name : XYZ 
    Last Name : XYZ 

你能告訴我我做錯了什麼嗎?

+0

沒有更多的信息和更清晰的問題是不可能的。這可能是您的列表人口代碼不正確,也許您對內部列表使用了相同的列表引用:您看到的最新信息添加的信息是? – 2013-03-07 14:58:29

+0

我看到我的所有信息都正確打印出來,只是它打印出來了三次。 – javaStudent 2013-03-07 17:34:51

+0

沒有更多信息是不可能的。只打印出列表中的內容。 – 2013-03-07 17:35:26

回答

0

由於您添加了3個變量的第一個列表

0

你只UserApplication一個實例:

UserApplication sBean = new UserApplication(); 
while (rs.next()) { 
    ... 

您需要的結果來創建一個UserApplication每個項目設置:

while (rs.next()) { 
    UserApplication sBean = new UserApplication(); 
    ... 

這就是爲什麼我作爲ked關於多次顯示相同的數據,它是必須是如果這是實際的代碼。您所做的只是更改實例的數據,並將相同的豆重新添加到列表中。

+0

感謝您的回答。 – javaStudent 2013-03-07 18:28:57

+0

我的輸出是: – javaStudent 2013-03-07 18:29:20

+0

學生編號:900名:XYZ姓:XYZ學生編號:0名:(無)姓:(無)學生編號:0名:(無)姓:(無) – javaStudent 2013-03-07 18:31:18