[英]Pass JsonArray data from serveResource to jsp and display on JSP - Liferay
[英]Display data's in jsp from controller
我正在一个项目下工作,该项目通过使用Spring Hibernate从数据库显示jsp表中的数据。 我成功通过休眠从DB获取了所有数据,并将它们也发送到javascript函数。 但是问题是,来自控制器的数据是一个对象,并以字符串形式显示在jsp表中。 我该怎么做?
我的控制器
@RequestMapping(value = "/view_all_record", method = RequestMethod.GET)
public @ResponseBody void getAllRecord(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
{
JsonArray jsonArray = null;
List<Customer> list = new ArrayList<Customer>();
try
{
//Fetching data from DB by hibernate
list = firstService.fetchList();
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(list, new TypeToken<List<Customer>>(){}.getType());
jsonArray = element.getAsJsonArray();
response.setContentType("application/json");
response.getWriter().print(jsonArray);
}catch(Exception e)
{
logger.error(e.getMessage());
}
}
我的JSP
<input type="button" value="View All" id="showTable" class="showTable" name="showTable">
<div id="tablediv" class="tablediv">
<table id="customerTable" class="customerTable">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody class="dataFetching">
</tbody>
</table>
</div>
我的剧本
<script type="text/javascript">
$(document).ready(function() {
$("#tablediv").hide();
$("#showTable").click(function(event){
$.get('./firstController/view_all_record',function(responseJson) {
$("#customerTable").find("tr:gt(0)").remove();
var table1 = $("#customerTable");
$.each(responseJson, function(key,value) {
var rowNew= $("<tr><td></td><td></td><td></td><td></td></tr>");
rowNew.children().eq(0).text(JSON.stringify(value['id']));
rowNew.children().eq(1).text(JSON.stringify(value['address']));
rowNew.children().eq(2).text(JSON.stringify(value['email']));
rowNew.children().eq(3).text(JSON.stringify(value['name']));
//rowNew.appenTo(table1);
});
});
$("#tablediv").show();
});
});
但是它在jsp表中没有显示任何值。
终于我解决了我的问题
var table1 = $("#customerTable");
$.each(responseJson, function(key,value) {
var rowNew = "<tr>"+
"<td>"+JSON.stringify(value['id'])+"</td>"+
"<td>"+JSON.stringify(value['name']).replace('"', '').replace('"', '')+"</td>"+
"<td>"+JSON.stringify(value['email']).replace('"', '').replace('"', '')+"</td>"+
"<td>"+JSON.stringify(value['address']).replace('"', '').replace('"', '')+"</td>"+
"</tr>"
$(rowNew).appendTo("#customerTable");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.