Search code examples
javajspjsp-tagsdisplaytag

Display Tag not showing details in jsp


cant get the values of the bean in my display tag.. showing the below error

  String sql;
        List<AcceptBean> list = new ArrayList<AcceptBean>();

        PreparedStatement prest = (PreparedStatement) conn.prepareStatement(occQuery);
        ResultSet rs = prest.executeQuery(occQuery);

        System.out.println(rs);
        String first = "";
        String last = "";
        String vill = "";
        String son = "";
        String dist = "";
        while (rs.next()) {

           AcceptBean bean = new AcceptBean();

           bean.setFirstName(rs.getString("FirstName"));

           bean.setLastName(rs.getString("LastName"));
           bean.setVillage(rs.getString("Village"));
           bean.setSonOf(rs.getString("SonOf"));
           bean.setDistrict(rs.getString("District"));
           list.add(bean);
}

       // list.add(objBean);
        System.out.println("list vlues in servelet" + list.get(0));
        System.out.println("list detials inserted");
        request.setAttribute("list1", list);

        ServletContext context = getServletContext();
        RequestDispatcher rd = context.getRequestDispatcher("/Report.jsp");
        System.out.println("request dispatched");
        rd.forward(request, response);

this is my jsp code

    <display:table name="requestScope.list1"  >
               <display:column property="firstname" title="First Name"></display:column>
                <display:column property="lastname" title="Last Name"   ></display:column>
                <display:column property="sonof" title="Father's Name"  ></display:column>
                <display:column property="district" title="District"  ></display:column>
                <display:column property="village" title="Village"  ></display:column>
            </display:table>

error Exception: [.LookupUtil] Error looking up property "firstname" in object type "ColdStorage.AcceptBean". Cause: Unknown property 'firstname' on class 'class ColdStorage.AcceptBean'


Solution

    1. Of course. What would be the point of a list if you can't put objects in it? It should be declared and initialized as

      List<AcceptBean> list = new ArrayList<AcceptBean> 
      

      though.

    2. The issue is that you tell the tag to find the list in the request attribute named "list", but it's stored in a request attribute named "list1".

    3. Just as you did, except of course you should create a new AcceptBean inside the wile loop, and add it to the list inside the while loop. Otherwise, you'll only ever have one object in your list, containing the last row retrieved from the database:

      List<AcceptBean> list = new ArrayList<AcceptBean>();
      
      PreparedStatement prest = (PreparedStatement) conn.prepareStatement(occQuery);
      ResultSet rs = prest.executeQuery();
      while (rs.next()) {
          AcceptBean bean = new AcceptBean();
          bean.setFirstName(rs.getString("FirstName"));
          bean.setLastName(rs.getString("LastName"));
          bean.setVillage(rs.getString("Village"));
          bean.setSonOf(rs.getString("SonOf"));
          bean.setDistrict(rs.getString("District"));
          list.add(bean);
      }
      
      request.setAttribute("list1", list);
      
    4. No. Why don't you read the tag documentation? It explains how to use it, what the attributes are, and contains lots of examples.

    5. See above.