淘宝商城(天猫)高级技术专家.3年研发+3年性能测试调优/系统测试+4年团队管理与测试架构、研发系统实践. 新舞台新气象, 深化测试基础架构及研发架构,希望能在某个技术领域成为真正的技术大牛。欢迎荐才http://bbs.51testing.com/viewthread.php?tid=120496&extra=&page=1 .邮件: jianzhao.liangjz@alibaba-inc.com,MSN:liangjianzhao@163.com.微博:http://t.sina.com.cn/1674816524

spring + velocity实现分页程序2

上一篇 / 下一篇  2008-09-01 18:02:58 / 个人分类:java性能监控与调优

接续

package com.ali;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.validation.BindException;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.ali.db.User;
import com.ali.util.PageUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserListController extends SimpleFormController {
 private IIbatisDAO currDAO;
 private String  m_pagesize;
 public UserListController() {
  setCommandClass(UserListForm.class);
 }

 public ModelAndView handleRequestInternal(HttpServletRequest request,
   HttpServletResponse response) throws Exception {
  
  if ( request.getMethod().equals("GET"))
  {
   //处理 http get请求
   System.out.println("do Get!!");
   
   UserListForm form = new UserListForm();
   form.setPage("1");
   form.setPageSize(getPageSize());
   System.out.println("handleRequestInternal");
   return onSubmit(request, response, form, null);
  }
  else
  {
   //return onSubmit(request, response, null, null);
   System.out.println("do Post!!");
   return super.handleRequestInternal(request, response);
  }
  
 }

 protected ModelAndView onSubmit(HttpServletRequest request,
   HttpServletResponse response, Object command, BindException errors)
   throws Exception {
  //处理http post请求
  UserListForm form = (UserListForm) command;
  System.out.println("user list:" + form);

  List list = this.getDAO().findUsers();
  ArrayList arrList = new ArrayList();
  for (Iterator i = list.iterator(); i.hasNext();) {
   User user = (User) i.next();
   System.out.println(user);
   arrList.add(user);
  }

  PageUtil util = new PageUtil(arrList, form.getPageSize(), form
    .getPage());
  System.out.println("list.size=" + arrList.size());
  request.setAttribute("userlist", util.getArrayList());

  request.setAttribute("pager", util);

  return new ModelAndView(this.getSuccessView());
 }

 public void setDAO(IIbatisDAO dao) {
  currDAO = dao;
 }

 public IIbatisDAO getDAO() {
  return currDAO;
 }
 public String getPageSize()
 {
  return m_pagesize;
 }
 
 public void setPageSize(String pagesize)
 {
  m_pagesize = pagesize;
 }
}

具体的velocity内容

 <BODY >
 
    <form name="userlist" method="post" action="/spring/userlist.do">
         <input type="hidden" name="page" >
         <input type="hidden" name="pageSize"  value=2>    
    </form>
 
 <table width="772"  border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
   <tr>
     <td>     
       <table width="750"  border="0" align="center" cellpadding="5" cellspacing="1">
         <tr align="center">
           <td background="images/q_12.gif"><span class="ff">用户名</span></td>
           <td background="images/q_12.gif" class="ff">站点</td>
           <td background="images/q_12.gif"><span class="ff">邮箱</span></td>
         </tr>
 #foreach($user in $userlist)
 #set($color="E9F4FF")
 #if($velocityCount%2==0)
 #set($color="FFFFFF")
 #end
         <tr align="center" bgcolor="$color">
                   <td class="ii">$user.getUsername()</td>         
                         <td>$user.getSite()</td>
             <td class="style28">$user.getEmail()</td>
         
                 </tr>
 #end       
       </table>
 #set($pageFirst=1)
 
 #if ($pager)
 #set($pageEnd=$pager.getPageCount())
 #set($pagePrevious=$pager.getPreviousPage())
 #set($pageNext = $pager.getNextPage())
 #else
 #set($pageEnd=1)
 #set($pagePrevious=1)
 #set($pageNext =1)
 #end
 <scrīpt>
 
 function goto(p) {
         userlist.page.value=p;
         userlist.pageSize.value=2;
          userlist.submit();
 }
 </scrīpt>
 <table width="100%" border="0">
   <tr>
   <td width="98%" height="43" align="right">
    #if ($pager)
    总 $pager.rowCount 条
    第 $pager.currentPage/$pager.pageCount 页
    #else
    总 0 条
    第 0 页
    #end
    <a href="#$pageFirst" ōnclick="goto($pageFirst)">首页</a>
    <a href="#$pagePrevious" ōnclick="goto($pagePrevious)">上一页</a>
    <a href="#$pageNext" ōnclick="goto($pageNext)">下一页</a>
    <a href="#$pageEnd" ōnclick="goto($pageEnd)">末页</a>
   </td>
   </tr>
 </table>
     </td>
   </tr>
 </table>
 
 </BODY>
 


TAG: java性能监控与调优 Spring velocity 分页

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar