Issue
EDIT: I can use the POJO example with the suggested href="https://stackoverflow.com/questions/64261496/using-mongodb-driver-pojos-with-java?answertab=votes#tab-top">post to output a list of users in the console, but no Object id
as shown below. Is that the problem causing output not showing to the JSP
page?
User [[email protected], fullName=Didi Dee, password=asdfwle]
User [[email protected], fullName=Lucy Liu, password=lalla]
What I expect the result like below, that show user details under each heading on the browser,
Here is the new method to list user in DAO class
@Override
public List<User> listAll() {
List<User> userList = new ArrayList<User>();
database.getCollection("User", User.class).find().into(userList);
for (User u : userList) {
System.out.println(u.toString());
}
return userList;
}
My service class
public List<User> listUser() {
List<User> userList = userDAO.listAll();
return userList;
}
The Controller
UserService userService = new UserService(request, response);
List<User> userList = userService.listUser();
request.setAttribute("userList", userList); // for jsp to get Attribute
String list_user_page = "user_list.jsp";
RequestDispatcher rd = request.getRequestDispatcher(list_user_page);
rd.forward(request, response);
The JSP to show the output.
<c:forEach items="${userList}" var="user" begin="1">
<tr>
<td>${user.userId}</td>
<td>${user.email}</td>
<td>${user.fullName}</td>
<td><a href="edit_user?id=${user.userId}">Edit</a> <a
href="javascript:void(0);" class="deleteLink" id="${user.userId}">Delete</a>
</td>
</tr>
</c:forEach>
Solution
Assuming the following:
You've included
CodecRegistry pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build()));
Your database contains records in the following format:
> db.User.find().pretty()
{
"_id" : ObjectId("5f7f92c4d91d2c38583dbfba"),
"fullname" : "Stu Dent",
"email" : "[email protected]",
"password" : "passWord"
}
{
"_id" : ObjectId("5f7f9497d91d2c38583dbfbb"),
"fullname" : "A. N. Other",
"email" : "[email protected]",
"password" : "strongerPassWord_1"
}
User is defined as a bean like this:
public class User
{
private String fullname;
private String email;
private String password;
public User()
{
// Default bean constructor
}
public String getFullname()
{
return fullname;
}
public void setFullname(String fullname)
{
this.fullname = fullname;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
@Override
public String toString()
{
return "User [fullname=" + fullname + ", email=" + email + ", password=" + password + "]";
}
}
The following code will read the contents of the database into the userList List:
List<User> userList = new ArrayList<User>();
db.getCollection("User", User.class).find().into(userList);
To output the list:
for (User u : userList)
{
System.out.println(u.toString());
}
Result:
User [fullname=Stu Dent, [email protected], password=passWord]
User [fullname=A. N. Other, [email protected], password=strongerPassWord_1]
If you insist on looping through the individual records, then I would suggest:
FindIterable<Document> userTbl = db.getCollection("User").find();
for (Document doc: userTbl)
{
User user = new User();
user.setFullname(doc.getString("fullname"));
user.setEmail(doc.getString("email"));
user.setPassword(doc.getString("password"));
System.out.println("User = " + user.toString());
}
Which produces:
User = User [fullname=Stu Dent, [email protected], password=passWord]
User = User [fullname=A. N. Other, [email protected], password=strongerPassWord_1]
Answered By - Spuggiehawk
Answer Checked By - Cary Denson (JavaFixing Admin)