本篇文章給大家分享的是有關(guān)使用SSM框架怎么實現(xiàn)一個分頁和搜索分頁功能,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

為魚臺等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及魚臺網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站制作、網(wǎng)站設(shè)計、魚臺網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
1.分頁
import java.io.Serializable;
/**
* 分頁
*/
public class Page implements Serializable {
private static final long serialVersionUID = -3198048449643774660L;
private int pageNow = 1; // 當前頁數(shù)
private int pageSize = 4; // 每頁顯示記錄的條數(shù)
private int totalCount; // 總的記錄條數(shù)
private int totalPageCount; // 總的頁數(shù)
@SuppressWarnings("unused")
private int startPos; // 開始位置,從0開始
@SuppressWarnings("unused")
private boolean hasFirst;// 是否有首頁
@SuppressWarnings("unused")
private boolean hasPre;// 是否有前一頁
@SuppressWarnings("unused")
private boolean hasNext;// 是否有下一頁
@SuppressWarnings("unused")
private boolean hasLast;// 是否有最后一頁
/**
* 通過構(gòu)造函數(shù) 傳入 總記錄數(shù) 和 當前頁
* @param totalCount
* @param pageNow
*/
public Page(int totalCount, int pageNow) {
this.totalCount = totalCount;
this.pageNow = pageNow;
}
/**
* 取得總頁數(shù),總頁數(shù)=總記錄數(shù)/總頁數(shù)
* @return
*/
public int getTotalPageCount() {
totalPageCount = getTotalCount() / getPageSize();
return (totalCount % pageSize == 0) ? totalPageCount
: totalPageCount + 1;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
/**
* 取得選擇記錄的初始位置
* @return
*/
public int getStartPos() {
return (pageNow - 1) * pageSize;
}
public void setStartPos(int startPos) {
this.startPos = startPos;
}
/**
* 是否是第一頁
* @return
*/
public boolean isHasFirst() {
return (pageNow == 1) ? false : true;
}
public void setHasFirst(boolean hasFirst) {
this.hasFirst = hasFirst;
}
/**
* 是否有首頁
* @return
*/
public boolean isHasPre() {
// 如果有首頁就有前一頁,因為有首頁就不是第一頁
return isHasFirst() ? true : false;
}
public void setHasPre(boolean hasPre) {
this.hasPre = hasPre;
}
/**
* 是否有下一頁
* @return
*/
public boolean isHasNext() {
// 如果有尾頁就有下一頁,因為有尾頁表明不是最后一頁
return isHasLast() ? true : false;
}
public void setHasNext(boolean hasNext) {
this.hasNext = hasNext;
}
/**
* 是否有尾頁
* @return
*/
public boolean isHasLast() {
// 如果不是最后一頁就有尾頁
return (pageNow == getTotalCount()) ? false : true;
}
public void setHasLast(boolean hasLast) {
this.hasLast = hasLast;
}
}有了這個工具類后,首先編寫MyBatis的XxxxMapper.xml配置文件中的SQL語句,如下:
<!-- 分頁SQL語句 -->
<select id="selectProductsByPage" resultMap="返回值類型">
select
*
from 表名 WHERE user_id = #{userId,jdbcType=INTEGER} limit #{startPos},#{pageSize}
</select>
<!-- 取得記錄的總數(shù) -->
<select id="getProductsCount" resultType="long">
SELECT COUNT(*) FROM 表名 WHERE user_id = #{userId,jdbcType=INTEGER}
</select>此處我們可以看到,2個<select>需要分別傳入3個和1個參數(shù),此時在對應(yīng)的DAO文件IXxxxDao中編寫接口來編寫對應(yīng)的方法,方法名和mapper.xml中的id屬性值一致:
/**
* 使用注解方式傳入多個參數(shù),用戶產(chǎn)品分頁,通過登錄用戶ID查詢
* @param page
* @param userId
* @return startPos},#{pageSize}
*/
public List<Products> selectProductsByPage(@Param(value="startPos") Integer startPos,@Param(value="pageSize") Integer pageSize,@Param(value="userId") Integer userId);
/**
* 取得產(chǎn)品數(shù)量信息,通過登錄用戶ID查詢
* @param userId
* @return
*/
public long getProductsCount(@Param(value="userId") Integer userId);接口定義完成之后需要編寫相應(yīng)的業(yè)務(wù)接口和實現(xiàn)方法,在接口中定義這樣一個方法,然后實現(xiàn)類中覆寫一下:
/** * 分頁顯示商品 * @param request * @param model * @param loginUserId */ void showProductsByPage(HttpServletRequest request,Model model,int loginUserId);
接下來實現(xiàn)類中的方法就是要調(diào)用DAO層和接受Controller傳入的參數(shù),進行業(yè)務(wù)邏輯的處理,request用來獲取前端傳入的參數(shù),model用來向JSP頁面返回處理結(jié)果。
@Override
public void showProductsByPage(HttpServletRequest request, Model model,int loginUserId) {
String pageNow = request.getParameter("pageNow");
Page page = null;
List<ProductWithBLOBs> products = new ArrayList<ProductWithBLOBs>();
int totalCount = (int) productDao.getProductsCount(loginUserId);
if (pageNow != null) {
page = new Page(totalCount, Integer.parseInt(pageNow));
allProducts = this.productDao.selectProductsByPage(page.getStartPos(), page.getPageSize(), loginUserId);
} else {
page = new Page(totalCount, 1);
allProducts = this.productDao.selectProductsByPage(page.getStartPos(), page.getPageSize(), loginUserId);
}
model.addAttribute("products", products);
model.addAttribute("page", page);
}接下來是控制器的編寫,當用戶需要跳轉(zhuǎn)到這個現(xiàn)實產(chǎn)品的頁面時,就需要經(jīng)過這個控制器中相應(yīng)方法的處理,這個處理過程就是調(diào)用業(yè)務(wù)層的方法來完成,然后返回結(jié)果到JSP動態(tài)顯示,服務(wù)器端生成好頁面后傳給客戶端(瀏覽器)現(xiàn)實,這就是一個MVC過程。
/**
* 初始化 “我的產(chǎn)品”列表 JSP頁面,具有分頁功能
*
* @param request
* @param model
* @return
*/
@RequestMapping(value = "映射路徑", method = RequestMethod.GET)
public String showMyProduct(HttpServletRequest request, Model model) {
// 取得SESSION中的loginUser
User loginUser = (User) request.getSession().getAttribute("loginUser");
// 判斷SESSION是否失效
if (loginUser == null || "".equals(loginUser)) {
return "redirect:/";
}
int loginUserId = loginUser.getUserId();
//此處的productService是注入的IProductService接口的對象
this.productService.showProductsByPage(request, model, loginUserId);
return "跳轉(zhuǎn)到的JSP路徑";
}JSP頁面接受部分我就不寫了,每個人都一樣,也就是結(jié)合JSTL和EL來寫,(在循環(huán)輸出的時候也做了判斷,如果接受的參數(shù)為空,那么輸出暫無商品,只有接受的參數(shù)不為空的時候,才循環(huán)輸出,使用<<c:when test="${}">結(jié)合<c:otherwise>),這里只給出分頁的相關(guān)代碼:
<!-- 分頁功能 start -->
<div align="center">
<font size="2">共 ${page.totalPageCount} 頁</font> <font size="2">第
${page.pageNow} 頁</font> <a href="myProductPage?pageNow=1" rel="external nofollow" rel="external nofollow" >首頁</a>
<c:choose>
<c:when test="${page.pageNow - 1 > 0}">
<a href="myProductPage?pageNow=${page.pageNow - 1}" rel="external nofollow" >上一頁</a>
</c:when>
<c:when test="${page.pageNow - 1 <= 0}">
<a href="myProductPage?pageNow=1" rel="external nofollow" rel="external nofollow" >上一頁</a>
</c:when>
</c:choose>
<c:choose>
<c:when test="${page.totalPageCount==0}">
<a href="myProductPage?pageNow=${page.pageNow}" rel="external nofollow" rel="external nofollow" >下一頁</a>
</c:when>
<c:when test="${page.pageNow + 1 < page.totalPageCount}">
<a href="myProductPage?pageNow=${page.pageNow + 1}" rel="external nofollow" >下一頁</a>
</c:when>
<c:when test="${page.pageNow + 1 >= page.totalPageCount}">
<a href="myProductPage?pageNow=${page.totalPageCount}" rel="external nofollow" rel="external nofollow" >下一頁</a>
</c:when>
</c:choose>
<c:choose>
<c:when test="${page.totalPageCount==0}">
<a href="myProductPage?pageNow=${page.pageNow}" rel="external nofollow" rel="external nofollow" >尾頁</a>
</c:when>
<c:otherwise>
<a href="myProductPage?pageNow=${page.totalPageCount}" rel="external nofollow" rel="external nofollow" >尾頁</a>
</c:otherwise>
</c:choose>
</div>
<!-- 分頁功能 End -->2.查詢分頁
關(guān)于查詢分頁,大致過程完全一樣,只是第三個參數(shù)(上面是loginUserId)需要接受用戶輸入的參數(shù),這樣的話我們需要在控制器中接受用戶輸入的這個參數(shù)(頁面中的<input>使用GET方式傳參),然后將其加入到SESSION中,即可完成查詢分頁(此處由于“下一頁”這中超鏈接的原因,使用了不同的JSP頁面處理分頁和搜索分頁,暫時沒找到在一個JSP頁面中完成的方法,出現(xiàn)了重復代碼,這里的重復代碼就是輸出內(nèi)容的那段代碼,可以單獨拿出去,然后用一個<include>標簽加載到需要的JSP頁面就可以了,這樣可以避免代碼重復):
這里給出控制器的代碼作為參考:
/**
* 通過 產(chǎn)品名稱 查詢產(chǎn)品
* @param request
* @param model
* @return
*/
@RequestMapping(value = "映射地址", method = RequestMethod.GET)
public String searchForProducts(HttpServletRequest request, Model model) {
HttpSession session = request.getSession();
String param = request.getParameter("param");
String condition = (String) session.getAttribute("condition");
//先判斷SESSION中的condition是否為空
if (condition == null) {
condition = new String();
session.setAttribute("condition", condition);
//如果Session中的condition為空,再判斷傳入的參數(shù)是否為空,如果為空就跳轉(zhuǎn)到搜索結(jié)果頁面
if (param == null || "".equals(param)) {
return "private/space/ProductSearchResult";
}
}
//如果SESSION不為空,且傳入的搜索條件param不為空,那么將param賦值給condition
if (param != null && !("".equals(param))) {
condition = param;
session.setAttribute("condition", condition);
}
//使用session中的condition屬性值來作為查詢條件
this.productService.showSearchedProductsByPage(request, model, condition);
return "跳轉(zhuǎn)的頁面";
}以上就是使用SSM框架怎么實現(xiàn)一個分頁和搜索分頁功能,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁名稱:使用SSM框架怎么實現(xiàn)一個分頁和搜索分頁功能
文章分享:http://chinadenli.net/article0/gspsio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、面包屑導航、網(wǎng)站內(nèi)鏈、網(wǎng)站建設(shè)、ChatGPT、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)