分页无刷新

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

public class AjaxHomePage {

@SuppressWarnings("unchecked")
public void onLoad(Map mapRequest, Map mapSession) throws UnsupportedEncodingException {
// 当前是那一个方法
String method = FormUtil.getString("methodx", mapRequest);
JSONObject jo = null;
if (StringUtils.isNotEmpty(method) && "addinfo".equals(method)) {
jo = this.addInform(mapRequest, mapSession);
}
if (StringUtils.isNotEmpty(method) && "addinfos".equals(method)) {
this.insertInfo(mapRequest, mapSession);
}
if (StringUtils.isNotEmpty(method) && "selectall".equals(method)) {
jo = this.selectinform(mapRequest, mapSession);
}
PrintWriter out = null;
HttpServletResponse response = ActionContext.getResponse();
// 返回JSON对象
try {
out = response.getWriter();
out.println(jo);
} catch (Exception e) {
} finally {
if (out != null) {
out.close();
}
}
}

/**
* 查询出要修改的资讯信息
*
* @param mapRequest
* @param mapSession
* @return
*/
@SuppressWarnings("unchecked")
public JSONObject addInform(Map mapRequest, Map mapSession) {
Map map = new HashMap();
// 获得要修改资讯的id
Integer id = Integer.valueOf(FormUtil.getString("id", mapRequest));
// 查询出要修改的资讯信息
Information information = InformationService.getByIdInformation(id);
System.out.println(information.getCreator());
map.put("result", JSONObject.fromObject(information));
return JSONObject.fromObject(map);
}

/**
* 插入方法
*
* @return
*/
public String insertInfo(Map mapRequest, Map mapSession) {
System.out.println("插入成功 !!!!!");
return null;

}



/**
* 查询所有
*
* @param mapRequest
* @param mapSession
* @return
* @throws UnsupportedEncodingException
*/
@SuppressWarnings("unchecked")
public JSONObject selectinform(Map mapRequest, Map mapSession)
throws UnsupportedEncodingException {
int pageSize = 10;// 每页显示的条数 默认5
int recordCount;// 记录总条数
int currentPage = 0;// 当前页
int totalPage = 0;// 总页数
int starts;// 重第几条记录开始
Map maps = new HashMap();
Map map = new HashMap();
List jsonList = new ArrayList();
// 得到标题
String title = FormUtil.getString("title", mapRequest);
// 得到资讯地区
String area = FormUtil.getString("area", mapRequest);
String istop = FormUtil.getString("istop", mapRequest);
if (("".equals(title) || title == null)
&& ("".equals(area) || area == null)
&& ("".equals(istop) || istop == null)) {
// 获得第几页
try {
currentPage = Integer.valueOf(FormUtil.getString(
"current", mapRequest));
} catch (Exception e) {
currentPage = 1;
}
// 查询出总条数
recordCount = InformationService.getInformationCount();
// 得到总页数
totalPage = InformationService.pageCountNo(recordCount, pageSize);
currentPage = currentPage > 0 ? (currentPage > totalPage ? totalPage
: currentPage)
: 1;
starts = (currentPage - 1) * pageSize;// 得到当前页的开始数
List<Information> list = InformationService.pageSelect(starts,
pageSize);
for (Information information : list) {
map.put("id", information.getId());
map.put("title", information.getTitle());
map.put("creaTime",
format(information.getCreaTime() == null ? 0l
: information.getCreaTime().getTime()));
jsonList.add(JSONObject.fromObject(map));
}
} else {
if (!"".equals(title)) {
title = new String(title.getBytes("ISO-8859-1"), "utf-8");
}
if (!"".equals(area)) {
area = new String(area.getBytes("ISO-8859-1"), "utf-8");
}
// 得到是否置顶
if ("".equals(istop)) {
maps.put("title", title);
maps.put("area", area);
maps.put("istop", istop);
maps.put("limitClauseStart", 0);
maps.put("limitClauseCount", 10);
} else {
maps.put("title", title);
maps.put("area", area);
maps.put("istop", Integer.valueOf(istop));
maps.put("limitClauseStart", 0);
maps.put("limitClauseCount", 10);
}
// 获得第几页
try {
currentPage = Integer.valueOf(FormUtil.getString("current",
mapRequest));
} catch (Exception e) {
currentPage = 1;
}
// 查询出总条数
recordCount = InformationService.getInformationIdCount(maps);
// 得到总页数
totalPage = InformationService.pageCountNo(recordCount, pageSize);
currentPage = currentPage > 0 ? (currentPage > totalPage ? totalPage
: currentPage)
: 1;
starts = (currentPage - 1) * pageSize;// 得到当前页的开始数
List<Information> list = InformationService
.selectparameterPage(maps);
for (Information information : list) {
map.put("id", information.getId());
map.put("title", information.getTitle());
map.put("creaTime",
format(information.getCreaTime() == null ? 0l
: information.getCreaTime().getTime()));
jsonList.add(JSONObject.fromObject(map));
}
}
map.put("result", JSONArray.fromObject(jsonList));
return JSONObject.fromObject(map);
}

/**
* 处理json的时间问题
*
* @param dateLong
* @return
*/
public String format(Long dateLong) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = df.format(dateLong);
return str;
}

}

