jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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 'pictrue.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">
<link rel="stylesheet" type="text/css" href="css/jkxyflower.css">
</head>
<body>
<jsp:include page="head.jsp"></jsp:include>
<div class="content">
<div class="left">
<h1>左边</h1>
</div>
<div class="right">
<s:iterator value="#request.flowers" id="flower">
<div class="newflower">
<s:form action="shoppingAction" method="post" theme="simple">
<img src="pic/<s:property value="#flower.picture"/>" />
<br>
<s:property value="#flower.flowername"/>
<br>
<s:property value="#flower.price" />元
<br>
数量<s:textfield size="4" name="quantity"></s:textfield>
<input type="hidden" name="id" value="<s:property value="#flower.flowerid" />">
<s:submit value="购买"></s:submit>
</s:form>
</div>
</s:iterator>
<div style="clear:both">
<s:set name="pager" value="#request.page" />
<s:if test="#pager.hasFirst">
<a href="browseFlower.action?currentPage=1">首页</a>
</s:if>
<s:if test="#pager.hasPrevious">
<a href="browseFlower.action?currentPage=<s:property value="#pager.currentPage-1" />">上一页</a>
</s:if>
<s:if test="#pager.hasNext">
<a href="browseFlower.action?currentPage=<s:property value="#pager.currentPage+1" />">下一页</a>
</s:if>
<s:if test="#pager.hasLast">
<a href="browseFlower.action?currentPage=<s:property value="#pager.totalPage" />">尾页</a>
</s:if>
<br>
当前第<s:property value="#pager.currentPage" />页,
总共<s:property value="#pager.totalPage" /> 页
</div>
</div>
</div>
<jsp:include page="foot.jsp"></jsp:include>
</body>
</html>
package com.jkxy.util;
/**
* @author
* @category 分页实体类
*/
public class Page {
//当前页码
private int currentPage;
//页面尺寸
private int pageSize = 4;
//总的记录数
private int totalSize;
//总的页码数
private int totalPage;
//是否有首页
private boolean hasFirst;
//是否有上一页
private boolean hasPrevious;
//是否有下一页
private boolean hasNext;
//是否有尾页
private boolean hasLast;
//构造方法
public Page() {
}
public Page(int currentPage, int totalSize) {
super();
this.currentPage = currentPage;
this.totalSize = totalSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public int getTotalPage() {
totalPage = totalSize/pageSize;
if(totalSize % pageSize != 0){
totalPage++;
}
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public boolean isHasFirst() {
if(currentPage == 1){
return false;
}
return true;
}
public void setHasFirst(boolean hasFirst) {
this.hasFirst = hasFirst;
}
public boolean isHasPrevious() {
if(isHasFirst()){
return true;
}
return false;
}
public void setHasPrevious(boolean hasPrevious) {
this.hasPrevious = hasPrevious;
}
public boolean isHasNext() {
if(isHasLast()){
return true;
}
return false;
}
public void setHasNext(boolean hasNext) {
this.hasNext = hasNext;
}
public boolean isHasLast() {
if(currentPage == getTotalPage()){
return false;
}
return true;
}
public void setHasLast(boolean hasLast) {
this.hasLast = hasLast;
}
}
action方法
package com.jkxy.action;
import java.util.List;
import java.util.Map;
import com.jkxy.entity.Catalog;
import com.jkxy.service.ICatalogService;
import com.jkxy.service.IFlowerService;
import com.jkxy.util.Page;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author
* @category 鲜花分类的控制层
*/
public class flowerAction extends ActionSupport {
private ICatalogService catalogservice;
private IFlowerService flowerService;
private int catalogid;
private int currentPage = 1;
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setCatalogid(int catalogid) {
this.catalogid = catalogid;
}
public void setFlowerService(IFlowerService flowerService) {
this.flowerService = flowerService;
}
public void setCatalogservice(ICatalogService catalogservice) {
this.catalogservice = catalogservice;
}
/**
* 获得鲜花的分类
* @return
* @throws Exception
*/
public String browseCatalog() throws Exception{
List<Catalog> catalogs= catalogservice.findAllCatalogs();
Map request = (Map) ActionContext.getContext().get("request");
request.put("catalogs", catalogs);
return SUCCESS;
}
public String browseFlowerPaging() throws Exception{
System.out.println(catalogid);
int totalSize = flowerService.getTotalByCatalogid(catalogid);
Page page = new Page(currentPage, totalSize);
List flowers = flowerService.getFlowerByCatalogidPaging(catalogid, currentPage,page.getPageSize());
Map request = (Map) ActionContext.getContext().get("request");
request.put("flowers", flowers);
request.put("page", page);
return "flowerpage";
}
}
service层
package com.jkxy.service.impl;
import java.util.List;
import com.jkxy.dao.IFlowerDao;
import com.jkxy.service.IFlowerService;
public class FlowerServiceImpl implements IFlowerService {
private IFlowerDao flowerdao;
public void setFlowerdao(IFlowerDao flowerdao) {
this.flowerdao = flowerdao;
}
@Override
public List getFlowerByCatalogidPaging(int catalogid, int currentPage,
int currentSize) {
return flowerdao.getFlowerByCatalogidPaging(catalogid, currentPage, currentSize);
}
@Override
public int getTotalByCatalogid(int catalogid) {
return flowerdao.getTotalByCatalogid(catalogid);
}
}
dao层
package com.jkxy.dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.jkxy.dao.IFlowerDao;
/**
* @author
* @category 鲜花持久层实现类
*/
public class FlowerDaoImpl implements IFlowerDao {
private SessionFactory sessionfactory;
public void setSessionfactory(SessionFactory sessionfactory) {
this.sessionfactory = sessionfactory;
}
@Override
public List getFlowerByCatalogidPaging(int catalogid, int currentPage,
int currentSize) {
Session session = sessionfactory.openSession();
Query query = session.createQuery("from Flower where catalogid ="+catalogid);
query.setFirstResult((currentPage-1)*currentSize);
query.setMaxResults(currentSize);
List flowers = query.list();
session.close();
return flowers;
}
@Override
public int getTotalByCatalogid(int catalogid) {
Session session = sessionfactory.openSession();
Query query = session.createQuery("from Flower where catalogid ="+catalogid);
List flowers = query.list();
session.close();
return flowers.size();
}
}