主頁 > 知識庫 > json實現(xiàn)jsp分頁實例介紹(附效果圖)

json實現(xiàn)jsp分頁實例介紹(附效果圖)

熱門標簽:山東ai外呼電銷機器人好用嗎 地圖標注專業(yè)和非專業(yè) 四川正規(guī)外呼系統(tǒng)軟件 福建電銷貓機器人收費 甘肅銷售電銷機器人公司 湖北地圖標注公司 智能電話機器人銷售話術(shù) 汝南縣地圖標注app 外呼直播語音系統(tǒng)
json 在上篇文章已有詳細介紹,json的既簡單易懂,又傳輸迅速。并且能和javascript很好的融為一體。
在不需要添加jar的前提下,能夠很好完成jsp分頁問題。
下面具體介紹分頁實例:
 
效果如圖所示,采用jsp+servlet技術(shù)
首先:編寫一個javaBean User.java
復制代碼 代碼如下:

package bean;
public class User {
private int id;
private String name;
private String password;
private int age;
public User() {
super();
}
public User(int id, String name, String password, int age) {
super();
this.id = id;
this.name = name;
this.password = password;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
//{'id':1,'name':'zhangsan','password':'123','age':1}
return "{'id':"+id+",'name':'"+name+"','password':'"+password+"','age':"+age+"}";
}

}

這里需要注意的 就是 toString的方法的改變
然后:我們來編寫它的control 層和Dao層
為了簡化代碼,便于取值,數(shù)據(jù)庫暫寫為一個集合
可以看到,
1.service 接收request請求 得到頁面所要展示當前頁(為第page頁)
2.創(chuàng)建一個字符串,向內(nèi)依次添加 從數(shù)據(jù)庫DB 得到的user,并將所有數(shù)據(jù)封裝
復制代碼 代碼如下:

[{},{},{}]

3.將此字符串 返回到請求頁面
復制代碼 代碼如下:

package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.User;
public class Paging extends HttpServlet {
public static final int PER_PAGE = 3;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//分頁 數(shù)據(jù)源 當前得到第幾頁的記錄 每頁顯示多少條
int page = Integer.parseInt(request.getParameter("page"));
// page = 1 i = 0
//page = 2 3
int length = 0;//記錄當前拿了多少條
StringBuffer sb = new StringBuffer();
sb.append("[");
//[{},{},{}]
String message = null;
if(page >= 1 page = 3){
for (int i = (page-1)*PER_PAGE; i DB.list.size()length PER_PAGE; i++) {
User u = DB.list.get(i);
sb.append(u.toString()+",");
length++;
}
if(length > 0){
message = sb.substring(0, sb.length()-1)+"]";
}else{
message = sb.toString()+"]";
}
}else{
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("搗亂");
return;
}
response.setContentType("text/html;charset=utf-8");
response.getWriter().println(message);
}
}
class DB{
public static ListUser> list = new LinkedListUser>();
static{
list.add(new User(1,"zhangsan","zs",34));
list.add(new User(2,"lisi","ls",34));
list.add(new User(3,"a","h",34));
list.add(new User(4,"b","d",34));
list.add(new User(5,"c","g",34));
list.add(new User(6,"d","a",34));
list.add(new User(7,"e","d",34));
list.add(new User(8,"f","e",34));
list.add(new User(9,"g","a",34));
}
}

之后 :jsp的填寫,通過ajax異步提交page,然后得到相應(yīng)的字符串
復制代碼 代碼如下:

var mgs = xmlHttpRequest.responseText;
var persons = mgs.evalJSON();

將json數(shù)據(jù)解析 并添加到顯示的區(qū)域
復制代碼 代碼如下:

%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
html>
head>
base href="%=basePath%>">
title>My JSP 'regist.jsp' starting page/title>
meta http-equiv="pragma" content="no-cache">
meta http-equiv="cache-control" content="no-cache">
meta http-equiv="expires" content="0">
meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
meta http-equiv="description" content="This is my page">
script type="text/javascript" src="js/prototype1.6.js">/script>
script type="text/javascript" src="js/jquery-1.4.4.js">/script>
script type="text/javascript">
var paging = 0;
//
function page(p){
/*if(p == 'next' paging 3){
paging ++;
}else if(p == 'last' paging > 1) {
paging --;
}else{
alert('錯誤');
}*/
if(p == 'next' paging 3){
paging ++;
if(paging > 1){
$(":button:eq(0)").removeAttr('disabled');
}
if(paging == 3){
$(":button:eq(1)").attr('disabled','disabled');
}
}else if(p == 'last' paging > 1){
paging --;
$(":button:eq(1)").removeAttr('disabled');
if(paging == 1){
$(":button:eq(0)").attr('disabled','disabled');
}
}
createXmlHttpRequest();
xmlHttpRequest.onreadystatechange=back;
var url = encodeURI("/json_page/Paging?page="+paging);
xmlHttpRequest.open("GET",url,true);
xmlHttpRequest.send(null);
}
//假設(shè)名字為xmlHttpRequest
function createXmlHttpRequest(){
if(window.ActiveXObject){
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttpRequest = new XmlHttpRequest();
}
}
//回調(diào)函數(shù)
function back(){
if( xmlHttpRequest.readyState == 4 xmlHttpRequest.status == 200){
var mgs = xmlHttpRequest.responseText;
var persons = mgs.evalJSON();
//alert(mgs);
$("table").empty();
$("table").append( $("tr>td>id/td>td>用戶名/td>td>密碼/td>td>age/td>/tr>"));
for(var i = 0 ; i persons.length;i++){
var person = persons[i];
var $tr = $("tr>td>"+person.id+"/td>td>"+person.name+"/td>td>"+person.password+"/td>td>"+person.age+"/td>/tr>");
$("table").append($tr);
}
}
}
/script>
/head>
body>
input type="button" disabled="disabled" value="上一頁" onclick="page('last');"/>input type="button" value="下一頁" onclick = "page('next');"/>
table>
tr>td>id/td>td>用戶名/td>td>密碼/td>td>age/td>/tr>
/table>
/body>
/html>

另外:要有這兩個js哦
復制代碼 代碼如下:

script type="text/javascript" src="js/prototype1.6.js">/script>
script type="text/javascript" src="js/jquery-1.4.4.js">/script>
您可能感興趣的文章:
  • JSP分頁顯示的實例代碼
  • jsp分頁顯示的實現(xiàn)代碼
  • 一個實用的JSP分頁代碼
  • jsp hibernate的分頁代碼
  • 一個通用的jsp分頁PageBean
  • JSP自定義分頁標簽TAG全過程
  • jsp讀取數(shù)據(jù)庫實現(xiàn)分頁技術(shù)簡析
  • JSP實現(xiàn)的簡單分頁示例
  • jsp+servlet+javabean實現(xiàn)數(shù)據(jù)分頁方法完整實例
  • 簡單實現(xiàn)jsp分頁

標簽:吳忠 肇慶 臨沂 白銀 黔東 昌都 梅州 南充

巨人網(wǎng)絡(luò)通訊聲明:本文標題《json實現(xiàn)jsp分頁實例介紹(附效果圖)》,本文關(guān)鍵詞  json,實現(xiàn),jsp,分頁,實例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《json實現(xiàn)jsp分頁實例介紹(附效果圖)》相關(guān)的同類信息!
  • 本頁收集關(guān)于json實現(xiàn)jsp分頁實例介紹(附效果圖)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章