【发布时间】:2015-04-15 13:52:23
【问题描述】:
我无法在 JSP 上显示我放入 ArrayList 中的内容。列表中的项目取自 Java 类中的数据库,然后显示在 JSP 中。我试过使用 c:forEach 但我仍然没有得到那么多。我是 JSP 的新手,我试图不使用这些脚本。这就是为什么我在我的 Java 类上做所有事情的原因。此外,为了让您了解它的工作原理,管理员选择查看用户的整个数据库,因此它选择选项单选按钮标签,进入 JSP 页面,在该页面评估管理员想要查看的内容并转发到必须显示 Arraylist 的其他页面。我在 JSP 页面中调用 List 的函数,该函数转发到显示数据库的 JSP。我不知道那是不是正确的方法。谢谢!
这是 JAVA 类
public List<administration> fullList() throws Exception{
List<administration> result = new ArrayList<administration>();
try{
Class.forName("com.mysql.jdbc.Driver");
connectMe = DriverManager.getConnection(url+dbName, userNameDB, passwordDB);
String query = "SELECT \n" +
" ControlAccess.UserName, \n" +
" ControlAccess.Pass, \n" +
" Users.First_Name,\n" +
" Users.Last_Name, \n" +
" UserInfo.Age, \n" +
" UserInfo.Country,\n" +
" UserInfo.Address,\n" +
" UserInfo.ZipCode,\n" +
" Sessions.Matrix1,\n" +
" Sessions.Matrix2,\n" +
" Sessions.Result,\n" +
" FilePath.LocationFiles\n" +
" FROM MatrixUsers.UserInfo \n" +
" INNER JOIN MatrixUsers.Users\n" +
" ON UserInfo.idUserInfo = Users.idUsers\n" +
" INNER JOIN MatrixUsers.ControlAccess \n" +
" ON ControlAccess.idControlAccess = UserInfo.idUserInfo\n" +
" INNER JOIN MatrixUsers.Sessions \n" +
" ON Sessions.idSessions = ControlAccess.idControlAccess\n" +
" INNER JOIN MatrixUsers.FilePath \n" +
" ON FilePath.idFilePath = Sessions.idSessions";
selectUsers = connectMe.prepareStatement(query);
selectUsers.executeQuery();
while(results.next()) {
administration admin = new administration();
admin.setUserName(results.getString("UserName"));
admin.setPassword(results.getString("Pass"));
admin.setFirstname(results.getString("First_Name"));
admin.setLastname(results.getString("Last_Name"));
admin.setAge(results.getInt("Age"));
admin.setCountry(results.getString("Country"));
admin.setAddress(results.getString("Address"));
admin.setZipcode(results.getInt("ZipCode"));
admin.setMatrix1(results.getString("Matrix1"));
admin.setMatrix2(results.getString("Matrix2"));
admin.setResult(results.getString("Result"));
admin.setLocation(results.getString("LocationFiles"));
result.add(admin);
}
results.close();
connectMe.close();
}catch(Exception e) {
e.printStackTrace();
}
return result;
}
这就是我试图在 JSP 中显示的方式:
<table>
<c:forEach var="admin" items="${admin.fullList()}">
<tr>
<td>${admin.username}" </td>
<td>${admin.password} </td>
<td>${admin.firstName} </td>
<td>${admin.lastName} </td>
<td>${admin.age} </td>
<td>${admin.country} </td>
<td>${admin.address} </td>
<td>${admin.zipcode} </td>
<td>${admin.Matrix1} </td>
<td>${admin.Matrix2} </td>
<td>${admin.Result} </td>
<td>${admin.location} </td>
</tr>
</c:forEach>
</table>
【问题讨论】:
-
您正在返回结果但访问管理员!!!
-
所以我要把管理员改成结果?另外我注意到在 Java 类中我有 result.next 但我没有为结果分配任何东西,我的错误!
-
still dont get that much没有显示所有数据是什么意思? -
对不起,我的意思是它只显示标题但没有表格。现在我也在编辑代码,如果我把 Administration.fullList();在我的前向 JSP 中,我收到一条错误消息 javax.el.PropertyNotFoundException: Property 'firstName' not found on type matrixcalculator.administration
-
您忘记将结果列表分配给具体的
java.sql.ResultSet这里selectUsers.executeQuery();。你需要这个results=selectUsers.executeQuery();。我假设results是java.sql.ResultSet的一种。您似乎已将java.sql.ResultSet声明为完全错误的类成员。在尽可能短的范围内声明和使用Connection、ResultSet和Statement/PreparedStatement(并在finally块中精确地关闭它们)。