0

I have a Java Bean as Below

  class Account 
  {
     private long id;
     private String userName;
     private String userId;

     //Getters, Setters for Above 
  }

   public List<Account> getAccountList()
   {    
      List<Account> accountList = new ArrayList();

      Account account;

      .
      .
      .
      //db code for fetching data's from database

      while(rs.next()) 
      {       
         account = new Account();

         account.setId(rs.getLong("Id"));
         account.setUserName(rs.getString("UserName"));
         account.setUserId(rs.getString("UserId"));

         accountList.add(account);
      }

      return accountList ;
  }

I am Assigning the List which I got in the function in a servlet and I am forwarding it to JSP page where I will display List of Users in Account.

List<Account> accountList = new ArrayList<Account>();
accountList = objdbUtil.getAccountList();
request.setAttribute("arrUsersList", accountList);          
RequestDispatcher rdst =  request.getRequestDispatcher("UserList.jsp");
rdst.forward(request, response);

Now how to display the Data's in JSP as the List contains collections in it.

Can I directly use below code

<c:forEach var="arrUsersList" items="${requestScope.arrUsersList}">
</c:forEach>
Java Beginner
  • 1,635
  • 11
  • 29
  • 51
  • yes you can, just make var='user' since its an item in your users list and not the list itself. Then to print the property you can just use ${user.userName} – jonasnas Feb 12 '13 at 14:46
  • 1
    And you don't need `requestScope.arrUsersList`, just `arrUsersList` will suffice. – Rohit Jain Feb 12 '13 at 14:47
  • Note that you can simply do: `List accountList = objdbUtil.getAccountList();`. Your code is creating an unnecessary object and throwing it away. – GriffeyDog Feb 12 '13 at 15:19

1 Answers1

1

You are on the right lines. The forEach tag will iterate through your list of Account objects.

<c:forEach var="account" items="${requestScope.arrUsersList}">
    <c:out value="${account.userName}" />
    ...
</c:forEach>
NickJ
  • 9,380
  • 9
  • 51
  • 74