0

EDIT: I can use the POJO example with the suggested 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,

enter image description here

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> &nbsp; <a
            href="javascript:void(0);" class="deleteLink" id="${user.userId}">Delete</a>
        </td>
    </tr>
</c:forEach>
3
  • 1
    Are you getting any errors, or is it just not working? In your DAO, you shouldn't need to do the loop if you're using POJO. That code just needs to be: ~~~ List<User> userList = new ArrayList<User>(); db.getCollection("Users", User.class).find().into(userList); ~~~ Commented Oct 8, 2020 at 12:33
  • The POJO I'm using just referenced the quick start guide to add relevant script to the dbUtils. Knowing that there is annotations to help in the entity, but I'm afraid to use that at this stage. Is it require to add the annotations to make the POJO works? Commented Oct 8, 2020 at 12:59
  • No, it's not required, but would keep your code cleaner. Also, you didn't say whether you're getting any errors and if so, what you're getting. Commented Oct 8, 2020 at 22:02

2 Answers 2

2

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]
Sign up to request clarification or add additional context in comments.

4 Comments

Instead, you output the to the console, for (User u : userList) { System.out.println(u.toString()); } how you hold it in the return method? Thanks
In the first case, return (userList); In the second case, you'll need to create the List<User> userList and add each "user" that you create in the for (Document...) loop, then add return (userLIst);
Can you add some debug around the code to output to the catalina.out log file? See if you can display any of the user details before you try to pass them back to the client. Are you still using the loop, or are you using the POJO method, specifying the User.class in the getCollection call?
I don't know much debug on eclipse for the list user method as I only found "debug on server" mode, which can't let me "step into" to see every line of the process. Besides, I'm using your POJO method. am now update the question to add the Catalina log file now, thanks and highly appreciated for your help!
1

The reason I can't show the user list because I forgot put the database connection to my controller that different from people used to do with JPA annotation and add a JPA layer on it.

response.setContentType("text/html");
MongoDB.getMongoDB();

enter image description here

Especially thanks Spuggiehawk for the suggested answer to write less code during a hard time debuggin.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.