欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入
使用到的技术:
·spring 3 mvc
·json
·jquery
·java
·mysql
首先,要了解如何在spring mvc中使用json.
以下主要从Dao和View及Controller来分析:
Dao层:
public List<Position> getPagePosition(int pageNum,int pageSize){
//select * from t_position order by id desc limit (pageNum-1)*pagesize , pagesize
List<Position> list = jdbc.query(“select * from position order by id desc limit ?,? ”, new Object[]{(pageNum-1)*pageSize>=0?(pageNum-1)*pageSize:0,pageSize},
new RowMapper<Position>() {
public Position mapRow(ResultSet rs, int rowNum)
throws SQLException {
return populate(rs);
}
}
);
return list;
}
Controller中代码:
@RequestMapping(“/positionlist”)
public String listPosition(Model model,
@RequestParam(value = “pagesize”, required = false, defaultValue = “5”) int pagesize,
@RequestParam(value = “pagenum”, required = false, defaultValue = “1”) int pagenum){
int recordCount = positionMgr.getAll()。size();
int pageCount = (recordCount+pagesize-1)/pagesize ;
model.addAttribute(“pageTitle”, “Position List”);
return “positionlist”;
}
@RequestMapping(“/positionlistajax”)
public @ResponseBody List<Position> listPositionDo(Model model,
@RequestParam(value = “pagesize”, required = false, defaultValue = “5”) int pagesize,
@RequestParam(value = “pagenum”, required = false, defaultValue = “1”) int pagenum){
List<Position> ret = positionMgr.getPagePosition(pagenum, pagesize);
return ret;
}
View层:
<script type=“text/javascript”>
var pageIndex = 0;
var pageSize = 5;
$(function () {
pageIndex = 1;
AjaxGetData(pageIndex, pageSize);
});
function AjaxGetData( index, size) {
$.ajax({
url: “${pageContext.request.contextPath}/positionlistajax”,
type: “Get”,
data: “pagenum=” + index + “&pagesize=” + size,
dataType: “json”,
success: function (json) {
var html = “”;
html += “<table>”;
html += “<thead>”;
html += “<tr><th colspan=7 >Position List</th></tr>”;
html += “<tr><th>ID</th><th>Name</th><th>Location</th><th>Nature</th><th>Number</th><th>End Date</th><th>Operation</th></tr>”;
html += “</thead>”;
html += “<tbody>”;
for(position in json){
html += “<tr>”;
html += “<td>”+json[position].id+“</td>”;
html += “<td>”+json[position].name+“</td>”;
html += “<td>”+json[position].location+“</td>”;
html += “<td>”+json[position].nature+“</td>”;
html += “<td>”+json[position].number+“</td>”;
html += “<td>”+json[position].endDate+“</td>”;
html += “<td><a href='editposition?id=”+json[position].id+“‘>Edit <a href='position?id=”+json[position].id+“'>View</td>”;
html += “</tr>”;
}
html += “</tbody>”;
html += “<tfoot>”;
html += “<tr>”;
html += “<td colspan='7'>”;
html += “<span>Total Records:” + ${recordCount} + “; Total Page:<span>” +${pageCount} + “” + “”;
html += “<a href='javascript:void' >First ”;
html += “<a href='javascript:void' >Pre ”;
html += “<a href='javascript:void'>Next ”;
html += “<a href='javascript:void' >Last ”;
html += “<input type='text' size='4' /><input type='button' value='Jump' /> ”;
html += “</td>”;
html += “</tr>”;
html += “</tfoot>”;
html += “</table>”;
//alert(html);
$('#divResult’)。html(“”);
$(‘#divResult’)。html(html);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest);
alert(textStatus);
alert(errorThrown);
}
});
}
[1] [2] 下一页
【责编:ivy】