1. 程式人生 > >基於freemark,ssm和Oracle實現的分頁功能

基於freemark,ssm和Oracle實現的分頁功能

freemark頁面`<#list mesgList as mesg>

m e s g . m s

g N o &lt; / b u t t o
n &gt; &lt; / t d &gt; &lt; t d
&gt; {mesg.msgNo}&lt;/button&gt;&lt;/td&gt; &lt;td&gt; {mesg.msgName}
m e s g . m s g S e r l &lt; / t d &gt; &lt; t d &gt; {mesg.msgSerl}&lt;/td&gt; &lt;td&gt; {mesg.orgMsgSerl}
m e s g . c o m S e r l &lt; / t d &gt; &lt; t d &gt; {mesg.comSerl}&lt;/td&gt; &lt;td&gt; {mesg.comSerlRef}
m e s g . t r a n s T y p e &lt; / t d &gt; &lt; t d &gt; {mesg.transType}&lt;/td&gt; &lt;td&gt; {mesg.transTypeStr}
m e s g . s e n d D a t e &lt; / t d &gt; &lt; t d &gt; {mesg.sendDate}&lt;/td&gt; &lt;td&gt; {mesg.msgStateName}

    </tr>
    </#list>
    <div >	    

<#if page.totalCount !=0>
p a g e . c u r r e n t / {page.current}頁/共 {page.end}頁
首頁
上一頁

<a href="
	<#if page.current != page.end>
		javascript:nextPage(${page.current+1});
	<#else>
		#
	</#if>
">下一頁</a>   
<a href="javascript:finalPage(${page.end})">尾頁</a>
<input type="text" id="numId"/>
<input type="button" id="button" onclick="clickIt()" value="查詢"/><br/>

</#if>`

<script language= "javascript" >
      $(document).ready(function(){
          var x = document.getElementById("up").innerHTML;
          $("#msgState").val(x);
      });
    function clickIt(){
      var msgState = document.getElementById("msgState").value;
  
      var id=document.getElementById("numId").value;
      form.action='list?pageNum='+id+'&msgState='+msgState;
      form.submit();
      }
      function indexPage(){
      var msgState = document.getElementById("msgState").value;
        form.action='list?pageNum=1&msgState='+msgState;
        form.submit();
      }
      function lastPage(pageNum){
      var msgState = document.getElementById("msgState").value;
      form.action='list?pageNum='+pageNum+'&msgState='+msgState;
      form.submit();
      }
      function nextPage(pageNum){
      var msgState = document.getElementById("msgState").value;
      form.action='list?pageNum='+pageNum+'&msgState='+msgState;
      form.submit();
      }
      function finalPage(pageNum){
      var msgState = document.getElementById("msgState").value;
      form.action='list?pageNum='+pageNum+'&msgState='+msgState;
      form.submit();
      }
     

	function dealMsg(temp) {
		var a = new Array();
		var i = 0;
		$(temp).parent().parent().children('td').each(function() {
		a[i] = $(temp).parent().parent().children("td").eq(i).text();
		i++;
		})
		form.action = 'deal?msgNo='+a[0]+'&msgSerl='+a[2]+'&orgMsgSerl='+a[3]+'&comSerl='+a[4]+'&comSerlRef='+a[5]+'&transType='+a[6]+'&sendDate='+a[8];
	    form.submit();
}

function qry() {
		var msgState = document.getElementById("msgState").value;
		
		form.action = 'list?msgState='+msgState;
	    form.submit();
}

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");
		//sang
				int pageNum=3;
				if (request.getParameter("pageNum") != null&&!"".equals(request.getParameter("pageNum"))){
					pageNum = Integer.parseInt(request.getParameter("pageNum"));
				}
			

		if (request.getParameter("msgState") != null && !"".equals(request.getParameter("msgState"))) {
			msgState = Long.parseLong(request.getParameter("msgState"));
		}

		//Map<String, Object> param = new HashMap<String, Object>();
		MesgQueryVo vo=new MesgQueryVo();
		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);
		}
		if (request.getParameter("pageNum") != null&&!"".equals(request.getParameter("pageNum"))){
			vo.setPageNum(pageNum);
		}
		else{
			vo.setPageNum(3);
		}
	    vo.setNumPerPage(15);
		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();
		Page page = new Page();
		page.setNumPerPage(numPerPage);
		page.setCurrent(pageNum);
		
		int totalCount = userMapper.getItemsCount(vo);
		page.setTotalCount(totalCount);//設定總記錄數
		if(totalCount%numPerPage!=0){
			page.setEnd(totalCount/numPerPage+1);
		}else{
			page.setEnd(totalCount/numPerPage);
		}
		if(page.getEnd()<3){
			page.setCurrent(1);
			vo.setPageNum(1);
		}
				
		int start = (pageNum - 1)*numPerPage;
		List list = userMapper.findByPage(vo);
		page.setMesgs(list);//設定當前頁面資料 
		
		return page;
	}

mapper層

<select id="findByPage"  resultType="mock.model.Mesg" parameterType="mock.constant.MesgQueryVo">
	select * from(select rownum rm,s.COM_SERL as comSerl,
	s.MSG_SERL as msgSerl,
	s.COM_SERL_REF as comSerlRef,
	s.ORG_MSG_SERL as orgMsgSerl,
	s.MSG_NO as msgNo,
	s.TRANS_TYPE as transType,
	to_char(s.SEND_DATE,'yyyy-mm-dd HH24:MI:SS') as sendDate,
	s.MESSAGE_STATUS as msgState
	from CPEC_MIDDLE_REQ_MSG s
	where
	s.STATUS =1
	<if test="msgtype != null and msgtype !='' ">
	and s.MSG_NO like '%'||#{msgtype}||'%'
	</if>
	<if test="msgState != -1 ">
	and s.MESSAGE_STATUS= #{msgState}
	</if>
	<if test="queryMsgNo !=null and queryMsgNo !='' ">
	and s.MSG_SERL like '%'||#{queryMsgNo}||'%'
	</if>
	order by s.INPUT_TIME desc) where rm&gt;(${pageNum}-1)*${numPerPage} and rm&lt;=${pageNum}*${numPerPage}
	</select>

突然意識到當前頁和每頁數·可以在page類中指定

package mock.model;

import java.util.List;

public class Page {
	private int current=3;//��ǰҳ
	private int end;//βҳ
	private int numPerPage=15;//ÿҳ��¼��
	private int totalCount;//�ܼ�¼��
	private List<Mesg> Mesgs;//��ǰҳ���� 
	public int getCurrent() {
		return current;
	}
	public void setCurrent(int current) {
		this.current = current;
	}
	public int getEnd() {
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	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 List<Mesg> getMesgs() {
		return Mesgs;
	}
	public void setMesgs(List<Mesg> mesgs) {
		Mesgs = mesgs;
	}
	@Override
	public String toString() {
		return "Page [current=" + current + ", end=" + end + ", numPerPage=" + numPerPage + ", totalCount=" + totalCount
				+ ", Mesgs=" + Mesgs + "]";
	}
	
	
}

freemark中的上一頁下一頁可以用c:if標籤控制是否顯示