CSDN博客

img yq76034150

struts分页的一种实现!(二)

发表于2004/4/20 12:06:00  1364人阅读

      查询Action的代码片断

 public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response)
  throws Exception {
  Base queryForm= (Base) form;
   
     if(!queryForm.getName().equals("")){
   PageController pc=new PageController();       
         EmployeeBase service=new EmployeeBase();      
         ArrayList result=(ArrayList)service.search(queryForm,pc);
       
            HttpSession session=request.getSession();
           
   session.setAttribute("queryForm",queryForm);
         session.setAttribute("pageController",service.getPageController());
       
         request.setAttribute("queryResult",result); 
   request.setAttribute("pageController",service.getPageController());     
      return mapping.findForward("haveResult");
     }else{
        return mapping.findForward("noResult");
     }
  
  
  
 
 }

(3),翻页Action的代码片断

public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response)
  throws Exception {
 
   
        file://读取翻页参数
     
  TurnPageForm turnPageForm=(TurnPageForm)form;
  
  file://从PageController中取出查询信息,并使用bean提供的调用接口处理结果
  
  HttpSession session=request.getSession();
  PageController pc=(PageController)session.getAttribute("pageController");  
  Base queryForm=(Base)session.getAttribute("queryForm");
  
 
  pc.setCurrentPage(turnPageForm.getViewPage());
  
  EmployeeBase service=new EmployeeBase();
  
  ArrayList result=(ArrayList)service.search(queryForm,pc);
  
  file://根据参数将数据写入 request
  
        request.removeAttribute("queryResult");
  request.removeAttribute("pageController");
  request.setAttribute("queryResult",result); 
  request.setAttribute("pageController",pc);
    
  file://forward 到显示页面
  
  
  
  return mapping.findForward("haveResult");
  
  
  
  
 
 }

(4)数据库访问bean中的片断

public Collection search(Base base, PageController pc)
  throws SQLException {
  ArrayList emps = new ArrayList();
  ResultSet rs = getSearchResult(base);

  rs.absolute(-1);
  pc.setTotalRowsAmount(rs.getRow());
  setPageController(pc);
  if (rs.getRow() > 0) {

   rs.absolute(pc.getPageStartRow());


   do {
    System.out.println("in loop" + rs.getRow());

    Base b = new Base();
    b.setName(rs.getString("Name"));
    b.setIdCard(rs.getString("IDCard"));
    System.out.println("From db:" + rs.getString("IDCard"));
    emps.add(b);
    if (!rs.next()) {
     break;
    }
   } while (rs.getRow() < (pc.getPageEndRow() + 1));
  }
  return emps;
 }


(5)在jsp中,翻页部分的代码片断

<bean:write name="pageController" property="description"/>
  
  <logic:equal name="pageController" property="hasPrevious" value="true">
              <a href="turnPage.do?viewPage=<bean:write name="pageController" property="previousPage"/>" class="a02">
           Previous
        </a>
  </logic:equal>

  <logic:equal name="pageController" property="hasNext" value="true">
     <a href="turnPage.do?viewPage=<bean:write name="pageController" property="nextPage"/>" class="a02">
     Next
     </a>
  </logic:equal>

 

这样一来,翻页的功能可以以你喜欢的方式表现给client

 

0 0

相关博文

我的热门文章

img
取 消
img