巴巴运动网学习笔记(26-30)

1.准备产品分类后台的管理页面(包括管理首页,产品类别列表显示页面,产品类别添加界面)

2.实现web层的分页功能(分页数据,开始页码和结束页码的计算,每页显示记录条数,总记录数,总页数)

3.将web层分页封装成通用模块(建立一个pagingBean对象来封装分页用到的数据,2中已列出)

a.action:

View Code
 1 public ActionForward execute(ActionMapping mapping, ActionForm form,
2 HttpServletRequest request, HttpServletResponse response)throws Exception {
3
4 ProductTypeForm productTypeForm = (ProductTypeForm)form;
5 int currentPage = productTypeForm.getCurrentPage();
6 int begin = (currentPage-1)*10;
7 LinkedHashMap<String, String> orderHashMap = new LinkedHashMap<String, String>();
8 orderHashMap.put("id", "asc");
9 ScrollResult<ProductType> scrollResult = productTypeService.getScrollResult(ProductType.class,begin,10,orderHashMap,"p.visible=?1",new Object[]{true});
10 PagingBean<ProductType> pagingBean = new PagingBean<ProductType>(currentPage,scrollResult,10);
11 request.setAttribute("pagingBean", pagingBean);
12 return mapping.findForward("list");
13 }

b.pagingBean:

View Code
 1 public class PagingBean<T> {
2 //总记录数
3 private long totalRecords;
4 //总页数
5 private int totalPages;
6 //当前页
7 private int currentPage;
8 //开始页码
9 private int beginPageIndex;
10 //结束页码
11 private int endPageIndex;
12 //数据集
13 private ScrollResult<T> scrollResult;
14 //每页显示记录数
15 private int pageCounts = 10;
16
17
18 public PagingBean(int currentPage, ScrollResult<T> scrollResult,int pageCounts) {
19 this.currentPage = currentPage;
20 this.scrollResult = scrollResult;
21 this.pageCounts = pageCounts;
22 this.setTotalRecords(scrollResult.getCount());
23 this.setTotalPages((int)(scrollResult.getCount()%pageCounts==0?scrollResult.getCount()/pageCounts:scrollResult.getCount()/pageCounts+1));
24 this.setBeginPageIndex((int)(WebTool.getPageIndex(pageCounts, currentPage, totalPages).getStartindex()));
25 this.setEndPageIndex((int)(WebTool.getPageIndex(pageCounts, currentPage, totalPages).getEndindex()));
26 }
27 public int getPageCounts() {
28 return pageCounts;
29 }
30 public void setPageCounts(int pageCounts) {
31 this.pageCounts = pageCounts;
32 }
33 public long getTotalRecords() {
34 return totalRecords;
35 }
36 public void setTotalRecords(long totalRecords) {
37 this.totalRecords = totalRecords;
38 }
39 public int getTotalPages() {
40 return totalPages;
41 }
42 public void setTotalPages(int totalPages) {
43 this.totalPages = totalPages;
44 }
45 public int getCurrentPage() {
46 return currentPage;
47 }
48 public void setCurrentPage(int currentPage) {
49 this.currentPage = currentPage;
50 }
51 public int getBeginPageIndex() {
52 return beginPageIndex;
53 }
54 public void setBeginPageIndex(int beginPageIndex) {
55 this.beginPageIndex = beginPageIndex;
56 }
57 public int getEndPageIndex() {
58 return endPageIndex;
59 }
60 public void setEndPageIndex(int endPageIndex) {
61 this.endPageIndex = endPageIndex;
62 }
63 public ScrollResult<T> getScrollResult() {
64 return scrollResult;
65 }
66 public void setScrollResult(ScrollResult<T> scrollResult) {
67 this.scrollResult = scrollResult;
68 }
69 }

3.webTool和pageIndex:

View Code
 1 public class WebTool {
2 public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){
3 long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2);
4 long endpage = currentPage+viewpagecount/2;
5 if(startpage<1){
6 startpage = 1;
7 if(totalpage>=viewpagecount) endpage = viewpagecount;
8 else endpage = totalpage;
9 }
10 if(endpage>totalpage){
11 endpage = totalpage;
12 if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;
13 else startpage = 1;
14 }
15 return new PageIndex(startpage, endpage);
16 }
17
18 }
View Code
 1 public class PageIndex {
2 private long startindex;
3 private long endindex;
4
5 public PageIndex(long startindex, long endindex) {
6 this.startindex = startindex;
7 this.endindex = endindex;
8 }
9 public long getStartindex() {
10 return startindex;
11 }
12 public void setStartindex(long startindex) {
13 this.startindex = startindex;
14 }
15 public long getEndindex() {
16 return endindex;
17 }
18 public void setEndindex(long endindex) {
19 this.endindex = endindex;
20 }
21 }

4.将分页功能从jsp页面中独立出来(主要实现href的独立)

a.topage()

View Code
1 <script language="JavaScript">
2 function topage(page){
3 var form = document.forms[0];
4 form.currentPage.value = page;
5 form.submit();
6 }
7 </script>

b.form

View Code
1 <html:form action="/control/product/type/list.do" method="post">
2 </html:form>

c.paging页面

View Code
 1 <%@ page contentType="text/html;charset=UTF-8" %>
2 <%@ include file="/WEB-INF/page/share/taglib.jsp" %>
3 <c:forEach begin="${pagingBean.beginPageIndex}" end="${pagingBean.endPageIndex}" var="index">
4 <c:if test="${pagingBean.currentPage==index}">
5 <span style=color:#FFFFFF>第${index}页</span>
6 </c:if>
7 <c:if test="${pagingBean.currentPage!=index}">
8 <a style=color:#FFFFFF href="javascript:topage(${index})">第${index}页</a>
9 </c:if>
10 </c:forEach>

d.include

View Code
1 <%@ include file="/WEB-INF/page/share/paging.jsp" %>

5.初步完成产品类别的添加功能

View Code
 1 public class ProductTypeManagerAction extends DispatchAction {
2 @Resource(name="productTypeServiceImpl")
3 ProductTypeService productTypeService;
4 /**
5 * 提供添加界面
6 * @param mapping
7 * @param form
8 * @param request
9 * @param response
10 * @return
11 * @throws Exception
12 */
13 public ActionForward addUI(ActionMapping mapping, ActionForm form,
14 HttpServletRequest request, HttpServletResponse response)throws Exception {
15 return mapping.findForward("add");
16 }
17 /**
18 * 添加产品类别
19 * @param mapping
20 * @param form
21 * @param request
22 * @param response
23 * @return
24 * @throws Exception
25 */
26 public ActionForward add(ActionMapping mapping, ActionForm form,
27 HttpServletRequest request, HttpServletResponse response)throws Exception {
28 ProductTypeForm productTypeForm = (ProductTypeForm)form;
29 ProductType productType = new ProductType(productTypeForm.getName(),productTypeForm.getNote());
30 if((Integer)productTypeForm.getParentId()!=null&&productTypeForm.getParentId()>0)productType.setParent(new ProductType(productTypeForm.getParentId()));
31 productTypeService.save(productType);
32 request.setAttribute("message", "添加成功!");
33 return mapping.findForward("message");
34 }
35 }

 

你可能感兴趣的:(学习笔记)