class index
package com.bydtrip.forms.homepage;

import java.util.List;
import java.util.Map;
public class index {
/**
* 第一次加载
* @param mapRequest
* @param mapSession
*/
@SuppressWarnings("unchecked")
public void onLoad(Map mapRequest, Map mapSession) {
System.out.println("开始加载页面");
int pageSize = 10;// 每页显示的条数 默认5
int recordCount;// 记录总条数
int currentPage = 0;// 当前页
int totalPage = 0;// 总页数
int starts;// 重第几条记录开始
// 获得第几页
try {
currentPage = Integer.valueOf(FormUtil.getString("currentPages",
mapRequest));
} catch (Exception e) {
currentPage = 1;
}
// 查询出总条数
recordCount = InformationService.getInformationCount();
// 得到总页数
totalPage = InformationService.pageCountNo(recordCount, pageSize);

// 当前页的首记录
currentPage = currentPage > 0 ? (currentPage > totalPage ? totalPage
: currentPage) : 1;
starts = (currentPage - 1) * pageSize;// 得到当前页的开始数
List<Information> list = InformationService
.pageSelect(starts, pageSize);
// 把总页数设置出去
mapRequest.put("totalPage", totalPage);
// 目前是第几页
mapRequest.put("currentPages", currentPage);
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://acegisecurity.org/webres" prefix="webres"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%String url=getServletContext().getContextPath(); %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="<%=url %>/public/css/admin.css" rel="stylesheet" type="text/css" />
<link href="<%=url %>/public/js/FCKeditor/sample.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%=url %>/public/js/FCKeditor/fckeditor.js"></script>

<link rel="stylesheet" type="text/css" href="<%=url %>/public/yui/fonts/fonts-min.css" />
<link rel="stylesheet" type="text/css" href="<%=url %>/public/yui/button/assets/skins/sam/button.css" />
<link rel="stylesheet" type="text/css" href="<%=url %>/public/yui/container/assets/skins/sam/container.css" />
<script type="text/javascript" src="<%=url %>/public/yui/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="<%=url %>/public/yui/connection/connection-min.js"></script>
<script type="text/javascript" src="<%=url %>/public/yui/element/element-min.js"></script>
<script type="text/javascript" src="<%=url %>/public/yui/button/button-min.js"></script>
<script type="text/javascript" src="<%=url %>/public/yui/dragdrop/dragdrop-min.js"></script>
<script type="text/javascript" src="<%=url %>/public/yui/container/container-min.js"></script>

<title>管理</title>
</head>
<body οnlοad="meth.firstPage()" class="yui-skin-sam">
<div id="back_global" >
<div class="back_right_ins">
<div class="lead"><h4>管理</h4></div>
<div class="new_all">
<div class="back_search">
<input type="text" value="请输入标题" name="title" id="title" class="m_right10 back_sear_box"
onMouseOver="this.focus()"
onMouseOut="if(this.value=='')this.value='请输入标题';"
onFocus="this.select()"
onClick="if(this.value=='请输入标题')this.value=''">

<input type="text" value="请输地区" name="area" id="area" class="m_right10 back_sear_box"
onMouseOver="this.focus()"
onMouseOut="if(this.value=='')this.value='请输地区';"
onFocus="this.select()"
onClick="if(this.value=='请输地区')this.value=''">
<select class="m_right10 back_sear_box" id="istop" name="istop">
<option value="no">--</option>
<option value="1">是</option>
<option value="0">否</option>
</select>
<input type="submit" value="搜 索" name="hunt" class="yellow_but" onClick="meth.hunt()">
</div>
<div class="hotel_one" >
<div class="but_action">
<input type="button" class="add_new_but_xz" onClick="add();">
<input type="submit" name="delete" value="" class="add_new_but_sc m_left10" οnclick="meth.todelete();"></div>
<div id="table"></div>
</div>
<div class="new_page"><div class="page_view"><div class="first">
<a href="javascript:;" id="firstPagebutton" οnclick="meth.firstPage()"><<</a></div><div class="fore">
<a href="javascript:;" οnclick="meth.prePage();" id="prePagebutton"><</a></div><div class="back">
<a href="javascript:;" οnclick="meth.nextPage();" id="nextPagebutton">></a></div><div class="last">
<a href="javascript:;" οnclick="meth.lastPage();" id="lastPagebutton">>></a></div>
<div class="number">[ <span id="nowPage" class="yellow">1</span> ]/[ <span id="countPage" class="yellow">${totalPage }</span> ] Go To</div>
<input class="p_n" name="" type="text" />
<input class="go_btn" name="" type="button" value=""/></div>
<div class="clear"></div></div>
</div><!--new_all-->
</div><!--back_right_ins-->
</div> <!--back_right-->


<script type="text/javascript">
YAHOO.namespace("example.container");
var fir =0;
YAHOO.util.Event.onDOMReady(function () {
// 提交前
var handleSubmit = function() {
if(fir==1){
var oEditor = FCKeditorAPI.GetInstance("detail");//fck的值
var theData = oEditor.GetXHTML(true);
var content = document.getElementById("details"); //隐的值
content.value = theData;
var s =document.getElementById('details').value;
var title =document.getElementById('showtitle').value;
if(title=="" || s==""){
alert("请输入完整");
return false;
}
var url='InformNewManagerForm.jsp';//添加
var postData='method=add&title='+title+'&detail='+s+'&date' + new Date();
}else if(fir == 2){
var oEditor = FCKeditorAPI.GetInstance("detail");//fck的值
var theData = oEditor.GetXHTML(true);
var content = document.getElementById("details"); //隐的值
content.value = theData;
var id =document.getElementById('infoid').value;
var s =document.getElementById('details').value;
var title =document.getElementById('showtitle').value;
if(title=="" || s==""){
alert("请输入完整");
return false;
}
var url="InformNewManagerForm.jsp";//编辑
var postData='method=update&id='+id+'&title='+title+'&detail='+s+'&date'+new Date();
}


var callback = {
success:function(o){
document.getElementById('showtitle').value="";
var d =document.getElementById('detail').value;
var oEditor = FCKeditorAPI.GetInstance("detail");//fck的值
oEditor.EditorDocument.body.innerHTML=""; //清空fck
window.location.reload();
},
failure:function(o){
alert("失败");
}
};
YAHOO.util.Connect.asyncRequest('post', url, callback, postData);
this.cancel();
};
//关闭
var handleCancel = function() {
this.cancel();
};
YAHOO.util.Dom.removeClass("dialog1", "yui-pe-content");
// Instantiate the Dialog
myDialog = new YAHOO.widget.Dialog("dialog1",
{ modal: true,
width : "50em",
height :"37em",
fixedcenter : true,
visible : false,
constraintoviewport : true,
buttons : [ { text:"确定", handler:handleSubmit},
{ text:"关闭", handler:handleCancel } ]
});
myDialog.render();
});

function selected()
{
var allsel=document.getElementsByName("id");
for(var i=0;i<allsel.length;i++){
allsel[i].checked=!allsel[i].checked;
}
}
function makeRequest(id){
alert(id);
}
//得到一个id
function getDom(id){
return document.getElementById(id);
}
//得到一个id的值
function getValue(id){
return getDom(id).value;
}

var meth = function(){
//从页面上得到总页数
var pageCount=getDom('countPage').innerHTML;
var title = ""; //标题
var area = "";//地区
var istop = "";//置顶
var currentPage = 1;//当前页数
return{
firstPage:function(){
meth.getConfirmPage(1); //第一次默认为首页
},prePage:function(){ //上一页
if(currentPage > 1){
currentPage -= 1;
meth.getConfirmPage(currentPage);
}
},nextPage:function(){ //下一页
if(currentPage < pageCount){
currentPage += 1;
meth.getConfirmPage(currentPage);
}
},lastPage:function(){ //最后一页
if(currentPage<=pageCount){
meth.getConfirmPage(pageCount);
currentPage = pageCount;
}
},hunt:function(){ //搜索
title=getValue('title');
area=getValue('area');
istop=getValue('istop');
if(title=='请输入资讯标题') title="";
if(area =='请输资讯地区') area="";
if(istop =='no')istop="";
meth.getConfirmPage(1);
},todelete:function(){ //删除
var checkeds= document.getElementsByName("id");

var a= new Array(checkeds.length);
var flag = false;
for(var i=0;i<checkeds.length;i++){
if(checkeds[i].checked == true){
flag = true;
break;
}
}
if(!flag){
alert("请至少选择一项");
} else{
var s = confirm('您确定要删除吗?');
meth.getConfirmPage(currentPage);
return s;
}
},
getConfirmPage:function(gotopage){//传入的是页数
pageCounts=gotopage;
if(pageCounts>pageCount)pageCounts= pageCount; //如果获得的页数大于总页就返回总页数
if(pageCounts<1)pageCounts=1; //如果获得的页数小于1的话返回1
var url='AjaxHomePage.jsp';
var postData ='methodx=selectall&current='+gotopage+'&title='+title+'&area='+area+'&istop='+istop+'&date='+new Date();//传入第几页
var callback = {
success:function(o){
var result=eval("("+o.responseText+")");
var tableData = result.result;
meth.createTable(tableData);
getDom("nowPage").innerHTML = gotopage;
getDom("countPage").innerHTML = pageCount;
},
failure:function(o){
alert(o.status+"failure");
}
//timeout:7000
};
YAHOO.util.Connect.asyncRequest('post', url, callback, postData);
},
createTable:function(tableData){//创建要显示的数据
var div=getDom('table');
div.innerHTML="";
div.innerHTML="<table class=\"h_msg_table m_top10\"><thead><tr><th width=\"7%\"><input type=\"checkbox\" id=\"sunbox\" name=\"sunbox\" οnclick=\"selected()\"></th><th width=\"68%\">主题</th><th width=\"18%\">发布时间</th><th>操作</th></tr></thead><tbody id=\"tablBody\"></tbody></table>";
if(tableData==0){
alert('数据为空');
return false;
}
var tbody=getDom('tablBody');
for(var i=0;i<tableData.length;i++){
//ie能识别出名为uniqueID的document对象的专用属性,名为uniqueID,ie是唯一能够识别这个属性的浏览器,所以uniqueID很适合用来确定脚本是不是在Ie中运行。使用document.uniqueID。
var myTr = (!!document.uniqueID)?document.createElement("<tr></tr>"):document.createElement("tr");
for(var j=0;j<3;j++){
var mytd = (!!document.uniqueID)?document.createElement('<td></td>'):document.createElement('td');
if(j==0){
mytd.innerHTML="<input type=\"checkbox\" name=\"id\" id=\"id\" value=\""+tableData[i].id+"\" />";
}
if(j==1){
mytd.innerHTML = "<a href=\"#\" class=\"bluelink\">"+tableData[i].title+"</a>";
}
if(j==2){
mytd.innerHTML ="<a href=\"#\" class=\"bluelink\">"+tableData[i].creaTime+"</a>";
}
myTr.appendChild(mytd);
}
var td1 = (!!document.uniqueID)?document.createElement("<td></td>"):document.createElement("td");
td1.innerHTML = "<a href=\"#\" id=\"edits\" name=\"edits\" class=\"bluelink\" onClick=\"makeRequest("+tableData[i].id+");\">编辑</a>";
myTr.appendChild(td1);
tbody.appendChild(myTr);
}
}
};
}();
</script>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值