【问题标题】:How to display String of Json in jsp table如何在jsp表中显示Json字符串
【发布时间】:2017-09-09 22:46:01
【问题描述】:

我到处找遍了,但找不到我需要的帮助。

在我的 servlet 中,我收到一个包含 JSON 数据的字符串。这些数据是来自数据库的行,包含以下内容:

[{"note_id":1,"title":"Homework","text":"Math ex. 15, 16, 17.","color":"Yellow","datetime":""}]

我的问题是我无法使用 jstl 在 html 表上显示这些数据。

这是我得到的:

(我压力很大,不知道如何解决这个问题)。

Servlet 代码(post 方法):

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    .
    .
    .
    String output = resp.getEntity(String.class);
    System.out.println("* JSON string contains:  * " + output); //prints the string with json data successfully

    ObjectMapper objectMapper = new ObjectMapper();
    TypeReference<ArrayList<Note>> mapType = new TypeReference<ArrayList<Note>>() {};
    ArrayList<Note> jsonToList = objectMapper.readValue(output, mapType);

    request.setAttribute("allNotesOfUser", jsonToList);

    RequestDispatcher rd = request.getRequestDispatcher("/Notes.jsp");
    rd.forward(request, response);
}

jsp代码(只是表格部分):

<table class="table table-striped">
        <thead>
            <tr>
                <th> Id </th>
                <th> Title </th>
                <th> Text </th>
                <th> Color </th>
                <th> Date/Time </th>
            </tr>
        </thead>

        <tbody>
        <c:forEach items="${allNotesOfUser}" var="pp">
                <tr>
                    <td><${pp.note_id}</td>
                    <td><${pp.title}</td>
                    <td><${pp.text}</td>
                    <td><${pp.color}</td>
                    <td><${pp.datetime}</td>
                </tr>
        </c:forEach>
        </tbody>

    </table>

注意实体:

@XmlRootElement
public class Note {
    private int note_id;
    private String title;
    private String text;
    private String color;
    private String datetime;


    public Note(int note_id, String title, String text, String color, String datetime){
        this.note_id = note_id;
        this.title = title;
        this.text = text;
        this.color = color;
        this.datetime = datetime;
    }

    public Note(){
        super();
    }

    public int getNote_id() {
        return note_id;
    }
    public void setNote_id(int note_id) {
        this.note_id = note_id;
    }


    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }


    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }


    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }


    public String getDatetime() {
        return datetime;
    }
    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }

}

【问题讨论】:

    标签: java json jsp servlets jstl


    【解决方案1】:

    我不知道为什么,但我尝试在我的 JSP 代码中插入 &lt;c:out value="" /&gt;,所以它看起来像这样:

    <c:forEach items="${allNotesOfUser}" var="pp">
                    <tr>
                        <td><c:out value="${pp.note_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>
                    </tr>
            </c:forEach>
    

    所以现在它正在工作:

    这是c:out的一些参考链接

    (现在我就继续卡在别的地方T-T哭了XD)

    【讨论】:

    • 知道json的结构是否未知吗?
    【解决方案2】:

    您可以将 Note 实体发送到 jsp 页面并打印它的简单方法。并且您还可以使用 JSON 对象 bt bst 方式与您必须使用 java 脚本的 JSON 对象。但你也可以使用com.google.gson.JSONObject 来解决这个问题。

    【讨论】:

      猜你喜欢
      • 2013-01-10
      • 1970-01-01
      • 2013-11-25
      • 2014-05-07
      • 2016-02-16
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多