怎么样用MYsql+jsp做电子商务网站商品分页显示功能。

怎么样用MYsql+jsp做电子商务网站商品分页显示功能。,第1张

实现jsp页面分页显示功能方案:

com.cn.page.PageBean.java 用于控制分页的情况和数据的处理的。

com.cn.page.PageView.java 用于在jsp页面中显示表单的。

//接口的定义:

public interface ContactBean {

public int getAvailableCount()throws Exception

public int getAvailableCount(int id)throws Exception

public PageBean listData(String page)throws Exception

public PageBean listData(String page,int id)throws Exception

public Collection getResult()throws Exception

}

//pageBean的定义:

public class PageBean {

public int curPage//当前的页值。

public int maxPage//最大的页数。

public int maxRowCount//最大的行数。

public static int rowsPerPage=10//控制分页的行数。

public java.util.Collection data//所要显示的数据的集合。

public PageBean() {

}

public void countMaxPage(){

if(this.maxRowCount%this.rowsPerPage==0){

this.maxPage=this.maxRowCount/this.rowsPerPage

}else{

this.maxPage=this.maxRowCount/this.rowsPerPage+1

}

}

public Collection getResult(){

return this.data

}

public PageBean(ContactAdapter bean,int id)throws Exception{

this.maxRowCount=bean.getAvailableCount(id)

if(maxRowCount==0)

maxRowCount=1

System.out.println(maxRowCount)

this.data=bean.getResult()

this.countMaxPage()

}

public PageBean(ContactAdapter bean)throws Exception{

this.maxRowCount=bean.getAvailableCount()

this.data=bean.getResult()

this.countMaxPage()

}

}

//对ContactBean接口进行adapter模式的封装。方便实用。

public class ContactAdapter implements ContactBean{

public ContactAdapter() { }

public int getAvailableCount()throws Exception{

return 0

}

public int getAvailableCount(int id)throws Exception{

return 0

}

public PageBean listData(String page)throws Exception {

return null

}

public PageBean listData(String page,int id)throws Exception {

return null

}

public Collection getResult()throws Exception{

return null

}

}

在使用中你可以实现com.cn.interfaces.ContactBean 或继承 com.cn.interfaces.ContactAdapter都可以。

实现方法:

实现一:其中不需要键入任何参数

public int getAvailableCount()throws Exception

public PageBean listData(String page)throws Exception

public Collection getResult()throws Exception

实现二:需要键入参数。

public int getAvailableCount(int id)throws Exception

public PageBean listData(String page,int id)throws Exception

public Collection getResult()throws Exception

例如:

