2017-04-13 14 views
0

我已經搜索了每個地方,但找不到所需的幫助。如何在jsp表中顯示Json字符串

在我的servlet中,我收到一個包含JSON數據的字符串。這些數據是從數據庫中的行與包含此:

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

我的問題是,我不能夠證明使用JSTL HTML表格,這些數據。

這就是我得到: enter image description here

(我就要強調,不能找出如何解決這個問題)。

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

} 

回答

0

簡單的方法,你可以發送注意實體到jsp頁面並打印出來。你也可以使用JSON對象bt bst方式使用JSON對象,你必須使用java腳本。但您也可以使用com.google.gson.JSONObject來解決此問題。

1

我不知道爲什麼,但我想在我的JSP代碼中插入<c:out value="" />,所以它看起來是這樣的:

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

所以現在它的工作: enter image description here

這裏是一個鏈接爲的c:out

一些參考(現在我只是繼續陷在別處TT哭了XD)

+0

一如果json的結構未知,您是否有想法? – CodeHunter

相關問題