栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JavaWeb(ajax实现无刷新分页)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaWeb(ajax实现无刷新分页)

目录

1.准备阶段需要

2.需要那些技术来实现无刷新分页

3.主要代码部分

4.实现后的界面截图


1.准备阶段需求

1.1首先需要开发工具eclipse

1.2需要jar包两个:JSON的jar包:fastjson-1.2.47.jar  ojdbc14.jar放入lib中, 以及jQuery的类库:jquery-3.3.1.js放到js文件夹内

2.需要那些技术来实现无刷新分页

     2.1、Java,Oracle数据库,AJAX,jQuery

     2.2、需要在项目中建立的五个包(采用三层架构+MVC模式)

2.3.jar包放置位置

 

 

3.主要代码部分

3.1需要在oracle数据库中建立表(goods)

数据库设计

create table goods
(
	gid				--商品编号
	gname			--商品名称
	gprice			--商品单价
	ginfo			--商品描述信息
	gpath			--商品图片路径
)
insert into goods values(1,'mp31',100,'mp31','images/1.jpg') ;

insert into goods values(2,'mp32',1200,'mp32','images/2.jpg') ;

insert into goods values(3,'mp33',100,'mp33','images/3.jpg') ;

insert into goods values(4,'mp34',1600,'mp34','images/4.jpg') ;

insert into goods values(5,'mp35',1500,'mp35','images/5.jpg') ;

insert into goods values(6,'mp36',1400,'mp36','images/6.jpg') ;

insert into goods values(7,'mp37',1700,'mp37','images/7.jpg') ;

insert into goods values(8,'mp38',1300,'mp38','images/8.jpg') ;

insert into goods values(9,'mp39',1500,'mp39','images/9.jpg') ;

insert into goods values(10,'mp310',1600,'mp310','images/10.jpg') ;

insert into goods values(11,'mp311',1600,'mp311','images/11.jpg');

3.2需要在eclipse你的项目中连接oracle数据库DBHelper

package com.zking.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import oracle.jdbc.driver.OracleDriver;


public class DBHelper {
	
	//两个静态常量
	private static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
	private static final String CNAME="oracle.jdbc.driver.OracleDriver";
	
	
	
	//加载驱动
	static {
		try {
			Class.forName(CNAME);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public static Connection getCon() {
		Connection con=null;
		try {
			con=DriverManager.getConnection(URL, "scott", "Tiger123");//(Tiger123是指你的数据库密码)
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}
	
	
	public static void myClose(Connection con,PreparedStatement ps,ResultSet rs) {
		try {
			if(con!=null&&!con.isClosed()){
				con.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(rs!=null){
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
public static void main(String[] args) {
	System.out.println(getCon());
}
	
}
3.3需要在com.zking.entity包中建立实体类(goods 注意实现Serializable 防止序列化&反序列化异常)
package com.zking.entity;

import java.io.Serializable;

public class Goods implements Serializable{
	private static final long serialVersionUID = 170842568495657866L;
	private int gid;
	private String gname;
	private int gprice;
	private String ginfo;
	private String gpath;
	
	public int getGid() {
		return gid;
	}
	public void setGid(int gid) {
		this.gid = gid;
	}
	public String getGname() {
		return gname;
	}
	public void setGname(String gname) {
		this.gname = gname;
	}
	public int getGprice() {
		return gprice;
	}
	public void setGprice(int gprice) {
		this.gprice = gprice;
	}
	public String getGinfo() {
		return ginfo;
	}
	public void setGinfo(String ginfo) {
		this.ginfo = ginfo;
	}
	public String getGpath() {
		return gpath;
	}
	public void setGpath(String gpath) {
		this.gpath = gpath;
	}
	
	public Goods() {
		// TODO Auto-generated constructor stub
	}
	public Goods(int gid, String gname, int gprice, String ginfo, String gpath) {
		this.gid = gid;
		this.gname = gname;
		this.gprice = gprice;
		this.ginfo = ginfo;
		this.gpath = gpath;
	}
	public Goods(String gname, int gprice, String ginfo, String gpath) {
		this.gname = gname;
		this.gprice = gprice;
		this.ginfo = ginfo;
		this.gpath = gpath;
	}
	
}

3.4需要在com.zking.dao包中写好方法然后封装到业务逻辑层(com.zking.biz)调用

package com.zking.entity;

import java.io.Serializable;

public class Goods implements Serializable{
	private static final long serialVersionUID = 170842568495657866L;
	private int gid;
	private String gname;
	private int gprice;
	private String ginfo;
	private String gpath;
	
	public int getGid() {
		return gid;
	}
	public void setGid(int gid) {
		this.gid = gid;
	}
	public String getGname() {
		return gname;
	}
	public void setGname(String gname) {
		this.gname = gname;
	}
	public int getGprice() {
		return gprice;
	}
	public void setGprice(int gprice) {
		this.gprice = gprice;
	}
	public String getGinfo() {
		return ginfo;
	}
	public void setGinfo(String ginfo) {
		this.ginfo = ginfo;
	}
	public String getGpath() {
		return gpath;
	}
	public void setGpath(String gpath) {
		this.gpath = gpath;
	}
	
	public Goods() {
		// TODO Auto-generated constructor stub
	}
	public Goods(int gid, String gname, int gprice, String ginfo, String gpath) {
		this.gid = gid;
		this.gname = gname;
		this.gprice = gprice;
		this.ginfo = ginfo;
		this.gpath = gpath;
	}
	public Goods(String gname, int gprice, String ginfo, String gpath) {
		this.gname = gname;
		this.gprice = gprice;
		this.ginfo = ginfo;
		this.gpath = gpath;
	}
	
}

3.5编写PageServlet包名为(com.zking.servlet)

package com.zking.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.zking.biz.GoodsBiz;
import com.zking.biz.IGoodsBiz;
import com.zking.entity.Goods;

@WebServlet("/page.do")
public class PageServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置编码方式
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=UTF-8");
		
		//获取out
		PrintWriter out=resp.getWriter();
		
		int pageIndex=1;
		int pageSize=5;
		
		//接收pid
		String pid=req.getParameter("pid");
		if(pid!=null) {
			pageIndex=Integer.parseInt(pid);
		}
		
		//调用biz层的分页方法
		IGoodsBiz igb=new GoodsBiz();
		//商品集合
		List ls=igb.getAllByPage(pageIndex, pageSize);
		//获取最大页码
		int max=igb.getMax("goods", pageSize);
		//把集合转为String
		String str=JSON.toJSonString(ls);
		//把响应输送到客户端
		out.print(str+"*"+max);//特殊字符做拼接
		out.flush();
		out.close();
	}
	
}

3.6最后需要view视图index.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here






	
首页  上一页  下一页  末页 

4.实现后的界面截图

主界面index.jsp效果预览

        

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/785376.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号