細心!用心!耐心!

          吾非文人,乃市井一俗人也,讀百卷書,跨江河千里,故申城一游; 一兩滴辛酸,三四年學業,五六點粗墨,七八筆買賣,九十道人情。

          BlogJava 聯系 聚合 管理
            1 Posts :: 196 Stories :: 10 Comments :: 0 Trackbacks
          ??1 package ?com.stt.doss.common.util;
          ??2
          ??3 import ?java.io.IOException;
          ??4 import ?java.util.ArrayList;
          ??5 import ?java.util.Collection;
          ??6 import ?java.util.Enumeration;
          ??7
          ??8 import ?javax.servlet.http.HttpServletRequest;
          ??9 import ?javax.servlet.jsp.JspException;
          ?10 import ?javax.servlet.jsp.JspWriter;
          ?11 import ?javax.servlet.jsp.PageContext;
          ?12
          ?13 import ?org.apache.log4j.Logger;
          ?14
          ?15 /**
          ?16 ?*?<p>
          ?17 ?*?Title:
          ?18 ?*?</p>
          ?19 ?*?<p>
          ?20 ?*?Description:實現前臺分頁
          ?21 ?*?</p>
          ?22 ?*?<p>
          ?23 ?*?Copyright:?Copyright?(c)?2006
          ?24 ?*?</p>
          ?25 ?*?<p>
          ?26 ?*?Company:?stt
          ?27 ?*?</p>
          ?28 ?*?
          ?29 ?*? @author ?william
          ?30 ?*? @version ?1.0
          ?31 ? */

          ?32 public ? class ?FrontSplitPageUtil? {
          ?33
          ?34 ???? private ? static ?Logger?log? = ?Logger.getLogger(FrontSplitPageUtil. class );
          ?35
          ?36 ???? private ? static ?String?PAGING_URL? = ? " PAGING_URL " ;
          ?37
          ?38 ???? public ? final ? static ? int ?getCurrentPage(PageContext?pageContext,
          ?39 ????????????String?collectionName,? int ?maxResults)? throws ?IOException,
          ?40 ????????????JspException? {
          ?41 ????????HttpServletRequest?request? = ?(HttpServletRequest)?pageContext
          ?42 ????????????????.getRequest();
          ?43 ????????Collection?collList? = ? new ?ArrayList();
          ?44 ???????? if ?(request.getAttribute(collectionName)? != ? null )? {
          ?45 ???????????? // ?get?collection?data?by?request
          ?46 ????????????collList? = ?(Collection)?request.getAttribute(collectionName);
          ?47 ????????}
          ? else ? if ?(request.getSession().getAttribute(collectionName)? != ? null )? {
          ?48 ???????????? // ?get?collection?data?by?request
          ?49 ????????????collList? = ?(Collection)?request.getSession().getAttribute(
          ?50 ????????????????????collectionName);
          ?51 ????????}

          ?52
          ?53 ????????request.getSession().setAttribute(collectionName,?collList);
          ?54
          ?55 ???????? int ?currentPage? = ? 1 ;
          ?56
          ?57 ???????? int ?count? = ?collList.size();
          ?58 ???????? int ?pageCount? = ?(count? % ?maxResults? == ? 0 )? ? ?count? / ?maxResults?:?(count
          ?59 ???????????????? / ?maxResults? + ? 1 );
          ?60 ???????? if (pageCount == 0 ) {
          ?61 ????????????pageCount = 1 ;
          ?62 ????????}

          ?63 ???????? if ?(request.getParameter( " currentPage " )? != ? null )? {
          ?64 ????????????currentPage? = ?Integer.valueOf(request.getParameter( " currentPage " ))
          ?65 ????????????????????.intValue();
          ?66 ???????????? if ?(currentPage? > ?pageCount)? {
          ?67 ????????????????currentPage? = ?pageCount? == ? 0 ? ? ? 1 ?:?pageCount;
          ?68 ????????????}

          ?69 ????????}

          ?70
          ?71 ???????? return ?currentPage;
          ?72
          ?73 ????}
          ? // ?end?getCurrentPage
          ?74
          ?75 ???? public ? final ? static ? int ?getCurrentOffset(PageContext?pageContext,
          ?76 ????????????String?collectionName,? int ?maxResults)? throws ?IOException,
          ?77 ????????????JspException? {
          ?78
          ?79 ???????? int ?currentPage? = ?getCurrentPage(pageContext,?collectionName,
          ?80 ????????????????maxResults);
          ?81 ???????? return ?(currentPage? - ? 1 )? * ?maxResults;
          ?82
          ?83 ????}
          ? // ?end?getCurrentOffset
          ?84
          ?85 ???? public ? final ? static ? void ?showPage(PageContext?pageContext,
          ?86 ????????????String?collectionName,? int ?maxResults)? throws ?IOException,
          ?87 ????????????JspException? {
          ?88
          ?89 ????????HttpServletRequest?request? = ?(HttpServletRequest)?pageContext
          ?90 ????????????????.getRequest();
          ?91
          ?92 ???????? int ?currentPage? = ?getCurrentPage(pageContext,?collectionName,
          ?93 ????????????????maxResults);
          ?94 ????????Collection?collList? = ? new ?ArrayList();
          ?95 ???????? if ?(request.getAttribute(collectionName)? != ? null )? {
          ?96 ???????????? // ?get?collection?data?by?request
          ?97 ????????????collList? = ?(Collection)?request.getAttribute(collectionName);
          ?98 ????????}
          ? else ? if ?(request.getSession().getAttribute(collectionName)? != ? null )? {
          ?99 ???????????? // ?get?collection?data?by?request
          100 ????????????collList? = ?(Collection)?request.getSession().getAttribute(
          101 ????????????????????collectionName);
          102 ????????}

          103 ???????? int ?count? = ?collList.size();
          104 ????????JspWriter?out? = ?pageContext.getOut();
          105 ???????? // ?set?url
          106 ????????String[]?urlArray? = ?getHttpUrls(request,?count,?currentPage,?maxResults);
          107
          108 ???????? // ?generate?html?string
          109 ????????String?sbTR? = ?getOutputString(pageContext,?count,?currentPage,
          110 ????????????????maxResults,?urlArray);
          111 ????????out.println(sbTR);? // ?output?html
          112
          113 ????}
          ? // ?end?showPage
          114
          115 ???? private ? static ?String[]?getHttpUrls(HttpServletRequest?request,? int ?count,
          116 ???????????? int ?currentPage,? int ?maxResults)? {
          117
          118 ???????? int ?pageCount? = ?(count? % ?maxResults? == ? 0 )? ? ?count? / ?maxResults?:?(count
          119 ???????????????? / ?maxResults? + ? 1 );
          120 ???????? if ?(pageCount? == ? 0 )? {
          121 ????????????pageCount? = ? 1 ;
          122 ????????}

          123 ????????String?urlString? = ?request.getContextPath()
          124 ???????????????? + ?(String)?request.getSession().getAttribute(PAGING_URL);
          125
          126 ????????Enumeration?enumParamNames? = ?request.getParameterNames();
          127
          128 ???????? // ?build?url,add?firstRequest?and?maxResults?parameters?to?url.
          129 ????????StringBuffer?firstPageURL? = ? new ?StringBuffer(urlString);
          130 ????????StringBuffer?previousPageURL? = ? new ?StringBuffer(urlString);
          131 ????????StringBuffer?nextPageURL? = ? new ?StringBuffer(urlString);
          132 ????????StringBuffer?lastPageURL? = ? new ?StringBuffer(urlString);
          133 ????????StringBuffer?jumpToPageURL? = ? new ?StringBuffer(urlString);
          134
          135 ????????firstPageURL.append( " ?currentPage=1 " );
          136
          137 ????????previousPageURL.append( " ?currentPage= " ).append(
          138 ????????????????(currentPage? - ? 1 )? <= ? 0 ? ? ? 1 ?:?(currentPage? - ? 1 ));
          139
          140 ????????nextPageURL.append( " ?currentPage= " ).append(
          141 ????????????????(currentPage? + ? 1 )? >= ?pageCount? ? ?pageCount?:?(currentPage? + ? 1 ));
          142
          143 ????????lastPageURL.append( " ?currentPage= " ).append(pageCount);
          144
          145 ????????jumpToPageURL.append( " ?currentPage={currentPageParameter} " );
          146
          147 ???????? // ?append?old?parameters?to?url
          148 ???????? while ?(enumParamNames.hasMoreElements())? {
          149 ????????????String?paramName? = ?(String)?enumParamNames.nextElement();
          150 ???????????? if ?( ! paramName.equals( " currentPage " )
          151 ???????????????????? && ? ! paramName.equals( " maxResults " ))? {
          152 ????????????????String?paramValue? = ?(String)?request.getParameter(paramName);
          153 ????????????????previousPageURL.append( " & " ).append(paramName).append( " = " )
          154 ????????????????????????.append(paramValue);
          155 ????????????????nextPageURL.append( " & " ).append(paramName).append( " = " ).append(
          156 ????????????????????????paramValue);
          157 ????????????????firstPageURL.append( " & " ).append(paramName).append( " = " ).append(
          158 ????????????????????????paramValue);
          159 ????????????????lastPageURL.append( " & " ).append(paramName).append( " = " ).append(
          160 ????????????????????????paramValue);
          161 ????????????????jumpToPageURL.append( " & " ).append(paramName).append( " = " ).append(
          162 ????????????????????????paramValue);
          163 ????????????}

          164 ????????}

          165
          166 ????????String[]?urlArray? = ? {?firstPageURL.toString(),
          167 ????????????????previousPageURL.toString(),?nextPageURL.toString(),
          168 ????????????????lastPageURL.toString(),?jumpToPageURL.toString()?}
          ;
          169
          170 ???????? return ?urlArray;
          171 ????}
          ? // ?end?getHttpUrls
          172
          173 ???? private ? static ?String?getOutputString(PageContext?pageContext,? int ?count,
          174 ???????????? int ?currentPage,? int ?maxResults,?String[]?urlArray)
          175 ???????????? throws ?JspException? {
          176
          177 ???????? int ?pageCount? = ?(count? % ?maxResults? == ? 0 )? ? ?count? / ?maxResults?:?(count
          178 ???????????????? / ?maxResults? + ? 1 );
          179 ???????? if ?(pageCount? == ? 0 )? {
          180 ????????????pageCount? = ? 1 ;
          181 ????????}

          182 ???????? // ?generate?html?string
          183 ????????StringBuffer?sbTR? = ? new ?StringBuffer();
          184
          185 ????????String?firstPageURL? = ?urlArray[ 0 ];
          186 ????????String?previousPageURL? = ?urlArray[ 1 ];
          187 ????????String?nextPageURL? = ?urlArray[ 2 ];
          188 ????????String?lastPageURL? = ?urlArray[ 3 ];
          189 ????????String?jumpToPageURL? = ?urlArray[ 4 ];
          190
          191 ???????? // ?String[]?firstArg?=?{?firstPageURL?};
          192 ???????? // ?String[]?preArg?=?{?previousPageURL?};
          193 ???????? // ?String[]?nextArg?=?{?nextPageURL?};
          194 ???????? // ?String[]?lastArg?=?{?lastPageURL?};
          195
          196 ???????? // ?parameter?for?paging?string
          197
          198 ???????? // ?string?buffer?append
          199 ????????sbTR.append( " <td> " );
          200 ????????sbTR.append( " [<a?href=\ "" ?+?firstPageURL?+? " \ " ?>首頁</a>] " );
          201 ????????sbTR.append( " [<a?href=\ "" ?+?previousPageURL?+? " \ " ?>上一頁</a>] " );
          202 ????????sbTR.append( " [<a?href=\ "" ?+?nextPageURL?+? " \ " ?>下一頁</a>] " );
          203 ????????sbTR.append( " [<a?href=\ "" ?+?lastPageURL?+? " \ " ?>末頁</a>] " );
          204 ????????sbTR.append( " </td> " );
          205 ????????sbTR.append( " <td> " );
          206 ????????sbTR.append( " 當前第? " ? + ?currentPage? + ? " 頁?/?共 " ? + ?pageCount? + ? " 頁? " );
          207 ????????sbTR.append( " </td> " );
          208 ????????sbTR.append( " <td> " );
          209 ????????sbTR
          210 ????????????????.append( " 轉到<input?id=\ " jumpToPage\ " ?name=\ " jumpToPage\ " ?type=\ " text\ " ?class=\ " bd\ " ?size=\ " 4 \ " ?/>頁 " );
          211 ????????sbTR
          212 ????????????????.append( " <input?name=\ " ?GO?\ " ?type=\ " button\ " ?class=\ " bk\ " ?value=\ " ?GO?\ " ?onclick=\ " GoToURL2( ' "
          213 ???????????????????????? + ?jumpToPageURL
          214 ???????????????????????? + ? " ',document.all.jumpToPage.value)\ " /> " );
          215 ????????sbTR.append( " </td> " );
          216
          217 ???????? // ?return
          218 ???????? return ?sbTR.toString();
          219 ????}
          ? // ?end?getOutputString
          220
          221 ???? public ? final ? static ? void ?showPage(PageContext?pageContext,
          222 ????????????String?collectionName,? int ?maxResults,?String?target)
          223 ???????????? throws ?IOException,?JspException? {
          224
          225 ????????HttpServletRequest?request? = ?(HttpServletRequest)?pageContext
          226 ????????????????.getRequest();
          227
          228 ???????? int ?currentPage? = ?getCurrentPage(pageContext,?collectionName,
          229 ????????????????maxResults);
          230 ????????Collection?collList? = ? new ?ArrayList();
          231 ???????? if ?(request.getAttribute(collectionName)? != ? null )? {
          232 ???????????? // ?get?collection?data?by?request
          233 ????????????collList? = ?(Collection)?request.getAttribute(collectionName);
          234 ????????}
          ? else ? if ?(request.getSession().getAttribute(collectionName)? != ? null )? {
          235 ???????????? // ?get?collection?data?by?request
          236 ????????????collList? = ?(Collection)?request.getSession().getAttribute(
          237 ????????????????????collectionName);
          238 ????????}

          239 ???????? int ?count? = ?collList.size();
          240 ????????JspWriter?out? = ?pageContext.getOut();
          241 ???????? // ?set?url
          242 ????????String[]?urlArray? = ?getHttpUrls(request,?count,?currentPage,?maxResults);
          243
          244 ???????? // ?generate?html?string
          245 ????????String?sbTR? = ?getOutputString(pageContext,?count,?currentPage,
          246 ????????????????maxResults,?urlArray,?target);
          247 ????????out.println(sbTR);? // ?output?html
          248
          249 ????}
          ? // ?end?showPage
          250
          251 ???? private ? static ?String?getOutputString(PageContext?pageContext,? int ?count,
          252 ???????????? int ?currentPage,? int ?maxResults,?String[]?urlArray,?String?target)
          253 ???????????? throws ?JspException? {
          254
          255 ???????? int ?pageCount? = ?(count? % ?maxResults? == ? 0 )? ? ?count? / ?maxResults?:?(count
          256 ???????????????? / ?maxResults? + ? 1 );
          257 ???????? if ?(pageCount? == ? 0 )? {
          258 ????????????pageCount? = ? 1 ;
          259 ????????}

          260 ???????? // ?generate?html?string
          261 ????????StringBuffer?sbTR? = ? new ?StringBuffer();
          262
          263 ????????String?firstPageURL? = ?urlArray[ 0 ];
          264 ????????String?previousPageURL? = ?urlArray[ 1 ];
          265 ????????String?nextPageURL? = ?urlArray[ 2 ];
          266 ????????String?lastPageURL? = ?urlArray[ 3 ];
          267 ????????String?jumpToPageURL? = ?urlArray[ 4 ];
          268
          269 ???????? // ?parameter?for?paging?string
          270
          271 ???????? // ?string?buffer?append
          272 ????????sbTR.append( " <td> " );
          273 ????????sbTR.append( " [<a?href=\ "" ?+?firstPageURL?+? " \ " ?target=\ "" ?+?target
          274 ???????????????? + ? " \ " > 首頁 </ a > ] " );
          275 ????????sbTR.append( " [<a?href=\ "" ?+?previousPageURL?+? " \ " ?target=\ "" ?+?target
          276 ???????????????? + ? " \ " > 上一頁 </ a > ] " );
          277 ????????sbTR.append( " [<a?href=\ "" ?+?nextPageURL?+? " \ " ?target=\ "" ?+?target
          278 ???????????????? + ? " \ " > 下一頁 </ a > ] " );
          279 ????????sbTR.append( " [<a?href=\ "" ?+?lastPageURL?+? " \ " ?target=\ "" ?+?target
          280 ???????????????? + ? " \ " > 末頁 </ a > ] " );
          281 ????????sbTR.append( " </td> " );
          282 ????????sbTR.append( " <td> " );
          283 ????????sbTR.append( " 當前第? " ? + ?currentPage? + ? " 頁?/?共 " ? + ?pageCount? + ? " 頁? " );
          284 ????????sbTR.append( " </td> " );
          285 ????????sbTR.append( " <td> " );
          286 ????????sbTR
          287 ????????????????.append( " 轉到<input?id=\ " jumpToPage\ " ?name=\ " jumpToPage\ " ?type=\ " text\ " ?class=\ " bd\ " ?size=\ " 4 \ " ?/>頁 " );
          288 ????????sbTR.append( " ?[<a?href=\ " #\ " ?onclick=\ " javascript:GoToURL( this , ' "
          289 ???????????????? + ?jumpToPageURL? + ? " ',document.all.jumpToPage.value,' " ? + ?target
          290 ???????????????? + ? " ')\ " ?target = \ "" ? + ?target? + ? " \ " > ?GO? </ a > ] " );
          291 ????????sbTR.append( " </td> " );
          292
          293 ???????? // ?return
          294 ???????? return ?sbTR.toString();
          295 ????}
          ? // ?end?getOutputString
          296 }
          ? // ?end?FrontSplitPageUtil
          297
          posted on 2007-03-16 15:25 張金鵬 閱讀(503) 評論(0)  編輯  收藏 所屬分類: core java中的一些數據結構的處理
          主站蜘蛛池模板: 通州市| 卓资县| 沂水县| 永兴县| 澜沧| 嘉荫县| 安徽省| 宿松县| 宝应县| 搜索| 临城县| 罗平县| 乌什县| 辉县市| 南宫市| 石河子市| 江华| 南乐县| 福贡县| 长汀县| 靖远县| 遂宁市| 延庆县| 仙居县| 监利县| 拉孜县| 维西| 青龙| 城市| 巴马| 通辽市| 大丰市| 巴彦淖尔市| 南安市| 东至县| 象山县| 行唐县| 离岛区| 红安县| 缙云县| 海晏县|