displayTag1.2データベースページング


import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.GenericValidator;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport
{
    /**
     *     
     */
    private static final long serialVersionUID = -8648961157882604306L;
    
    private ArrayList<UserInfo> userList;
    
    private int count;
    
    public String execute()
        throws Exception
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        String pageIndexName =
            new org.displaytag.util.ParamEncoder("element").encodeParameterName(org.displaytag.tags.TableTagParameters.PARAMETER_PAGE); //       
        int pageSize = 10; //       
        int pageIndex =
            GenericValidator.isBlankOrNull(request.getParameter(pageIndexName)) ? 0
                : (Integer.parseInt(request.getParameter(pageIndexName)) - 1); //    
        count = 50;
        userList = getList(pageIndex, pageSize);
        return SUCCESS;
    }
    
    /**
     * <       >
     * <      >
     * @param begin
     * @param pageSize
     * @return
     * @see [ 、 #  、 #  ]
     */
    public ArrayList<UserInfo> getList(int begin, int pageSize)
    {
        ArrayList<UserInfo> list = new ArrayList<UserInfo>();
        UserInfo userInfo = null;
        begin = begin * pageSize;
        pageSize = (begin + 1) * pageSize;
        for (int i = begin; i <= pageSize; i++)
        {
            userInfo = new UserInfo();
            userInfo.setId("id" + i);
            userInfo.setName("name" + i);
            userInfo.setPass("pass" + i);
            userInfo.setHref("href" + i);
            list.add(userInfo);
        }
        return list;
    }
    
    public int getCount()
    {
        return count;
    }
    
    public void setCount(int count)
    {
        this.count = count;
    }
    
    public ArrayList<UserInfo> getUserList()
    {
        return userList;
    }
    
    public void setUserList(ArrayList<UserInfo> userList)
    {
        this.userList = userList;
    }
    
}

 
 
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link rel="stylesheet" type="text/css" href="css/screen.css" />
  <link rel="stylesheet" type="text/css" href="css/maven-theme.css" />
  <link rel="stylesheet" type="text/css" href="css/css.css" />
 </head>
 <body>
  <div align="center">
   <display:table name="userList" cellspacing="0" cellpadding="0" pagesize="10" class="simple" htmlId="tabId" id="element" 
   requestURI="displayTag.action" partialList="true" size="count">
    <display:column property="id" title="  ID" />
    <display:column property="name" title="   "/>
    <display:column property="pass" title="    " />
    <display:column property="href" title="  "/>
   </display:table>
  </div>
 </body>
</html>