分页,向前台json传值工具类:
package com.xshcar.carcloud.util; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /** * 基类 * @author Administrator * */ public class BaseAction extends ActionSupport{ private Integer startpage; //开始页 private Integer endpage;//结尾页 private Integer topage;//总页数 protected Integer count; private final int SHOWPAGE = 10;//当前分页显示条数 private Integer pagenum; private Integer pagesize; private SimpleDateFormat baseFormat=new SimpleDateFormat("yyyy-MM-dd"); public void paginationTools(Integer pagenum,Integer pagesize){ this.pagenum=pagenum; this.pagesize=pagesize; topage= this.count%pagesize==0?count/pagesize:count/pagesize+1; if (topage < SHOWPAGE) { /** if中是总页数小于SHOWPAGES的情况 */ this.startpage = 1; this.endpage = topage; } else { /** else中是总页数大于SHOWPAGES的情况 */ if (pagenum <= SHOWPAGE / 2 + 1) { this.startpage = 1; this.endpage = SHOWPAGE; } else { this.startpage = pagenum - (SHOWPAGE / 2); this.endpage =pagenum + (SHOWPAGE / 2 - 1); if (this.endpage >= topage) { this.endpage = topage; this.startpage = topage - SHOWPAGE + 1; } } } } /** * 向前台传值方法 * @return * @throws IOException */ public void writeJsonToJsp(String msg) throws IOException{ HttpServletResponse response = ServletActionContext.getResponse(); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter pw = response.getWriter(); pw.write(msg); pw.flush(); pw.close(); } /** * 时间转换 相加 * @param time 要转换的时间 * @param type 1.小时,2.月份,3.年份 * @param count 数量,加多少或者减多少 * @return */ public String convertDates(Date time,Integer count,Integer type){ String date=""; try { Calendar cc = Calendar.getInstance(); cc.setTime(time);//2015-06-02 if(type==1){//小时 cc.add(Calendar.DAY_OF_MONTH, count); }else if(type==2){//月份 cc.add(Calendar.MONTH, count); }else if(type==3){//年份 cc.add(Calendar.YEAR, count); } date=baseFormat.format(cc.getTime()); } catch (Exception e) { e.printStackTrace(); } return date; } public Integer getStartpage() { return startpage; } public void setStartpage(Integer startpage) { this.startpage = startpage; } public Integer getEndpage() { return endpage; } public void setEndpage(Integer endpage) { this.endpage = endpage; } public Integer getTopage() { return topage; } public void setTopage(Integer topage) { this.topage = topage; } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } }
action:
//pagenum,pagesize在action中定义为常量eg: private Integer pagenum = 1; 并生成get/set方法 pagenum代表当前页,pagesize代表每页显示条数 public String gettcoutoFunctionList(){ try { if(!"".equals(tctofName) && tctofName!=null){ tctofName=tctofName.trim(); } List tcoutoFunctionList=tcouponsTofunctionService.gettcoutoFunction(tctofName, pagenum, pagesize); this.count=tcouponsTofunctionService.gettcoutoFunctionCount(tctofName); paginationTools(pagenum, pagesize); request.put("tcoutoFunctionList",tcoutoFunctionList); } catch (Exception e) { e.printStackTrace(); } return "tcoutoFunctionList"; }
daoImpl:
public List gettcoutoFunction(String tctof_name,Integer pagenum,Integer pagesize) throws Exception { String sql="select tcft.tcft_name,tc.*,ct.S_FullName from t_couponsTofunction tc inner join carshop_tbl ct on tc.s_id=ct.s_id"; sql+=" inner join t_coupons_function_type tcft on tcft.tcft_id=tc.tcft_id "; sql+=" and tc.tctof_status=0 "; if(!"".equals(tctof_name) && tctof_name!=null){ sql+=" and tctof_name like ‘%"+ tctof_name +"%‘ "; } sql+=" order by tctof_createtime desc "; Session session=getHibernateTemplate().getSessionFactory().getCurrentSession(); Query query=session.createSQLQuery(sql); query.setFirstResult((pagenum-1)*pagesize); query.setMaxResults(pagesize); return query.list(); } //数量查询 public Integer gettcoutoFunctionCount(String tctof_name) throws Exception{ String sql="select Count(*) from t_couponsTofunction tc inner join carshop_tbl ct on tc.s_id=ct.s_id"; sql+=" and tc.tctof_status=0 "; if(!"".equals(tctof_name) && tctof_name!=null){ sql+=" and tctof_name like ‘%"+ tctof_name +"%‘ "; } Session session=getHibernateTemplate().getSessionFactory().getCurrentSession(); Object obj=session.createSQLQuery(sql).uniqueResult(); Integer num=Integer.parseInt(obj.toString()); return num; }
页面引入文件:
<%@include file="/js/page.jsp" %>
引入的page.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %> <script type="text/javascript"> $(function(){ /** * 分页封装 * @longxiaoqiong */ /*var startpage=1; var endpage=2; var pagenum=1; var topage=1; var count=16;*/ var startpage=${startpage}; var endpage=${endpage}; var pagenum=${pagenum}; var topage=${topage}; var count=${count}; var page="<div class=‘page‘ style=‘right:0;‘ id=‘page‘>"; page+="<ul>"; page+="<li class=‘back1‘ onclick=‘gopage(1)‘></li>"; if(pagenum==startpage){ page+="<li class=‘back‘ onclick=‘gopage("+pagenum+")‘></li>"; }else{ page+="<li class=‘back‘ onclick=‘gopage("+(pagenum-1)+")‘></li>"; } for(var i=startpage;i<=endpage;i++){ if(i==pagenum){ page+="<li class=‘NoSelect‘>"+i+"</li>"; }else{ page+="<li class=‘No‘><a href=‘javascript:void(0);‘ onclick=‘gopage("+i+")‘>"+i+"</a></li>"; } } if(pagenum==endpage){ page+="<li class=‘next‘ onclick=‘gopage("+(pagenum)+")‘> </li>"; }else{ page+="<li class=‘next‘ onclick=‘gopage("+(pagenum+1)+")‘> </li>"; } page+="<li class=‘next1‘ onclick=‘gopage("+topage+")‘> </li> "; page+="<li class=‘zhuan_page‘>当前:<font color=‘red‘>"+pagenum+"</font>/"+topage+"</li>"; page+="<li class=‘zhuan_page‘> 总数:"+count+"</li><input type=‘hidden‘ name=‘pagenum‘ id=‘pagenum‘ value=‘1‘>"; page+="</ul></div>"; if(topage!=0){ document.getElementById("divpage").innerHTML=page; } }); function gopage(pagenum){ document.getElementById("pagenum").value=pagenum; document.getElementById("pageform").submit(); } </script>
时间: 2024-10-13 07:47:14