显示检索到的数据(从RESTful服务)到表(客户端)

时间:2017-04-13 02:26:10

标签: java web-services rest java-ee jersey

我有2个项目文件夹。第一个是RESTful服务,另一个是客户端。我想做的是:

  • 从特定用户的RESTful服务(我将其作为列表)中获取所有注释。
  • 然后在表格(html)中向客户端显示备注。

当我尝试显示音符时,我收到以下错误:

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

HTML代码(我使用JSP)。错误出现在' forEach' 循环中:

<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>

RESTful服务代码:

@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();
    }

客户端servlet代码(post方法):

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);
    }

0 个答案:

没有答案