参考链接:http://www.cnblogs.com/chenping-987123/archive/2011/02/14/1954640.html
使用到的技术:
·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 += "";
-
//alert(html);
-
$('#divResult').html("");
-
$('#divResult').html(html);
-
-
-
},
-
error: function (XMLHttpRequest, textStatus, errorThrown) {
-
alert(XMLHttpRequest);
-
alert(textStatus);
-
alert(errorThrown);
-
}
-
});
-
}
-
-
function GoToFirstPage() {
-
pageIndex = 1;
-
AjaxGetData( pageIndex, pageSize);
-
}
-
-
function GoToPrePage() {
-
pageIndex -= 1;
-
pageIndex = pageIndex >= 1 ? pageIndex : 1;
-
AjaxGetData( pageIndex, pageSize);
-
}
-
-
function GoToNextPage() {
-
if (pageIndex < parseInt($("#count").text())) {
-
pageIndex += 1;
-
}
-
AjaxGetData( pageIndex, pageSize);
-
}
-
-
function GoToEndPage() {
-
pageIndex = parseInt($("#count").text()) ;
-
AjaxGetData( pageIndex, pageSize);
-
}
-
-
function GoToAppointPage(e) {
-
var page = $(e).prev().val();
-
if (isNaN(page)) {
-
alert("Page should be a valid number");
-
}
-
else {
-
var tempPageIndex = pageIndex;
-
pageIndex = parseInt($(e).prev().val());
-
if (pageIndex <= 0 || pageIndex > parseInt($("#count").text())) {
-
pageIndex = tempPageIndex;
-
alert("Please input valid page scope!");
-
}
-
else {
-
AjaxGetData(pageIndex, pageSize);
-
}
-
}
-
}
-
</script>
- <div id="divResult" ></div>
阅读(6919) | 评论(0) | 转发(0) |