内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

使用SSM框架怎么實(shí)現(xiàn)一個(gè)分頁(yè)和搜索分頁(yè)功能

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

為魚臺(tái)等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及魚臺(tái)網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、魚臺(tái)網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

1.分頁(yè)

import java.io.Serializable; 
/** 
* 分頁(yè) 
*/ 
public class Page implements Serializable {  
  private static final long serialVersionUID = -3198048449643774660L;  
  private int pageNow = 1; // 當(dāng)前頁(yè)數(shù)  
  private int pageSize = 4; // 每頁(yè)顯示記錄的條數(shù)  
  private int totalCount; // 總的記錄條數(shù) 
  private int totalPageCount; // 總的頁(yè)數(shù)  
  @SuppressWarnings("unused") 
  private int startPos; // 開始位置,從0開始 
 
  @SuppressWarnings("unused") 
  private boolean hasFirst;// 是否有首頁(yè) 
 
  @SuppressWarnings("unused") 
  private boolean hasPre;// 是否有前一頁(yè) 
 
  @SuppressWarnings("unused") 
  private boolean hasNext;// 是否有下一頁(yè) 
 
  @SuppressWarnings("unused") 
  private boolean hasLast;// 是否有最后一頁(yè)    
  /** 
  * 通過構(gòu)造函數(shù) 傳入 總記錄數(shù) 和 當(dāng)前頁(yè) 
  * @param totalCount 
  * @param pageNow 
  */ 
  public Page(int totalCount, int pageNow) { 
    this.totalCount = totalCount; 
    this.pageNow = pageNow; 
  } 
   
  /** 
  * 取得總頁(yè)數(shù),總頁(yè)數(shù)=總記錄數(shù)/總頁(yè)數(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; 
  } 
 
  /** 
  * 是否是第一頁(yè) 
  * @return 
  */ 
  public boolean isHasFirst() { 
    return (pageNow == 1) ? false : true; 
  } 
 
  public void setHasFirst(boolean hasFirst) { 
    this.hasFirst = hasFirst; 
  } 
  /** 
  * 是否有首頁(yè) 
  * @return 
  */ 
  public boolean isHasPre() { 
    // 如果有首頁(yè)就有前一頁(yè),因?yàn)橛惺醉?yè)就不是第一頁(yè) 
    return isHasFirst() ? true : false; 
  } 
 
  public void setHasPre(boolean hasPre) { 
    this.hasPre = hasPre; 
  } 
  /** 
  * 是否有下一頁(yè) 
  * @return 
  */ 
  public boolean isHasNext() { 
    // 如果有尾頁(yè)就有下一頁(yè),因?yàn)橛形岔?yè)表明不是最后一頁(yè) 
    return isHasLast() ? true : false; 
  } 
 
  public void setHasNext(boolean hasNext) { 
    this.hasNext = hasNext; 
  } 
  /** 
  * 是否有尾頁(yè) 
  * @return 
  */ 
  public boolean isHasLast() { 
    // 如果不是最后一頁(yè)就有尾頁(yè) 
    return (pageNow == getTotalCount()) ? false : true; 
  } 
 
  public void setHasLast(boolean hasLast) { 
    this.hasLast = hasLast; 
  }  
}

有了這個(gè)工具類后,首先編寫MyBatis的XxxxMapper.xml配置文件中的SQL語(yǔ)句,如下:

<!-- 分頁(yè)SQL語(yǔ)句 --> 
<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個(gè)<select>需要分別傳入3個(gè)和1個(gè)參數(shù),此時(shí)在對(duì)應(yīng)的DAO文件IXxxxDao中編寫接口來編寫對(duì)應(yīng)的方法,方法名和mapper.xml中的id屬性值一致:

/** 
* 使用注解方式傳入多個(gè)參數(shù),用戶產(chǎn)品分頁(yè),通過登錄用戶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ù)接口和實(shí)現(xiàn)方法,在接口中定義這樣一個(gè)方法,然后實(shí)現(xiàn)類中覆寫一下:

/** 
  * 分頁(yè)顯示商品 
  * @param request 
  * @param model 
  * @param loginUserId 
  */ 
  void showProductsByPage(HttpServletRequest request,Model model,int loginUserId);

接下來實(shí)現(xiàn)類中的方法就是要調(diào)用DAO層和接受Controller傳入的參數(shù),進(jìn)行業(yè)務(wù)邏輯的處理,request用來獲取前端傳入的參數(shù),model用來向JSP頁(yè)面返回處理結(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); 
}

接下來是控制器的編寫,當(dāng)用戶需要跳轉(zhuǎn)到這個(gè)現(xiàn)實(shí)產(chǎn)品的頁(yè)面時(shí),就需要經(jīng)過這個(gè)控制器中相應(yīng)方法的處理,這個(gè)處理過程就是調(diào)用業(yè)務(wù)層的方法來完成,然后返回結(jié)果到JSP動(dòng)態(tài)顯示,服務(wù)器端生成好頁(yè)面后傳給客戶端(瀏覽器)現(xiàn)實(shí),這就是一個(gè)MVC過程。

