我的 searchUser 方法中有一个 arrayLists (personalList,c1List,c2List) 的 arrayList (mainList)。
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;
}
在我的 struts 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 次(取决于我在 mainList 中有 3 个还是 4 个 arrayLists。
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
你能告诉我我做错了什么吗?