我已經搜索了每個地方,但找不到所需的幫助。如何在jsp表中顯示Json字符串
在我的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;
}
}
一如果json的結構未知,您是否有想法? – CodeHunter