<!--com.BooKShop.dao.BookDao.java-->
/** 分页浏览所有图书*/
public List browseBook(int pageSize, int pageNo) throws Exception {
Transaction fx=null;
List list = null;
try{
Query query = session.createQuery("from Books b order by b.bid");
query.setMaxResults(pageSize);
query.setFirstResult((pageNo-1)*pageSize);
fx=session.beginTransaction();
list = query.list();
fx.commit();
if (!Hibernate.isInitialized(list))Hibernate.initialize(list);
}catch(Exception ex){
if(fx!=null)fx.rollback();
System.out.println("在执行BookDao类中的browseBook方法时出错:/n");
ex.printStackTrace();
}finally{
//session.close();
}
return list;
}
/** 统计图书数量 */
public int countBook() throws Exception {
Transaction fx = null;
int count = 0;
try{
fx = session.beginTransaction();
Query query = session.createQuery("select count(*) from Books");
query.setMaxResults(1);
count = ((Integer)query.uniqueResult()).intValue();
fx.commit();
}catch(Exception ex){
if(fx!=null)fx.rollback();
System.out.println("在执行BookDao类中的countBook方法时出错:/n");
ex.printStackTrace();
}finally{
//session.close();
}
return count;
}
<!-- com.yourcompany.struts.action.QuerybooksAction.java -->
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.BooKShop.dao.BookDao;
import com.BooKShop.dao.ClassDao;
/**
* MyEclipse Struts
* Creation date: 11-23-2009
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class QuerybooksAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String p=null;//跳转变量
List list = null;
String action=null;
try {
action = new String (request.getParameter("action").getBytes("ISO8859-1"),"gb2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String text=null;
try {
text = new String(request.getParameter("text").getBytes("ISO8859-1"),"gb2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BookDao bookdao=new BookDao();
if("书名".equals(action)){
p="bookm";
list = bookdao.getBookName(text);
request.getSession().removeAttribute("book");
request.getSession().setAttribute("book",list );
}else if("作者".equals(action)){
p="bookm";
list = bookdao.getByAuthor(text);
request.getSession().removeAttribute("book");
request.getSession().setAttribute("book",list );
}
return mapping.findForward(p);
}
}
<!-- 页面代码如下 -->
<%@ page language="java" pageEncoding="GB2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>booksmanage.jsp</title>
</head>
<%
//得到本页的参数内容
int totals=0,totalPages=1,pageNo=1,prePageNo=1,nextPageNo=1,preOk=1,nextOk=1;
String action = "../browbook.do?";
if (request.getAttribute("totals")!=null) totals = Integer.parseInt(request.getAttribute("totals").toString());
if (request.getAttribute("totalPages")!=null) totalPages = Integer.parseInt(request.getAttribute("totalPages").toString());
if (request.getAttribute("pageNo")!=null) pageNo = Integer.parseInt(request.getAttribute("pageNo").toString());
if (totalPages<1)totalPages=1;
//计算上一页和下一页的页码及按钮的状态
prePageNo = pageNo;
nextPageNo = pageNo;
if (pageNo > 1) prePageNo--;
else prePageNo = 1;
if (pageNo < totalPages) nextPageNo++;
else nextPageNo = totalPages;
if (pageNo == 1) preOk=0;
if (pageNo == totalPages) nextOk=0;
%>
<body>
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF" class="bg">
<tr align="center">
<td height="25" colspan="9">
<table width="100%" height="25" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center" background="image/admin_bg_1.gif" class="baise">
<form action="../querybooks.do" >
<table width="99%" border="0" align="right" cellpadding="0" cellspacing="0">
<tr>
<td height="22" class="heise"><strong> <font color="#FFFFFF">图书信息检索</font>
<input name="text" type="text" class="bd" size="20" />
<select name="action" class="bd" id="First_Class">
<option value="">请选择:</option>
<option value="书名">书名</option>
<option value="作者">作者</option>
</select>
<font color="#FFFFFF">
<input name="go" type="submit" class="an" value="搜索" />
</font></strong>
</td>
</tr>
</table>
</form>
</table>
</td>
</tr >
<tr align="center" bgcolor="#F1F3F5">
<!-- <td width="8%" height="24" class="td" >编号</td>-->
<td width="10%" height="24" class="td" >图书名</td>
<td width="8%" height="24" class="td" >作者</td>
<td width="8%" height="24" class="td" >出版社</td>
<td width="10%" height="24" class="td" >价格</td>
<td width="8%" height="24" class="td" >订购数量</td>
<td width="10%" height="24" class="td" >剩余数量</td>
<td width="10%" height="24" class="td" >编辑</td>
<td width="10%" height="24" class="td" >删除</td>
</tr >
<logic:present name="book" scope="session">
<logic:iterate id="Books" name="book" scope="session">
<logic:present name="Books">
<tr bgcolor="#F1F3F5" align="center">
<!-- <td><input type="text" size="10%" id="Text" value="${Books.bid}" readonly></td>-->
<td><input type="text" size="30%" id="Text" value="${Books.bookname}" readonly></td>
<td><input type="text" size="10%" id="Text" value="${Books.author}" readonly></td>
<td><input type="text" size="30%" id="Text" value="${Books.publish}" readonly></td>
<td><input type="text" size="10%" id="Text" value="${Books.price}" readonly></td>
<td><input type="text" size="10%" id="Text" value="${Books.amount}" readonly></td>
<td><input type="text" size="10%" id="Text" value="${Books.leavNum}" readonly></td>
<td><a href="../manage/updatebooks.jsp?bid=${Books.bid}" > <img src="image/edit.gif" alt="编辑" width="12" height="12" border="0" /></a></td>
<td><a href="../addbooks.do?bid=${Books.bid}&action=1"> <img src="image/icon_del.gif" alt="删除" width="13" height="13" border="0" /></a> </td>
</tr>
</logic:present>
</logic:iterate>
</logic:present>
</table>
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="1" class="bg">
<tr >
<td width="100%"height="24" align="right" bgcolor="#F1F3F5" class="font">
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
<!-- <form action="" method="post" name="pa" id="pa">-->
<tr>
<td height="20" align="right" valign="middle" class="heise">共有<font color="#FF2D00"><%=totals%></font>条信息
分<font color="#FF2D00"><%=totalPages%></font>页 <font color="#FF2D00">10</font>条/页
当前第<font color="#FF2D00"><%=pageNo%></font>页
<img src="image/logo_img042.gif" alt="1" width="11" height="11" align="absmiddle" />
<a href="<%=action%>pageNo=1">首页</a>
<%if (preOk==1){%>
<a href="<%=action%>pageNo=<%=prePageNo%>">上页</a>
<%}else{%>
上页
<%}%>
<%if (nextOk==1){%>
<a href="<%=action%>pageNo=<%=nextPageNo%>">下页</a>
<%}else{%>
下页
<%}%>
<a href="<%=action%>pageNo=<%=totalPages%>">尾页</a>
<!--<img src="image/logo_img044.gif" alt="2" width="11" height="11" align="absmiddle" />
<input name="Page" type="text" class="bd" id="Page" value="1" size="3" />
<input name="Submit" type="submit" class="an" value="Go" /> -->
</td>
</tr>
<!-- </form>-->
</table></td>
</tr>
</table>
</body>
</html:html>