struts分页显示.docx
《struts分页显示.docx》由会员分享,可在线阅读,更多相关《struts分页显示.docx(14页珍藏版)》请在冰豆网上搜索。
![struts分页显示.docx](https://file1.bdocx.com/fileroot1/2023-1/26/47f09bcd-29f4-42e1-89d9-cc23402d21c6/47f09bcd-29f4-42e1-89d9-cc23402d21c61.gif)
struts分页显示
学习Struts已经有2个多月了,前几天群里的朋友问我Struts分页显示的问题,觉得好像与在jsp中的差不多,但还是遇到了这样那样的问题,好不容易花了几天时间把问题都搞清楚,觉得还是写点东西跟大家分享一下的好!
至于Struts的语法这里就不多介绍了,不懂的朋友可以先看网上的其他文章。
一开发环境
Elicpse+StrutsStudio+SqlServer2000+Tomcat。
二开发思路
既然讲的是Struts,那自然离不了MVC,分页显示也是如此。
1建立适当的模型组件,对应你要查询数据库中的表。
这部分由我们熟悉的javaBean来充当。
并在其中建立数据库查询方法,该方法需要一个java.sql.Conntection类型的参数,并返回一个ArrayList。
在本例中为Book.java
2建立分页所需要的模型组件,也是由javaBean来充当,通过由Book中提供的ArrayList来构造。
本例中为PageBean.java.。
3建立控制器组件,这部分由Struts中的Action来实现。
主要负责将实例化Book,并利用返回的ArrayList对象,构造PageBean。
以及接收由视图传递而来的action参数。
从而在PageBean对象中调用不同的方法,该方法返回Book[]对象。
最后将Book[]和PageBean放入request中。
本例中为PageListAction.java。
4建立视图组件,这部分由jsp来充当,为了不出现java代码,我们使用Struts提供的标签库,主要负责从request中取出刚刚放入的对象,通过反复调用PageListAction以及action参数,而实现分页显示。
本例中为pagetest.jsp.
5建立并配置struts-config.xml。
6建立数据库。
三实例代码MyPlaces(自我空间自由展现)k(h_K;S+t_G4ky
MyPlaces(自我空间自由展现)_[#H_t#r_{_v_T_l_M_C:
]
1Book.java
packagebean;
1V+s_i_N1E_Y^"@_JGuestimportjava.sql.*;MyPlaces(自我空间自由展现)_YF_b"?
n_x"cO
importjava.util.ArrayList;MyPlaces(自我空间自由展现)5G_W:
e(W2N_S%E4sk
/**MyPlaces(自我空间自由展现))J'R*D(v0|+Z
*@作者李敏强MyPlaces(自我空间自由展现)_N*@y;W_A_e5b_e_Z,S
*Struts分页显示数据Bean,对应数据库中Book表
e
b+{0~_y\Guest */
U6U6_I5~'g0GGuestpublicclassBook{MyPlaces(自我空间自由展现)_iQ_d_S2f
privateStringbookname;//书名MyPlaces(自我空间自由展现)_M9{m/~+g_m'J_T_Zr
privateStringauthor; //作者MyPlaces(自我空间自由展现)-f__"w4V.b_o
privateStringprice; //价格
.}(nI;R+^_\_\_~Guest
U1h2Jh0wGuestpublicBook(Stringname,Stringauthor,Stringprice){MyPlaces(自我空间自由展现)w;B_@q.~x
this.bookname=name;MyPlaces(自我空间自由展现);a9t;Z&U_b,X[_v_c
this.author=author;
3M
x_BmP_HCGuest this.price=price;
*[6c_d_{_r_N0g_^_e.O3MGuest}
#V_H3[_~_t%Y&B_N_fGuestMyPlaces(自我空间自由展现)_X4E;Zd__c{
publicStringgetAuthor(){
_h_N6Y_r_k_G8?
_}&kGuest returnauthor;
p_a_AK)?
3w0cGuest } MyPlaces(自我空间自由展现)_FE_c_T_iJ
MyPlaces(自我空间自由展现)&TF'\$?
.H/f
publicvoidsetAuthor(Stringauthor){MyPlaces(自我空间自由展现)"C_N"~8j8W_T_v_]
this.author=author;MyPlaces(自我空间自由展现)w_Z6j_v_K.w_]1i2B0o-@
}MyPlaces(自我空间自由展现)0j_J$C_Sx_H
7}:
a_}_w!
i_`Guest publicStringgetBookname(){MyPlaces(自我空间自由展现)_a_f_d,Q_R_m7lP
returnbookname;
%I_\$yW8~__Guest }
\!
bi%o%D_X_fGuest
0SL;T#f.h_@_ZGuestpublicvoidsetBookname(Stringbookname){MyPlaces(自我空间自由展现)_d_[a;}!
C6u_N_o
this.bookname=bookname;MyPlaces(自我空间自由展现)!
](J_`_K5W_l(~E__
}MyPlaces(自我空间自由展现)_t_A_@}P_p%o6`_u_{
_lr_n_G6r6v_OGuest publicStringgetPrice(){MyPlaces(自我空间自由展现)_m_c*n_{$^_J!
o
returnthis.price;
!
M9C_Lv/t@Guest }
#G_]Q'S6i_oGuest MyPlaces(自我空间自由展现))S_B_j0\_G_v_I
publicvoidsetPrice(Stringprice){
)u_D_j__~"E_U_w3X;h7tGuest this.price=price;
3@*F_G_n/g8^T_nYGuest }MyPlaces(自我空间自由展现)l_h:
A.y&u+y_E
MyPlaces(自我空间自由展现)PD*]__;?
8o8x
publicstaticArrayListgetAllBook(Connectionconnection){
_s/q_r.o!
]_W!
Q1z8dGuest Stringsql="select*frombook";
_M:
M1d$K#F-H4kGuest ArrayListarrayList=newArrayList();
_S%N't9hR;M9~5|Guest try{
_c_h_J8Kw#?
`_N_aGuest Statementstatement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);MyPlaces(自我空间自由展现)+T"PS4?
-PP_?
_J
ResultSetresultSet=statement.executeQuery(sql); MyPlaces(自我空间自由展现)_|1]2G9`4k_L-aP%G_x?
System.out.println("BookBean数据查询已完成!
");
&n_\*Vt,B_H0MFGuest while(resultSet.next())MyPlaces(自我空间自由展现)_I_Ji;d4N
{
_f_L.hP!
s2`)U_j+__B5~8gGuest Stringname=resultSet.getString("name");
_x_m_Y"a_c"P$mGuest Stringauthor=resultSet.getString("author");MyPlaces(自我空间自由展现)_W_J5K:
mL0W(^-G;P'v
Stringprice=resultSet.getString("price");MyPlaces(自我空间自由展现)_H_?
#n3|'dW_j_m_e
System.out.println("开始数据封装:
name="+name+"author="+author+"price="+price);
*pj_B4u_~Guest Bookbook=newBook(name,author,price); MyPlaces(自我空间自由展现)$i_z_t*n_z_G_Mu
arrayList.add(book);
_b)c_K
|!
S_h-K_HGuest }
_E$E_g'C,y_Y_J_U_o_oGuest connection.close();MyPlaces(自我空间自由展现)l_a~_x_?
_W
resultSet.close();MyPlaces(自我空间自由展现)0K/m__^/@8X
}catch(SQLExceptione)
)A5U$B_|v_]_BGuest {MyPlaces(自我空间自由展现)!
thh#^_[}$i&v
System.out.println("数据库异常"+e.toString());MyPlaces(自我空间自由展现)O?
_tF'R*T_O2F_D1Q+UO
}
returnarrayList;MyPlaces(自我空间自由展现)_y$N/R4c&s/H_K
}
0y.c'i_cL%I-W_F7oGuest}
2PageBean.javaMyPlaces(自我空间自由展现)*Gy_U_H(p,o$J_w_Y
MyPlaces(自我空间自由展现)3i4_.|_f%@w_?
_a
packagepage;
K_~_f+R}&Q
w5xGuestimportbean.Book;
_O/N_N3~fzA9GGuestimportjava.util.*;MyPlaces(自我空间自由展现),v_oJ_Uv_N!
o
/**
5to,E8E$N:
H_A:
gMj_s/eGuest *@作者李敏强
a(z0V5o&J_P3p5dGuest *Struts分页显示逻辑Bean
0`6w3a_^/EGuest */
_B_W#Z)t$s2r/y_r_AGuestpublicclassPageBean{
_J_c_T-[*B_`4c9u_k_UGuestMyPlaces(自我空间自由展现)^9L5l+]_r"?
O
intcurrentPage=1; //当前页
_Y:
E_i_T$x2f)ZGuestpublicinttotalPages=0; //总页数
_gS_z*S_B0]_q!
Z_w2yGuest intpageRecorders=5;//每页5条数据
9FU&wV6k_p#WGuest inttotalRows=0; //总数据数MyPlaces(自我空间自由展现)&@"]5{&|2un_A$P5D%z_n
intpageStartRow=0;//每页的起始数
_JQ_C_^!
{_bQ_C_uGuest intpageEndRow=0; //每页显示数据的终止数
!
D_^O_`_S;e*I-i)xGuest booleanhasNextPage=false;//是否有下一页MyPlaces(自我空间自由展现)_d_q_F7^1k"b.^0V
booleanhasPreviousPage=false;//是否有前一页
_T"W5|'v_?
#R_F*{Guest ArrayListarrayList;MyPlaces(自我空间自由展现)_?
_p$W_h9@P9_q_T)a%ce(E
Iteratorit;MyPlaces(自我空间自由展现)_Z_Y_GX;Q_]$D0@+C
publicPageBean(){}MyPlaces(自我空间自由展现)9Y_d_\_w_l7T_~9u
1~_R^&y&r%D*u4k4\kGuestpublicPageBean(ArrayListarrayList){
__!
Y7o_Y%S3G_?
1X__Guest this.arrayList=arrayList;MyPlaces(自我空间自由展现)'Q_}2o,r-L*h_A
totalRows=arrayList.size();
_I_}(A_O_c9`9q;QGuest it=arrayList.iterator(); MyPlaces(自我空间自由展现)_[_hH_A6x_H
hasPreviousPage=false;
_H'A(O)JV)DGuest currentPage=1;MyPlaces(自我空间自由展现)_h4{i_~_u&v_e
if((totalRows%pageRecorders)==0)
J7r_i2@o:
[)u:
xGuest {
(p_y;Y,f
p4V__Guest totalPages=totalRows/pageRecorders; MyPlaces(自我空间自由展现)_l.O_v!
Di_E$M'r
}
_x_d-X_^_yGuest else
_p"m'p_[_`IGuest {
^_S4{_C_e"z4xGuest totalPages=totalRows/pageRecorders+1;
_h!
~_z_R9G_O_Ff4]_yGuest }MyPlaces(自我空间自由展现)"Y:
u_o_j%{
/u_}_C:
L
wm1G)^_{Guest if(currentPage>=totalPages) MyPlaces(自我空间自由展现)_aC'k.T:
T
{MyPlaces(自我空间自由展现)8a_m_R_S_v
hasNextPage=false;
_X.G_C_U_y_H_dGuest }MyPlaces(自我空间自由展现)_g9U&z_K6}_F)H
else
_t/A_N`_E0ME%IGuest {
sE_N1G/S_JGuest hasNextPage=true;MyPlaces(自我空间自由展现)-X_D(|9Z1J'CiC_L!
f
}
y"n_T"f_X-pGuest if(totalRows {MyPlaces(自我空间自由展现)M_`_KAF
this.pageStartRow=0; MyPlaces(自我空间自由展现)(F@_x5o#Ty_K_y_E
this.pageEndRow=totalRows; MyPlaces(自我空间自由展现)8^_W&~:
y%A_}.h_d7l
}MyPlaces(自我空间自由展现)&b_d_t?
MW#mQ,|_B0w
else
*s,R_B_M_f{(T-D1M_HGuest {
_a_D8Q.j_P_G!
g9y_EGuest this.pageStartRow=0;
:
L+h-M_Uj_g%F_EGuest this.pageEndRow=pageRecorders; MyPlaces(自我空间自由展现)5Qa_s_g___L
}
}
/**MyPlaces(自我空间自由展现)I3[8z)n9zn_Q
*@returnReturnsthecurrentPage.
U{0D_y_^9~3v_MMGuest */MyPlaces(自我空间自由展现)-E9^_M.o-C_|:
lP/j
publicStringgetCurrentPage(){
u_q"e_S2J)E:
G&T7c;B*]7FGuest returnthis.toString(currentPage);
__1t_?
1g_1U-@Guest }
U_o:
M_A3f5QI*B_h_l_}Guest /**
_`4r&h6a_D
V_Y6l_S_rVGuest *@paramcurrentPageThecurrentPagetoset.
!
__B8G-tO.swGuest */
_~"?
_?
_[_`_E#[1MLe@Guest publicvoidsetCurrentPage(intcurrentPage){MyPlaces(自我空间自由展现)5Lb8v_S)r6c_U
this.currentPage=currentPage;
;m%V7X_?
N8YGuest }
Z2o_C9r0H_HGuest /**MyPlaces(自我空间自由展现)_c*j.E_n_w)c_S7B_m_O
*@returnReturnsthepageRecorders.
;c_a!
J$w_E9B_\6D0K9\Guest */MyPlaces(自我空间自由展现)Q_L%Y_`oR1m
publicintgetPageRecorders(){
1Y_x_Z!
b9^7KGuest returnpageRecorders;MyPlaces(自我空间自由展现)*[u_h_G5P_T_u.`"y
}
9|g(t_x!
}_n9h-C2^Guest /**MyPlaces(自我空间自由展现)t$I#B_O_m_w:
g*n{
*@parampageRecordersThepageRecorderstoset.MyPlaces(自我空间自由展现)#p9^_ZR6Q*?
_
*/MyPlaces(自我空间自由展现)_K9m$Zo_P_w
publicvoidsetPageRecorders(intpageRecorders){MyPlaces(自我空间自由展现)N;A7W_f_~_?
this.pageRecorders=pageRecorders;
;S_E:
N_p+V2M/yoGuest }MyPlaces(自我空间自由展现)_d5L!
P_D|_}_q_u
/**
/k$t.y_t_NGuest *@returnReturnsthepageEndRow.
0L*C.m.hg_f8n!
mGuest */
S_~-n_u_KGuest publicintgetPageEndRow(){MyPlaces(自我空间自由展现)9J(f_n_y"A_U_y
returnpageEndRow;
_P5E*p.f7|8S_A_F7L_eGuest }
8v_Z)C_E_N_I"A_B_{Guest /**
!
H%x_d__RZGuest *@returnReturnsthepageStartRow.
_~9K_{uU_WGuest */MyPlaces(自我空间自由展现)/ss_H'P"E0n
publicintgetPageStartRow(){MyPlaces(自我空间自由展现).H_T
m_|/s^
returnpageStartRow;MyPlaces(自我空间自由展现)_h3P!
P_C_Bi0w
}
_~___oE"m*L#Z9fGuest /**MyPlaces(自我空间自由展现).\-AA_X3B_W&x9e&}-{'c
*@returnReturnsthetotalPages.MyPlaces(自我空间自由展现)2D_n_L0rZA_J
*/
_v2},U}8G!
G'e+\Guest publicStringgetTotalPages(){
_o%e_Tl_s_hP$fe_W_CGuest
_S#}e`_?
_OGuest returnthis.toString(totalPages);MyPlaces(自我空间自由展现).Y_z*e_b_@!
U_d
}MyPlaces(自我空间自由展现)_dS_L&p"h_`:
P
/**MyPlaces(自我空间自由展现)_F._RV_{_Os
u7{
*@returnReturnsthetotalRows.
8J2X_}!
L_o_~4GL_hGuest */
_mj'^/B_W5b!
h[Guest publicStringgetTotalRows(){
1m,F0q*|_Jx4g#[Guest returnthis.toString(totalRows);MyPlaces(自我空间自由展现)1t8?
.L!
c&R_];k_u
}MyPlaces(自我空间自由展现)/Xib_k"C%SO_E
/**
_U;q/k.n7y+wGuest *@returnReturnsthehasNextPage.MyPlaces(自我空间自由展现)8OLz#I9h};o4b
*/
P%@c1A_L_]3|"N*VGuest publicbooleanisHasNextPage(){MyPlaces(自我空间自由展现)L!
I_d%J_]_V_G6['s;m
returnhasNextPage;
{9r!
b_N4a_]!
A%e_UGuest }
_n/oWO_y7o&Y_X_{Guest /**
__R*`_E'f)nGuest *@paramhasNextPageThehasNextPagetoset.MyPlaces(自我空间自由展现)_w_U3a/P'P,J_d}
*/
-k3k3P_N_WJ_fY_])f_HGuest publicvoidsetHasNextPage(booleanhasNextPage){MyPlaces(自我空间自由展现)Z_m-t/p_Q+v_Y)b
this.hasNextPage=hasNextPage;MyPlaces(自我空间自由展现)_H4T&K;__Y%d_S_df
}MyPlaces(自我空间自由展现)_h)e3D*g)|_NA+K_l
/**
_Zu_E.a.T_WGuest *@returnReturnsthehasPreviousPage.
;QP$]_x0g!
O*sGuest