由于篇幅较长,所以分开两篇来写。下面是我的一个实际例子,从数据库中分页获取管理员的数据,然后在JSP页面上表示出来。
我的Action的代码:
1import java.util.List;
2import javax.servlet.http.*;
3
4import org.apache.struts.action.*;
5
6import xxx.Administrator;
7import xxx.TurnPageForm;
8import xxx.PageData;
9
10/**//**
11 * <p>Title: </p>
12 *
13 * <p>Description: </p>
14 *
15 * <p>Copyright: Copyright (c) 2003</p>
16 *
17 * <p>Company: </p>
18 *
19 * @author George Hill
20 * @version 1.0
21 */
22
23public class AdminListAction extends Action {
24
25 private static final int NUMBER = 15;
26
27 /**//**
28 * 执行管理员列表操作
29 * @param mapping ActionMapping
30 * @param form ActionForm
31 * @param request HttpServletRequest
32 * @param response HttpServletResponse
33 * @throws Exception
34 * @return ActionForward
35 */
36 public ActionForward execute(ActionMapping mapping, ActionForm form,
37 HttpServletRequest request,
38 HttpServletResponse response) throws Exception {
39 HttpSession session = request.getSession();
40
41 //获取页码
42 String pageStr = request.getParameter("page");
43 if (pageStr == null)
44 pageStr = String.valueOf(session.getAttribute("page"));
45 else
46 session.setAttribute("page", pageStr);
47 int page = 1;
48 try {
49 page = Integer.parseInt(pageStr);
50 } catch (NumberFormatException nfe) {
51 }
52
53 //获得总记录数
54 int count = Administrator.countAllAdministrators();
55 int maxPage = count / NUMBER;
56 if (count % NUMBER != 0)
57 maxPage++;
58
59 //获得列表
60 List list = Administrator.getAdministrators(NUMBER, (page - 1) * NUMBER);
61
62 if (count != 0 && list == null)
63 list = Administrator.getAdministrators(NUMBER, 0);
64
65 if (list != null) {
66 PageData data = new PageData(list, page, maxPage);
67
68 request.setAttribute("admins", data);
69 }
70
71 //分页部分
72 TurnPageForm tform = new TurnPageForm();
73 tform.setCurrentPage(page);
74 tform.setMaxPage(maxPage);
75 request.setAttribute("turnPageForm", tform);
76
77 return mapping.findForward("list");
78 }
79}
80 JSP页面部分的代码片断:
1 <logic:present name="admins">
2 <table width="90%" border="0" align="center" cellpadding="0" cellspacing="1" class="table6">
3 <tr align="center" class="table4">
4 <td width="10%" height="18" align="center" nowrap class="head1"><bean:message key="list.index"/></td>
5 <td width="20%" class="head1" align="center" nowrap><bean:message key="admin.account"/></td>
6 <td width="30%" class="head1" align="center" nowrap><bean:message key="admin.name"/></td>
7 <td width="10%" class="head1" align="center" nowrap><bean:message key="admin.status"/></td>
8 <td width="30%" class="head1" align="center" nowrap><bean:message key="list.action"/></td>
9 </tr>
10 <logic:iterate name="admins" property="list" id="entry" indexId="index">
11 <tr class="table5" onmouseover="changeColor(this, '#99CCFF')" onmouseout="changeColor(this, '#F2F3F4')">
12 <td align="center" nowrap><bean:write name="index"/></td>
13 <td align="center" nowrap><bean:write name="entry" property="account"/></td>
14 <td align="center" nowrap><bean:write name="entry" property="name"/></td>
15 <logic:equal name="entry" property="status" value="true">
16 <td align="center" nowrap><bean:message key="status.enable"/></td>
17 <td align="center" nowrap>
18 <html:link page="/disableAdmin.do?status=false" paramId="account" paramName="entry" paramProperty="account" onclick="return MM_popupDisableMsg()">
19 <font color="red"><bean:message key="status.disable"/></font>
20 </html:link>
21 <html:link page="/modifyAdmin.do?action=link" paramId="account" paramName="entry" paramProperty="account">
22 <bean:message key="action.modify"/>
23 </html:link>
24 <html:link action="/deleteAdmin" paramId="account" paramName="entry" paramProperty="account" onclick="return MM_popupDelMsg()">
25 <font color="red"><bean:message key="action.delete"/></font>
26 </html:link>
27 </td>
28 </logic:equal>
29 <logic:equal name="entry" property="status" value="false">
30 <td align="center" nowrap><font color="red"><bean:message key="status.disable"/></font></td>
31 <td align="center" nowrap>
32 <html:link page="/enableAdmin.do?status=true" paramId="account" paramName="entry" paramProperty="account">
33 <bean:message key="status.enable"/>
34 </html:link>
35 <html:link page="/modifyAdmin.do?action=link" paramId="account" paramName="entry" paramProperty="account">
36 <bean:message key="action.modify"/>
37 </html:link>
38 <html:link action="/deleteAdmin" paramId="account" paramName="entry" paramProperty="account" onclick="return MM_popupDelMsg()">
39 <font color="red"><bean:message key="action.delete"/></font>
40 </html:link>
41 </td>
42 </logic:equal>
43 </tr>
44 </logic:iterate>
45 </table>
46 <table width="90%" border="0" align="center" cellpadding="3" cellspacing="0">
47 <tr>
48 <td align="left"></td>
49 <html:form action="/turnPage" method="POST">
50 <td align="right" nowrap>
51 <html:hidden property="url" value="/listAdmin.do"/>
52 <html:hidden property="currentPage"/>
53 <html:hidden property="maxPage"/>
54 <bean:message key="page.the"/>
55 <bean:write name="admins" property="page"/>
56 <bean:message key="page.page"/>/
57 <bean:message key="page.all"/><bean:write name="admins" property="maxPage"/><bean:message key="page.page"/>
58 <bean:message key="page.turn"/>
59 <logic:equal name="admins" property="maxPage" value="1">
60 <html:text property="page" styleClass="input_log" styleId="page" size="3" value="" disabled="true"/>
61 <html:submit property="turn" styleClass="t_input" styleId="turn" value="GO" disabled="true"/>
62 </logic:equal>
63 <logic:notEqual name="admins" property="maxPage" value="1">
64 <html:text property="page" styleClass="input_log" styleId="page" size="3" value=""/>
65 <html:submit property="turn" styleClass="t_input" styleId="turn" value="GO"/>
66 </logic:notEqual>
67 <logic:equal name="admins" property="firstPage" value="true">
68 <html:submit property="first" styleClass="t_input" styleId="first" disabled="true"><bean:message key="page.first"/></html:submit>
69 <html:submit property="preview" styleClass="t_input" styleId="preview" disabled="true"><bean:message key="page.previous"/></html:submit>
70 </logic:equal>
71 <logic:notEqual name="admins" property="firstPage" value="true">
72 <html:submit property="first" styleClass="t_input" styleId="first"><bean:message key="page.first"/></html:submit>
73 <html:submit property="preview" styleClass="t_input" styleId="preview"><bean:message key="page.previous"/></html:submit>
74 </logic:notEqual>
75 <logic:equal name="admins" property="lastPage" value="true">
76 <html:submit property="next" styleClass="t_input" styleId="next" disabled="true"><bean:message key="page.next"/></html:submit>
77 <html:submit property="last" styleClass="t_input" styleId="last" disabled="true"><bean:message key="page.last"/></html:submit>
78 </logic:equal>
79 <logic:notEqual name="admins" property="lastPage" value="true">
80 <html:submit property="next" styleClass="t_input" styleId="next"><bean:message key="page.next"/></html:submit>
81 <html:submit property="last" styleClass="t_input" styleId="last"><bean:message key="page.last"/></html:submit>
82 </logic:notEqual>
83 </td>
84 </html:form>
85 </tr>
86 </table>
87 </logic:present> 实际的页面效果图:
OK,我的整个实现就完成了。我觉得有许多地方还是需要完善的,例如我的实现不够抽象,而且觉得繁琐了点,不过我一直都用着,还没有碰到过什么问题。请各位看完以后多多指教。
posted on 2005-05-13 11:33
小米 阅读(4819)
评论(25) 编辑 收藏 所属分类:
Struts