jsp分頁的處理(SSM框架舉例)
阿新 • • 發佈:2019-01-30
首先我們需要一個工具類Page類來表達:
import java.util.List; public class Page { private int everyPage = 10; //每頁顯示的條數 private int totalCount; //總共需要顯示的條數 private int totalPage; //總共需要顯示的頁數 private int beginPage = 1; //開始的頁數 private boolean hasPrePage; //是否有上一頁 private boolean hasNextPage; //是否有下一頁 private List list; //用來儲存的資料結構(統一為一個list) /** * 初始化:當獲得了總共需要顯示的條數後即可執行這個方法 */ public void init(){ int totalPage = totalCount % everyPage == 0 ? totalCount / everyPage : totalCount / everyPage + 1; this.setTotalPage(totalPage); this.setHasNextPage(beginPage >= totalCount); this.setHasPrePage(beginPage <= 1); } public int getEveryPage() { return this.everyPage; } public void setEveryPage(int everyPage) { this.everyPage = everyPage; } public int getTotalCount() { return this.totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getTotalPage() { return this.totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getBeginPage() { return this.beginPage; } public void setBeginPage(int beginPage) { this.beginPage = beginPage; } public boolean isHasPrePage() { return this.hasPrePage; } public void setHasPrePage(boolean hasPrePage) { this.hasPrePage = hasPrePage; } public boolean isHasNextPage() { return this.hasNextPage; } public void setHasNextPage(boolean hasNextPage) { this.hasNextPage = hasNextPage; } public List getList() { return this.list; } public void setList(List list) { this.list = list; } }
假設你有這麼一個類Microblog,其結構如下:
public class Microblog { private Integer id; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column microblog.userId * 這條微博現在屬於誰 * @mbggenerated */ private Integer userid; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column microblog.content * 這條微博的內容 * @mbggenerated */ private String content; /** * This field was generated by MyBatis Generator. * This field corresponds to the database column microblog.time * 這條微博現在釋出的時間 * @mbggenerated */ private Date time; /** * 這條微博是否是轉載的,是轉載的為true,否則為false */ private Boolean isForward; private Integer sourceuserid; /** * 這條部落格的原創部落格的id(原創的話即為0) */ private Integer sourceMicroblogId; /** * 你轉發的那條微博(不一定是原創的那一條,原創的話即為0) */ private Integer forwardMicroblogId; /*接下來就是相應的get和set方法,博主在此省略*/ }
接下來看看底層如何獲取這些資料,(假設你此時需要取得一條微博的所有轉發微博)xml檔案對應的mybatis程式碼為:
<select id="selectPageByForwardMicroblogId" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from microblog where forwardMicroblogId = #{0,jdbcType=INTEGER} order by time desc limit #{1,jdbcType=INTEGER},#{2,jdbcType=INTEGER} </select>
其對應介面中的方法宣告為:(介面為MicroblogMapper)
/**
* 檢視當前頁面的轉發評論
* @param forwardMicroblogId
* @param beginPage 從第幾個開始尋找幾個,這個是從0開始的(並不是第幾頁)
* @param everyPage 一頁多少條記錄
* @return
*/
List<Microblog> selectPageByForwardMicroblogId(Integer forwardMicroblogId, int beginPage, int everyPage);
Service層的寫法為:
List<Microblog> microblogs = this.microblogMapper.selectPageByForwardMicroblogId(
microblog.getForwardMicroblogId(), (page.getBeginPage() - 1)*page.getEveryPage(), page.getEveryPage());
int totalCount = this.microblogMapper.countByForwardMicroblogId(microblog.getForwardMicroblogId());
page.setList(microblogs);
page.setTotalCount(totalCount);
page.init();
Controller層的寫法:
@RequestMapping("user/microblog_showForwardMore")
public String showForwardMore(Microblog microblog, Page page, ModelMap modelMap){
page = this.microblogService.getTenByForward(microblog,page);
Microblog mic = this.microblogService.findOne(microblog.getForwardMicroblogId());
modelMap.addAttribute("microblog", mic);
modelMap.addAttribute("Page", page);
modelMap.addAttribute("partSelect",1);
return "/user/showMore";
}
接下來,你再將page傳給頁面,定義為“Page”(博主並沒有轉化為JSON資料,因為暫時還沒有接觸,但其實變成JSON更好,因為這樣在使用前臺的框架時可以更加方便並且可以更加通用,假如你的前臺並不是jsp),其使用大致為
<c:forEach var="m" items="${Page.list}">
<div style="border: 1px solid black">
<a href="user_scan.action?id=${m.sourceUser.id}">@${m.sourceUser.name}</a>:${m.forwardRemark }
<c:forEach items="${m.forwardRemarks}" var="r">
<a href="user_scan.action?id=${r.userId}">@${r.userName }</a>${r.remark }
</c:forEach>
time:${m.getNTime()}<br /> <a href="#"
onclick="prom(${m.sourceMicroblogId==0?m.id:m.sourceMicroblogId }, ${m.id})">
(轉發${m.getForwards() }) </a> <br />
</div>
</c:forEach>
取資料是比較簡單的,因為可以利用各種前臺標籤(SSM貌似支援最基礎的C標籤,如果大家知道可以用其他標籤的話,請告訴博主,博主將感激不盡),主要說一下首頁、上一頁、下一頁的顯示:<a
href="microblog_showForwardMore.action?forwardMicroblogId=${Microblog.forwardMicroblogId }&sourceMicroblogId=${Microblog.sourceMicroblogId}&beginPage=1"><input
type="button" class="className" value=" 首 頁 " /> </a>
<c:choose>
<c:when test="${Page.beginPage == 1}">
<input type="button" class="className" style="width: 55px;"
value="上一頁" />
</c:when>
<c:otherwise>
<a
href="microblog_showForwardMore.action?forwardMicroblogId=${Microblog.forwardMicroblogId }&sourceMicroblogId=${Microblog.sourceMicroblogId}&beginPage=${Page.beginPage-1}"><input
type="button" class="className" style="width: 55px;" value="上一頁" /></a>
</c:otherwise>
</c:choose>
共
${Page.totalCount }條記錄,
每頁
${Page.getEveryPage() }條
${Page.beginPage }/${Page.totalPage}
<c:choose>
<c:when test="${Page.beginPage >= Page.totalPage}">
<input type="button" class="className" style="width: 55px;"
value="下一頁" />
</c:when>
<c:otherwise>
<a
href="microblog_showForwardMore.action?forwardMicroblogId=${Microblog.forwardMicroblogId }&sourceMicroblogId=${Microblog.sourceMicroblogId}&beginPage=${Page.beginPage+1}"><input
type="button" class="className" style="width: 55px;" value="下一頁" /></a>
</c:otherwise>
</c:choose>
<a
href="microblog_showForwardMore.action?forwardMicroblogId=${Microblog.forwardMicroblogId }&sourceMicroblogId=${Microblog.sourceMicroblogId}&beginPage=${Page.totalPage } "> <input
type="button" class="className" value="尾 頁" /></a>
點選首頁這些按鈕時,改變的只有一個beginPage屬性而已,請謹記。
總結:博主最開始的分頁是在利用struts2寫的,當時看了一個晚上,結果寫的時候還是磕磕絆絆,但是當你真正實現了一個之後,再看別人寫的就不再那麼困難了,所以“千里之行始於足下”,希望大家能早日完成自己的功能!