/** 
* 初始化 “我的產(chǎn)品”列表 JSP頁(yè)面,具有分頁(yè)功能 
* 
* @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接口的對(duì)象 
  this.productService.showProductsByPage(request, model, loginUserId); 
 
  return "跳轉(zhuǎn)到的JSP路徑"; 
}

JSP頁(yè)面接受部分我就不寫了,每個(gè)人都一樣,也就是結(jié)合JSTL和EL來寫,(在循環(huán)輸出的時(shí)候也做了判斷,如果接受的參數(shù)為空,那么輸出暫無商品,只有接受的參數(shù)不為空的時(shí)候,才循環(huán)輸出,使用<<c:when test="${}">結(jié)合<c:otherwise>),這里只給出分頁(yè)的相關(guān)代碼:

<!-- 分頁(yè)功能 start --> 
  <div align="center"> 
    <font size="2">共 ${page.totalPageCount} 頁(yè)</font> <font size="2">第 
      ${page.pageNow} 頁(yè)</font> <a href="myProductPage?pageNow=1" rel="external nofollow" rel="external nofollow" >首頁(yè)</a> 
    <c:choose> 
      <c:when test="${page.pageNow - 1 > 0}"> 
        <a href="myProductPage?pageNow=${page.pageNow - 1}" rel="external nofollow" >上一頁(yè)</a> 
      </c:when> 
      <c:when test="${page.pageNow - 1 <= 0}"> 
        <a href="myProductPage?pageNow=1" rel="external nofollow" rel="external nofollow" >上一頁(yè)</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" >下一頁(yè)</a> 
      </c:when> 
      <c:when test="${page.pageNow + 1 < page.totalPageCount}"> 
        <a href="myProductPage?pageNow=${page.pageNow + 1}" rel="external nofollow" >下一頁(yè)</a> 
      </c:when> 
      <c:when test="${page.pageNow + 1 >= page.totalPageCount}"> 
        <a href="myProductPage?pageNow=${page.totalPageCount}" rel="external nofollow" rel="external nofollow" >下一頁(yè)</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" >尾頁(yè)</a> 
      </c:when> 
      <c:otherwise> 
        <a href="myProductPage?pageNow=${page.totalPageCount}" rel="external nofollow" rel="external nofollow" >尾頁(yè)</a> 
      </c:otherwise> 
    </c:choose> 
  </div> 
  <!-- 分頁(yè)功能 End -->

2.查詢分頁(yè)

關(guān)于查詢分頁(yè),大致過程完全一樣,只是第三個(gè)參數(shù)(上面是loginUserId)需要接受用戶輸入的參數(shù),這樣的話我們需要在控制器中接受用戶輸入的這個(gè)參數(shù)(頁(yè)面中的<input>使用GET方式傳參),然后將其加入到SESSION中,即可完成查詢分頁(yè)(此處由于“下一頁(yè)”這中超鏈接的原因,使用了不同的JSP頁(yè)面處理分頁(yè)和搜索分頁(yè),暫時(shí)沒找到在一個(gè)JSP頁(yè)面中完成的方法,出現(xiàn)了重復(fù)代碼,這里的重復(fù)代碼就是輸出內(nèi)容的那段代碼,可以單獨(dú)拿出去,然后用一個(gè)<include>標(biāo)簽加載到需要的JSP頁(yè)面就可以了,這樣可以避免代碼重復(fù)):

這里給出控制器的代碼作為參考:

/** 
  * 通過 產(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é)果頁(yè)面 
      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)的頁(yè)面"; 
  }

以上就是使用SSM框架怎么實(shí)現(xiàn)一個(gè)分頁(yè)和搜索分頁(yè)功能,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁(yè)名稱:使用SSM框架怎么實(shí)現(xiàn)一個(gè)分頁(yè)和搜索分頁(yè)功能
文章分享:http://m.rwnh.cn/article0/gspsio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、面包屑導(dǎo)航、網(wǎng)站內(nèi)鏈、網(wǎng)站建設(shè)ChatGPT、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)
新安县| 九台市| 天等县| 天台县| 桃园县| 名山县| 交城县| 邛崃市| 醴陵市| 日照市| 赫章县| 余江县| 丹巴县| 乐安县| 嫩江县| 高阳县| 洛阳市| 探索| 赣州市| 吴堡县| 瓦房店市| 来宾市| 岳阳县| 元阳县| 沅陵县| 唐山市| 上栗县| 五家渠市| 察哈| 本溪| 留坝县| 古交市| 吐鲁番市| 武威市| 邵阳县| 武安市| 徐水县| 曲水县| 赞皇县| 陈巴尔虎旗| 拉孜县|