page类
package mock.model;
import java.util.List;
public class Page<T> {
//已知数据
private int current=3;//当前页,默认为3
private int numPerPage=15;//每页多少条,默认为15
private int totalCount;// 总记录数,查数据库
//需要计算得到的
private int totalPage;//总页数
private List<Mesg> Mesgs;//页面显示的记录
private int start;
private int end;
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
public int getNumPerPage() {
return numPerPage;
}
public void setNumPerPage(int numPerPage) {
this.numPerPage = numPerPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<Mesg> getMesgs() {
return Mesgs;
}
public void setMesgs(List<Mesg> mesgs) {
Mesgs = mesgs;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public Page(){}
public Page(int totalCount,int current){
this.totalCount=totalCount;
this.current=current;
if(this.totalCount%this.numPerPage==0){
this.totalPage=this.totalCount/this.numPerPage;
}else{
this.totalPage=this.totalCount/this.numPerPage+1;
}
this.start=1;
this.end=5;
if(this.totalPage<5){
this.end=this.totalPage;
}
else{
this.start=this.current-2;
this.end=this.current+2;
if(this.start<=0){
this.start=1;
this.end=5;
}
if(this.end>this.totalPage){
this.end=this.totalPage;
this.start=this.end-5;
}
}
}
}
freemark页面
<#list mesgList as mesg>
<tr>
<td><button onClick="dealMsg(this)">${mesg.msgNo}</button></td>
<td>${mesg.msgName}</td>
<td>${mesg.msgSerl}</td>
<td>${mesg.orgMsgSerl}</td>
<td>${mesg.comSerl}</td>
<td>${mesg.comSerlRef}</td>
<td style="display:none;">${mesg.transType}</td>
<td>${mesg.transTypeStr}</td>
<td>${mesg.sendDate}</td>
<td>${mesg.msgStateName}</td>
</tr>
</#list>
<div>
共有${page.totalCount}条记录,共${page.totalPage}页,现在是第${page.current}页
<a href="javascript:indexPage()">首页</a>
<#if page.current!=1>
<a href="javascript:lastPage(${page.current-1})">上一页</a>
</#if>
<#list page.start..page.end as i>
<#if i==page.current>
${i}
<#else>
<a href="javascript:lastPage(${i})">${i}</a>
</#if>
</#list>
<#if page.current!=page.end>
<a href="javascript:lastPage(${page.current+1})">下一页</a>
</#if>
<a href="javascript:lastPage(${page.totalPage})">尾页</a>
controller层
@RequestMapping("/list")
public String list(ModelMap map, Model model, HttpServletRequest request) {
String scheme = request.getScheme();
String serverName = request.getServerName();
int port = request.getServerPort();
String path = request.getContextPath();
String basePath = scheme + "://" + serverName + ":" + port + path;
System.out.println(basePath);
request.setAttribute("basePath", basePath);
long msgState = -1;
String msgType = request.getParameter("msgtype");
String queryMsgNo = request.getParameter("queryMsgNo");
MesgQueryVo vo=new MesgQueryVo();
Page<Mesg> pageBean=new Page<Mesg>();
int pageNum=pageBean.getCurrent();
if (request.getParameter("pageNum") != null&&!"".equals(request.getParameter("pageNum"))){
pageNum = Integer.parseInt(request.getParameter("pageNum"));
}
vo.setPageNum(pageNum);
vo.setNumPerPage(pageBean.getNumPerPage());
if (request.getParameter("msgState") != null && !"".equals(request.getParameter("msgState"))) {
msgState = Long.parseLong(request.getParameter("msgState"));
}
//Map<String, Object> param = new HashMap<String, Object>();
if (request.getParameter("msgState") != null && !"".equals(request.getParameter("msgState"))) {
vo.setMsgState(msgState);
}
if (StringUtils.isNotEmpty(msgType)) {
msgType = msgType.toUpperCase();
vo.setMsgtype(msgType);
}
if(StringUtils.isNotEmpty(queryMsgNo)){
vo.setQueryMsgNo(queryMsgNo);
}
Page page = mesgService.findByPage(vo);
List<Mesg> mesgList=page.getMesgs();
if (mesgList != null && mesgList.size() > 0) {
for (int i = 0; i < mesgList.size(); i++) {
if (mesgList.get(i).getMsgState() != null && mesgList.get(i).getMsgState() >= 0) {
String msgStateName = MessageStatus.getResult(mesgList.get(i).getMsgState()).getDescription();
mesgList.get(i).setMsgStateName(msgStateName);
}
if (mesgList.get(i).getMsgNo() != null && !"".equals(mesgList.get(i).getMsgNo())) {
MesgType type = null;
try {
type = (MesgType) MesgType.class
.getField(mesgList.get(i).getMsgNo().substring(0, 7).replace(".", "")).get(null);
mesgList.get(i).setMsgName(type.getDescription());
} catch (Exception e) {
System.err.println("查询异常,数据中存在报文类型空的");
}
}
mesgList.get(i).setTransTypeStr(getTransTypeStr(String.valueOf(mesgList.get(i).getTransType())));
}
}
map.addAttribute("page", page);
map.addAttribute("mesgList", mesgList);
map.addAttribute("msgState", msgState);
map.addAttribute("msgType", msgType);
map.addAttribute("queryMsgNo", queryMsgNo);
return "/mesg/view/list";
}
service层
public Page findByPage(MesgQueryVo vo) {
int numPerPage=vo.getNumPerPage();
int pageNum=vo.getPageNum();
int totalCount = userMapper.getItemsCount(vo);
Page page=new Page(totalCount,pageNum);
if(page.getEnd()<3){
page.setCurrent(1);
vo.setPageNum(1);
}
List list = userMapper.findByPage(vo);
page.setMesgs(list);//设置当前页面数据
return page;
}