Public class TestContact extends ContactAdapter{

Private Collection data

Public int getAvailableCount(int unitID) throws Exception{

BSIResultSet rs = new DBUtil().getResult(“

select count(*) NUM from TableTest where unit_id ="+id)

return rs.getInt(NUM)

}

public PageBean listData(String page,int incept_sort_id)throws Exception {

PageBean p = new PageBean(this,incept_sort_id)

int pageNum = Integer.parseInt(page)

BSIResultSet rs = con.getResult(

sql, ( pageNum -1)* p.rowsPerPage,, pageNum*p.rowsPerPage )

p.curPage = pageNum

p.data = this.getTestBean(rs)

//此方法是把查询的数据封装在一个collection中。

return p

}

public Collection getResult()throws Exception{

return this.data

}

}

其中testBean是封装好的数据bean。

如果没有参数的话。可以使用没有参数的一组接口。

例如:在jsp中如何是用借口。

<jsp:useBean id="pageCtl" class="com.cn.page.PageBean" scope="request"/>

<%

Vector c=(Vector)pageCtl.getResult()

Iterator it=c.iterator()

while(it.hasNext()){

TestBean d=(TestBean)it.next()//的到每一个单独的数据。

//可以取出此bean的属性值。

%>

。。。。。//属性使用。业务逻辑。

<form action="test.do" method="post" name="PageForm">

<%out.print(ViewPage.getView(pageCtl))%>

</form>

//显示分页的表单。

加入javaScript的page.js.在jsp页面上加入:

<script language="JavaScript" src="js/page.js">即可。

function jumping(){

document.PageForm.submit()

return

}

function gotoPage(Pagenum){

document.PageForm.jumpPage.value=Pagenum

document.PageForm.submit()

return

}

今天给同学用JSP+JavaBean+Servlet做了个分页。用的是MySQL+Tomcat+MyEclispe环境。

分页计数器如下:

package org.pagedemo.page

/**

* Create on 2008-08-28 11:05

*

* @author qpy_2006

* 要用到分页,则在创建该类对象时

* 只需在传入rows的前提下调用counter(page,rows)方法就可以设置firstRow,totalRows,totalPages

* 在查询的dao中,把该类对象做为参数传入,

* 再通过调用对象的getFirstRow(),及getMaxSize()设置

*/

public class PageUtil {

private int totalRows // 总行数即总记录数

private int totalPages// 总页数

private int currentPage = 1// 当前页

private int maxSize = 3// 每页存放3条记录

private int firstRow = 0// 数据库中第一行数据

/** Creates a new instance of PageCounter */

public PageUtil() {

}

public int getTotalRows() {

return totalRows

}

public void setTotalRows(int totalRows) {

this.totalRows = totalRows

}

public int getTotalPages() {

return totalPages

}

public void setTotalPages(int totalPages) {

this.totalPages = totalPages

}

public int getCurrentPage() {

return currentPage

}

public void setCurrentPage(int currentPage) {

this.currentPage = currentPage

}

public int getMaxSize() {

return maxSize

}

public void setMaxSize(int maxSize) {

this.maxSize = maxSize

}

public int getFirstRow() {

return firstRow

}

public void setFirstRow(int firstRow) {

this.firstRow = firstRow

}

// 根据请求参数pageType翻页,即设置firstRow

// 根据请求参数rows总记录数计算总页数

public void counter(String pageType,int rows) {

this.totalRows = rows

this.totalPages = (this.totalRows + this.maxSize - 1) / this.maxSize

System.out.println("counter 中的totalPages的值为 "+this.totalPages+" 页")

pageType = pageType.toLowerCase()

if ("first".equals(pageType)) {

first()

} else if ("last".equals(pageType)) {

last()

} else if ("next".equals(pageType)) {

next()

} else if ("previous".equals(pageType)) {

previous()

}

}

// 首页

private void first() {

currentPage = 1

firstRow = 0

}

// 上页

private void previous() {

if (totalPages == 0) {

currentPage = 1

} else {

if (currentPage == 1) {

currentPage = totalPages

} else {

currentPage--

}

}

firstRow = (currentPage - 1) * maxSize

}

// 下页

private void next() {

if (totalPages == 0) {

currentPage = 1

} else {

if (currentPage <totalPages) {

currentPage++

} else if (currentPage == totalPages) {

currentPage = 1

}

}

firstRow = (currentPage - 1) * maxSize

}

// 尾页

private void last() {

currentPage = totalPages

if (totalPages == 0) {

currentPage = 1

}

firstRow = (currentPage - 1) * maxSize

}

}

查询出所有的记录数:

package org.pagedemo.page

import java.sql.Connection

import java.sql.PreparedStatement

import java.sql.ResultSet

import java.sql.SQLException

import org.pagedemo.db.DBConnection

public class PageDAO extends DBConnection {

// 查出所有的记录数

public int getRows() {

Connection conn = this.getConnection()

String sql = "select count(*) from t_page"

int rows = 0

try {

PreparedStatement pstmt = conn.prepareStatement(sql)

ResultSet rs = pstmt.executeQuery()

while (rs.next()) {

rows = rs.getInt(1)

}

System.out.println("rows " + rows)

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} finally {

if (conn != null)

try {

conn.close()

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

}

return rows

}

}

Servlet中控制分页

package org.pagedemo.servlet

import java.io.IOException

import java.util.ArrayList

import java.util.List

import javax.servlet.ServletException

import javax.servlet.http.HttpServlet

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpServletResponse

import javax.servlet.http.HttpSession

import org.pagedemo.page.PageDAO

import org.pagedemo.page.PageUtil

import org.pagedemo.po.User

import org.pagedemo.user.dao.UserDAO

/**

* @author qpy_2006 Servlet,本系统的核心控制器

*/

public class UserServlet extends HttpServlet {

private static final long serialVersionUID = 1L

public void destroy() {

super.destroy()

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html")

this.doPost(request, response)

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html")

// 获得HttpSession并取得session,用户保存数据

// 以便在jsp页面或者其他servlet中获得该session中得值

HttpSession session = request.getSession()

// 获取分页的请求类别

String pageType = request.getParameter("pageType")

String action = request.getParameter("action")

List<User>users = new ArrayList<User>()

PageDAO pd = new PageDAO()

PageUtil pu = new PageUtil()

UserDAO ud = new UserDAO()

// 获取数据库中记录数

int rows = pd.getRows()

if ("initial".equals(action)) {

// 第一次进入index.jsp页面时,查询出第一页

// 设置分页的各个属性,参见PageUtil这个类

pu.counter("first", rows)

session.setAttribute("pageBean", pu)

} else if ("page".equals(action)) {

// 点击分页请求时执行下一代码

pu = (PageUtil) session.getAttribute("pageBean")

pu.counter(pageType, pu.getTotalRows())

}

users = ud.findAll(pu)

// 把查询的结果users放到session中,

// 以便在jsp页面从session读取该users对象

session.setAttribute("users", users)

// 把PageUtil放到session中

session.setAttribute("pageBean", pu)

// 在servlet跳转到某个jsp页面

request.getRequestDispatcher("index.jsp").forward(request, response)

}

public void init() throws ServletException {

}

}

详细代码及工程到如下网址下载:http://download.csdn.net/user/qpyong


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/8533944.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-17
下一篇2023-04-17

发表评论

登录后才能评论

评论列表(0条)

    保存