setter和getter方法是Date類型 這是我的連接代碼:從MongoDB的Java來JSP入門日期顯示爲空
public void connectToDb() {
try {
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("bookingSystem");
bookingsCol = db.getCollection("bookings");
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
在蒙戈得到的值的代碼是:
"_id" : ObjectId("5820145df23fc5ccffd52155"),
"userID" : "1B",
"bookings" : [
{
"startDateTime" : ISODate("2017-08-22T08:00:00Z")
}
]
public Booking getBooking(int bookingID, String userID) {
connectToDb();
BasicDBObject query = new BasicDBObject();
query.put("bookings.bookingID", bookingID);
DBCursor cursor = bookingsCol.find(query);
while (cursor.hasNext()) {
DBObject theObj = cursor.next();
BasicDBList bookingsList = (BasicDBList) theObj.get("bookings");
for (int i = 0; i < bookingsList.size(); i++) {
BasicDBObject bookingObj = (BasicDBObject) bookingsList.get(i);
Date startDateTime = bookingObj.getDate("startDateTime");
booking.setStartDateTime(startDateTime);
}
}
return booking;
}
當我返回值,接收格式:星期五Sep 22 12:00:00 MUT 2017這是很好的,因爲它匹配我的輸入,但是當我做jsp的booking.getStartDateTime()。但是大小爲1.
在jsp中,getListOfBooking()是我預訂類中的一個函數,它返回一個對象數組,這就是爲什麼我將.size()知道它是否有價值。我試着用一個字符串來完成它,但它的工作原理與它顯示錯誤的日期有關。
<%
out.println(bookings.getListOfBooking().get(0).getStartDateTime());
out.println(bookings.getListOfBooking().size());
%>
知道錯誤將有所幫助... – evanchooly
當我想打印它顯示的值爲空。否則代碼中沒有錯誤。 – Agneysh