json 在上篇文章已有详细介绍,json的既简单易懂,又传输迅速。并且能和javascript很好的融为一体。
在不需要添加jar的前提下,能够很好完成jsp分页问题。
下面具体介绍分页实例:
效果如图所示,采用jsp+servlet技术
首先:编写一个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层
为了简化代码,便于取值,数据库暂写为一个集合
可以看到,
1.service 接收request请求 得到页面所要展示当前页(为第page页)
2.创建一个字符串,向内依次添加 从数据库DB 得到的user,并将所有数据封装
复制代码 代码如下:
[{},{},{}]
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 {
//分页 数据源 当前得到第几页的记录 每页显示多少条
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 List<User> list = new LinkedList<User>();
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,然后得到相应的字符串
复制代码 代码如下:
var mgs = xmlHttpRequest.responseText;
var persons = mgs.evalJSON();
将json数据解析 并添加到显示的区域
复制代码 代码如下:
<%@ 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="/UploadFiles/2021-04-02/prototype1.6.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery-1.4.4.js"><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);
}
//假设名字为xmlHttpRequest
function createXmlHttpRequest(){
if(window.ActiveXObject){
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttpRequest = new XmlHttpRequest();
}
}
//回调函数
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="/UploadFiles/2021-04-02/prototype1.6.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery-1.4.4.js">
在不需要添加jar的前提下,能够很好完成jsp分页问题。
下面具体介绍分页实例:
效果如图所示,采用jsp+servlet技术
首先:编写一个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层
为了简化代码,便于取值,数据库暂写为一个集合
可以看到,
1.service 接收request请求 得到页面所要展示当前页(为第page页)
2.创建一个字符串,向内依次添加 从数据库DB 得到的user,并将所有数据封装
复制代码 代码如下:
[{},{},{}]
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 {
//分页 数据源 当前得到第几页的记录 每页显示多少条
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 List<User> list = new LinkedList<User>();
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,然后得到相应的字符串
复制代码 代码如下:
var mgs = xmlHttpRequest.responseText;
var persons = mgs.evalJSON();
将json数据解析 并添加到显示的区域
复制代码 代码如下:
<%@ 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="/UploadFiles/2021-04-02/prototype1.6.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery-1.4.4.js"><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);
}
//假设名字为xmlHttpRequest
function createXmlHttpRequest(){
if(window.ActiveXObject){
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}else{
xmlHttpRequest = new XmlHttpRequest();
}
}
//回调函数
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="/UploadFiles/2021-04-02/prototype1.6.js"><script type="text/javascript" src="/UploadFiles/2021-04-02/jquery-1.4.4.js">
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月23日
2024年11月23日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]