dao 方法:
Java代码
/**
* get appCodes by Pagination
*
* @param appCode
* @param pagination
*/
@SuppressWarnings("unchecked")
public Pagination getAppCodes(final AppCode appCode, final Pagination pagination) {
HibernateCallback callback = new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException, SQLException {
int totalRow = 0;
List<AppCode> objList = new ArrayList<AppCode>();
try {
Criteria criteria = session.createCriteria(AppCode.class);
// 只查状态有效的
criteria.add(Restrictions.eq("status", CConstants.VALID));
totalRow = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
criteria.setProjection(null);
pagination.setTotalRow(totalRow);
objList = criteria.addOrder(Order.asc("category")).addOrder(Order.asc("codeId")).setFirstResult(pagination.getStartRow()).setMaxResults(pagination.getPageSize()).list();
pagination.setObjList(objList);
} catch (Exception e) {
log.error("uh oh, getappCodes by Pagination failed...");
e.printStackTrace();
}
return pagination;
}
};
return (Pagination) getHibernateTemplate().execute(callback);
}
/**
* get appCodes by Pagination
*
* @param appCode
* @param pagination
*/
@SuppressWarnings("unchecked")
public Pagination getAppCodes(final AppCode appCode, final Pagination pagination) {
HibernateCallback callback = new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException, SQLException {
int totalRow = 0;
List<AppCode> objList = new ArrayList<AppCode>();
try {
Criteria criteria = session.createCriteria(AppCode.class);
// 只查状态有效的
criteria.add(Restrictions.eq("status", CConstants.VALID));
totalRow = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
criteria.setProjection(null);
pagination.setTotalRow(totalRow);
objList = criteria.addOrder(Order.asc("category")).addOrder(Order.asc("codeId")).setFirstResult(pagination.getStartRow()).setMaxResults(pagination.getPageSize()).list();
pagination.setObjList(objList);
} catch (Exception e) {
log.error("uh oh, getappCodes by Pagination failed...");
e.printStackTrace();
}
return pagination;
}
};
return (Pagination) getHibernateTemplate().execute(callback);
}
Service 方法:
Java代码
/**
* get appCodes by Pagination
* @param appCode
* @param pagination
*/
public Pagination getAppCodes(AppCode appCode, Pagination pagination) {
return appCodeDao.getAppCodes(appCode, pagination);
}
/**
* get appCodes by Pagination for Flex
* @param map
* @param pagination
*/
public Pagination query(Map<String, String> map, Pagination pagination) {
if (map.isEmpty()) {
return getAppCodes(new AppCode(), pagination);
}
AppCode appCode = new AppCode();
if (StringUtils.isNotEmpty(map.get("id"))) {
appCode.setId(map.get("id"));
}
if (StringUtils.isNotEmpty(map.get("codeId"))) {
appCode.setCodeId(map.get("codeId"));
}
if (StringUtils.isNotEmpty(map.get("codeName"))) {
appCode.setCodeName(map.get("codeName"));
}
if (StringUtils.isNotEmpty(map.get("category"))) {
appCode.setCategory(map.get("category"));
}
if (StringUtils.isNotEmpty(map.get("categoryDesc"))) {
appCode.setCategoryDesc(map.get("categoryDesc"));
}
if (StringUtils.isNotEmpty(map.get("description"))) {
appCode.setDescription(map.get("description"));
}
if (StringUtils.isNotEmpty(map.get("status"))) {
appCode.setStatus(map.get("status"));
}
if (StringUtils.isNotEmpty(map.get("createDate"))) {
appCode.setCreateDate(DateTypeConverter.convertFromString(map.get("createDate")));
}
return getAppCodes(appCode, pagination);
}
/**
* get appCodes by Pagination
* @param appCode
* @param pagination
*/
public Pagination getAppCodes(AppCode appCode, Pagination pagination) {
return appCodeDao.getAppCodes(appCode, pagination);
}
/**
* get appCodes by Pagination for Flex
* @param map
* @param pagination
*/
public Pagination query(Map<String, String> map, Pagination pagination) {
if (map.isEmpty()) {
return getAppCodes(new AppCode(), pagination);
}
AppCode appCode = new AppCode();
if (StringUtils.isNotEmpty(map.get("id"))) {
appCode.setId(map.get("id"));
}
if (StringUtils.isNotEmpty(map.get("codeId"))) {
appCode.setCodeId(map.get("codeId"));
}
if (StringUtils.isNotEmpty(map.get("codeName"))) {
appCode.setCodeName(map.get("codeName"));
}
if (StringUtils.isNotEmpty(map.get("category"))) {
appCode.setCategory(map.get("category"));
}
if (StringUtils.isNotEmpty(map.get("categoryDesc"))) {
appCode.setCategoryDesc(map.get("categoryDesc"));
}
if (StringUtils.isNotEmpty(map.get("description"))) {
appCode.setDescription(map.get("description"));
}
if (StringUtils.isNotEmpty(map.get("status"))) {
appCode.setStatus(map.get("status"));
}
if (StringUtils.isNotEmpty(map.get("createDate"))) {
appCode.setCreateDate(DateTypeConverter.convertFromString(map.get("createDate")));
}
return getAppCodes(appCode, pagination);
}
分页类:Java代码
public class Pagination {
/** 当前页 */
private int currentPage = 1;
/** 每页显示数 */
private int pageSize = CConstants.PAGE_SIZE;
/** 总行数 */
private int totalRow = 0;
/** 页码 List */
private List<Integer> pages = new ArrayList<Integer>();
/** 总页数 */
private int totalPage;
/** 当前页在数据库中的起始行 */
private int startRow=0;
/** 查询参数保存 javabean的形式 */
private Object queryObject ;
/** 要显示的数据集 */
private List objList;
public Pagination() {
}
public Pagination(int currentPage, int pageSize, int totalRow) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalRow = totalRow;
}
public Pagination(int pageSize) {
this.pageSize = pageSize;
this.currentPage = 1;
this.totalRow = 1;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
if (currentPage==0) {
return;
}
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRow() {
return totalRow;
}
public void setTotalRow(int totalRow) {
this.totalRow = totalRow;
}
public int getStartRow() {
if (this.currentPage > 1) {
this.startRow = (this.currentPage - 1) * this.pageSize;
} else {
this.startRow = 0;
}
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public List<Integer> getPages() {
this.pages.clear();
for (int i = 0; i < this.totalPage; i++) {
try {
this.pages.add((i+1));
} catch (Exception e) {
e.printStackTrace();
}
}
return pages;
}
public void setPages(List<Integer> pages) {
this.pages = pages;
}
public Object getQueryObject() {
return queryObject;
}
public void setQueryObject(Object queryObject) {
this.queryObject = queryObject;
}
public List getObjList() {
return objList;
}
public void setObjList(List objList) {
this.objList = objList;
}
public int getTotalPage() {
this.totalPage = this.totalRow / this.pageSize;
if (this.totalRow % this.pageSize != 0) {
this.totalPage += 1;
}
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
}
public class Pagination {
/** 当前页 */
private int currentPage = 1;
/** 每页显示数 */
private int pageSize = CConstants.PAGE_SIZE;
/** 总行数 */
private int totalRow = 0;
/** 页码 List */
private List<Integer> pages = new ArrayList<Integer>();
/** 总页数 */
private int totalPage;
/** 当前页在数据库中的起始行 */
private int startRow=0;
/** 查询参数保存 javabean的形式 */
private Object queryObject ;
/** 要显示的数据集 */
private List objList;
public Pagination() {
}
public Pagination(int currentPage, int pageSize, int totalRow) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalRow = totalRow;
}
public Pagination(int pageSize) {
this.pageSize = pageSize;
this.currentPage = 1;
this.totalRow = 1;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
if (currentPage==0) {
return;
}
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRow() {
return totalRow;
}
public void setTotalRow(int totalRow) {
this.totalRow = totalRow;
}
public int getStartRow() {
if (this.currentPage > 1) {
this.startRow = (this.currentPage - 1) * this.pageSize;
} else {
this.startRow = 0;
}
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public List<Integer> getPages() {
this.pages.clear();
for (int i = 0; i < this.totalPage; i++) {
try {
this.pages.add((i+1));
} catch (Exception e) {
e.printStackTrace();
}
}
return pages;
}
public void setPages(List<Integer> pages) {
this.pages = pages;
}
public Object getQueryObject() {
return queryObject;
}
public void setQueryObject(Object queryObject) {
this.queryObject = queryObject;
}
public List getObjList() {
return objList;
}
public void setObjList(List objList) {
this.objList = objList;
}
public int getTotalPage() {
this.totalPage = this.totalRow / this.pageSize;
if (this.totalRow % this.pageSize != 0) {
this.totalPage += 1;
}
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
}
Action 代码:
Java代码
@SuppressWarnings("serial")
public class AppCodeAction extends BaseAction{
@Resource(name="appCodeService")
private AppCodeService appCodeService;
private AppCode appCode;
public AppCode getAppCode() {
return appCode;
}
public void setAppCode(AppCode appCode) {
this.appCode = appCode;
}
public String query() {
if (LOG.isDebugEnabled()) {
LOG.debug("Entering query method...");
}
if (this.pagination == null) {
this.pagination = new Pagination(CConstants.PAGE_SIZE);
}
if (this.appCode == null) {
this.appCode = new AppCode();
}
try{
this.pagination = appCodeService.getAppCodes(appCode, pagination);
}catch (Exception e) {
addActionError(getText("search.exception", new String []{getText("AppCode")}));
return ERROR;
}
return SUCCESS;
}
}
@SuppressWarnings("serial")
public class AppCodeAction extends BaseAction{
@Resource(name="appCodeService")
private AppCodeService appCodeService;
private AppCode appCode;
public AppCode getAppCode() {
return appCode;
}
public void setAppCode(AppCode appCode) {
this.appCode = appCode;
}
public String query() {
if (LOG.isDebugEnabled()) {
LOG.debug("Entering query method...");
}
if (this.pagination == null) {
this.pagination = new Pagination(CConstants.PAGE_SIZE);
}
if (this.appCode == null) {
this.appCode = new AppCode();
}
try{
this.pagination = appCodeService.getAppCodes(appCode, pagination);
}catch (Exception e) {
addActionError(getText("search.exception", new String []{getText("AppCode")}));
return ERROR;
}
return SUCCESS;
}
}
BaseAction Java代码
import com.opensymphony.xwork2.ActionSupport;
import com.tjsoft.util.Pagination;
@SuppressWarnings("serial")
public class BaseAction extends ActionSupport {
/** 分页类 */
protected Pagination pagination;
public Pagination getPagination() {
return pagination;
}
public void setPagination(Pagination pagination) {
this.pagination = pagination;
}
public String preAdd(){
if (LOG.isDebugEnabled()) {
LOG.debug("preAdd query method...");
}
return INPUT;
}
}
import com.opensymphony.xwork2.ActionSupport;
import com.tjsoft.util.Pagination;
@SuppressWarnings("serial")
public class BaseAction extends ActionSupport {
/** 分页类 */
protected Pagination pagination;
public Pagination getPagination() {
return pagination;
}
public void setPagination(Pagination pagination) {
this.pagination = pagination;
}
public String preAdd(){
if (LOG.isDebugEnabled()) {
LOG.debug("preAdd query method...");
}
return INPUT;
}
}
分页导航:Jsp代码
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ include file="/pages/taglibs.jsp" %>
<s:if test="#request.pagination.totalPage>0">
<div class="pageDiv" id="pageDiv">
<!-- 总记录数
<span>${pagination.totalRow}</span>-->
<!-- 当前页/总页数
<span>${pagination.currentPage}/${pagination.totalPage}</span>-->
<!-- 当前页不是第一页时生成首页和上一页 -->
<c:if test="${pagination.currentPage>1}">
<span οnclick="doPage('1')"><a>首页</a></span>
<span οnclick="doPage('pre')"><a>« 上一页</a></span>
</c:if>
<!-- 计算要分几次显示页码,每次显示10页 -->
<c:choose>
<c:when test="${pagination.totalPage %10 > 0}">
<c:set var="count" value="${pagination.totalPage/pagination.pageSize+1}" />
</c:when>
<c:otherwise>
<c:set var="count" value="${pagination.totalPage/pagination.pageSize}" />
</c:otherwise>
</c:choose>
<!-- count > 1 -->
<c:choose>
<c:when test="${count - 1 > 1}">
<!-- 1~10页先生成 -->
<label id="p_1" class="cruLabel">
<c:forEach var="p" begin="1" end="10">
<span οnclick="doPage('${p}')" <c:if test="${p==pagination.currentPage}">class='selected'</c:if>><a>${p}</a></span>
</c:forEach>
<span οnclick="doPageLabel('p_2')"><a> »</a></span>
</label>
<c:forEach var="index" begin="2" end="${count}">
<label id="p_${index}" class="hideLabel">
<span οnclick="doPageLabel('p_${index-1}')"><a>« </a></span>
<c:forEach items="${pagination.pages}" var="page" begin="${(index-1)*pagination.pageSize}" end="${index*pagination.pageSize-1}">
<c:if test="${page>10}">
<span οnclick="doPage('${page}')" <c:if test="${page==pagination.currentPage}">class='selected'</c:if>><a>${page}</a></span>
</c:if>
</c:forEach>
<c:if test="${count - index > 1}">
<span οnclick="doPageLabel('p_${index+1}')"><a> »</a></span>
</c:if>
</label>
</c:forEach>
</c:when>
<c:otherwise>
<c:forEach items="${pagination.pages}" var="page">
<span οnclick="doPage('${page}')" <c:if test="${page==pagination.currentPage}">class='selected'</c:if>><a>${page}</a></span>
</c:forEach>
</c:otherwise>
</c:choose>
<!-- 当前页不是最后页时生成下一页 末页 -->
<c:if test="${pagination.currentPage!=pagination.totalPage}">
<span οnclick="doPage('next')"><a>下一页 »</a></span>
<!-- <span οnclick="doPage('${pagination.totalPage}')"><a>末页</a></span>-->
</c:if>
<!--<span>转到</span>
<select id="to_p">
<c:forEach items="${pagination.pages}" var="tp">
<option value="${tp}"
<c:if test="${tp eq pagination.currentPage}">
selected="selected"
</c:if>
>${tp}</option>
</c:forEach>
</select>
<span>页</span>
<span οnclick="doPage(document.getElementById('to_p').value)">确定</span>-->
</div>
</s:if>
<script type="text/javascript">
/*** 翻页*/
function doPage(page){
var form=document.forms[0];
if('${pagination.currentPage}'===page){
return;
}else if("next"===page){
if('${pagination.currentPage}'=='${pagination.totalPage}')return;
form.action='<s:url />?pagination.currentPage='+(new Number('${pagination.currentPage}')+1);
}else if("pre"===page){
if('${pagination.currentPage}'==1)return;
form.action='<s:url />?pagination.currentPage='+(new Number('${pagination.currentPage}')-1);
}else {
form.action='<s:url />?pagination.currentPage='+page;
}
form.submit();
}
</script>