0

I have 2 project folders. The first one is a RESTful service and the other one is a client. What I'm trying to do is:

  • Getting all notes from the RESTful service (I do this as a list) of a specific user.
  • Then display the notes to the client in a table (html).

When I try to display the notes I get the following error:

javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>

The HTML code (I'm using JSP). The error appears at the 'forEach' loop:

<table class="table table-striped">
        <thead>
            <tr>
                <!-- Here we create the columns -->
                <th> Id </th>
                <th> Title </th>
                <th> Text </th>
                <th> Color </th>
                <th> Date/Time </th>
                <th> Actions </th> <!-- the table header for Actions -->
            </tr>
        </thead>

        <!-- Table body - The data in the table -->
        <tbody>

        <c:forEach items="${note-all}" var="pp">
                <tr>
                    <td><c:out value="${pp.id}" /></td>
                    <td><c:out value="${pp.title}" /></td>
                    <td><c:out value="${pp.text}" /></td>
                    <td><c:out value="${pp.color}" /></td>
                    <td><c:out value="${pp.datetime}" /></td>
                    <!-- The final column is the Actions, which is a list of buttons,
                    that we can perfom on our note Entities. -->
                    <td>
                        <div class="btn-group">

                            <!-- ***** Edit Button ***** -->
                            <a href="@Url.Action("Edit", new {pp.id})" class="btn btn-xs btn-primary">
                                <i class="glyphicon glyphicon-edit"></i>
                                Edit
                            </a>

                            <a href="@Url.Action("Delete", new {pp.id})" class="btn btn-xs btn-danger" data-post="Are you sure you want to delete this?">
                                <i class="glyphicon glyphicon-remove"></i>
                                Delete
                            </a>

                        </div>
                    </td>

                </tr>
        </c:forEach>

        </tbody>
    </table>

The RESTful service code:

@Path("/getAll")
    @POST
    @Consumes({MediaType.APPLICATION_FORM_URLENCODED/})
    @Produces({MediaType.APPLICATION_XML})
    public Response login(@FormParam("username") String uname
            ) throws ClassNotFoundException, SQLException{


        System.out.println(uname);

        //*First*: We get the id of the user 
        String sql = "SELECT user_id "
                + " FROM user_table "
                + " WHERE username = ?";

        PreparedStatement ps = DbCon.getPreparedStatement(sql);
        ps.setString(1, uname);
        ResultSet rs = ps.executeQuery();
        String id = "";

        if(rs.next()){
            id = rs.getString("user_id");
        }

        //*Second*: We get the users notes
        String sql2 = "SELECT * "
                + " FROM notes_table "
                + " WHERE user_id_fk = ?";

        PreparedStatement ps2 = DbCon.getPreparedStatement(sql2);
        ps2.setString(1, id);
        ResultSet rs2 = ps2.executeQuery();

        ArrayList<Note> note_AL = new ArrayList<Note>();

        if(rs2.next()){
            Note note = new Note();
            note.setId(rs2.getInt("note_id"));
            note.setTitle(rs2.getString("title"));
            note.setText(rs2.getString("text"));
            note.setColor(rs2.getString("color"));
            note.setDate(rs2.getString("datetime"));

            note_AL.add(note);
        }

        //we wrap the ArrayList with Generic ENtity
        GenericEntity<ArrayList<Note>> generic_list_of_notes = new GenericEntity<ArrayList<Note>>(note_AL){};

    return Response.ok(generic_list_of_notes).build();
    }

The client servlet code (the post method):

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Form form = new Form();
        form.add("username", "ali");


        //We create a client object
        Client client = Client.create();

        //We create a resource object and pass a url to it
        WebResource webR = client.resource("http://localhost:8080/MyNote/api/notes/getAll");

        ClientResponse resp = webR.accept(MediaType.APPLICATION_XML/*"text/html"*/).post(ClientResponse.class, form);

        //for debug
        if (resp.getStatus() != 200){
             System.err.println("Unable to connect to the RESTFUL web service");
        }

        List<Note> output = resp.getEntity(new GenericType<List<Note>>(){});

        request.setAttribute("note-all", output);


        RequestDispatcher rd = request.getRequestDispatcher("/Notes.jsp");
        rd.forward(request, response);
    }
Yoseph
  • 121
  • 1
  • 5
  • 12
  • well it looks like the result of the REST call is XML – Scary Wombat Apr 13 '17 at 02:30
  • Can you print out the generic_list_of_notes ? – Fady Saad Apr 13 '17 at 02:36
  • It's very odd what you are trying to do. You should just switch over to Jersey 2.x and use it's [MVC support](https://jersey.java.net/documentation/latest/mvc.html) to serve up the JSP pages. See [example](http://stackoverflow.com/a/31900846/2587435) – Paul Samsotha Apr 13 '17 at 02:56
  • if I add @XmlRootElement to the Note class, then I will be able to print the following out from the list "Model.Note@4d9f141c". – Yoseph Apr 13 '17 at 03:15
  • Is there a way I can make this work through what I have done? – Yoseph Apr 13 '17 at 03:15

0 Answers0