- Struts 分页显示
- 分页类 Page.java:
- public class Page {
- int currentPage=1;//当前页数
- int totalPages=0;//总页数
- int pageRecorders=3;//每页显示数
- int totalRows=0;//总数据数
- int pageStartRow=0;//每页的起始数
- int pageEndRow;//每页的终止数
- boolean hasNextPage=false;//是否有下一页
- boolean hasPreviousPage=false;//是否有前一页
- List<Country> list;
- public Page(){}
- public Page(List<Country> list)
- {
- //初始化所有参数
this.list=list;
totalRows=list.size();
currentPage=1;
hasPreviousPage=false;
totalPages=totalRows%pageRecorders==0?totalRows%pageRecorders:totalRows%pageRecorders+1;
if(currentPage>=totalPages) {
hasNextPage=false;
}
else {
hasNextPage=true;
}
if(totalRows<pageRecorders) {
this.pageStartRow=0;
this.pageEndRow=totalRows;
}
else {
this.pageStartRow=0;
this.pageEndRow=pageRecorders;
} - }
- public void setList(List<Country> list) {
- this.list = list;
- }
- public int getTotalRows() {
- return totalRows;
- }
- public int getCurrentPage() {
- return currentPage;
- }
- public boolean isHasNextPage() {
- return hasNextPage;
- }
- public boolean isHasPreviousPage() {
- return hasPreviousPage;
- }
- public int getTotalPages() {
- return totalPages;
- }
- public List<Country> getList(int curpage) {
- this.currentPage=curpage;
- if(currentPage*pageRecorders<totalRows) {
- pageEndRow=currentPage*pageRecorders;
- pageStartRow=pageEndRow-pageRecorders;
- }
- else{
- pageEndRow=totalRows;
- pageStartRow=pageRecorders*(totalPages-1);
- }
- return list.subList(pageStartRow,pageEndRow);
- }
- public List<Country> getNextPage() {
- currentPage=currentPage+1;
- if((currentPage-1)>0) {
- hasPreviousPage=true;
- }
- else {
- hasPreviousPage=false;
- }
- if(currentPage>=totalPages) {
- hasNextPage=false;
- }
- else {
- hasNextPage=true;
- }
- List<Country> list=this.getList(currentPage);
- return list;
- }
- public List<Country> getPreviousPage() {
- currentPage=currentPage-1;
- if(currentPage==0) {
- currentPage=1;
- }
- if(currentPage>=totalPages) {
- hasNextPage=false;
- }
- else {
- hasNextPage=true;
- }
- if((currentPage-1)>0) {
- hasPreviousPage=true;
- }
- else {
- hasPreviousPage=false;
- }
- List<Country> list=this.getList(currentPage);
- return list;
- }
- }
- Action:
- public class PageListActionAction extends Action {
- List<Country> list=new ArrayList<Country>();
- Page pd;
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- String action;
- int pageNum=1;
- action=request.getParameter("action");
- if(action==null||action.equals("")) {
- try {
- list=Dbutil.getBindTable();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- pd=new Page(list);
- List<Country> country=pd.getList(pageNum);
- request.getSession().setAttribute("list",country);
- request.getSession().setAttribute("pa",pd);
- }
- else {
- if(action=="nextPage"||action.equals("nextPage")) {
- List<Country> country=pd.getNextPage();
- request.getSession().setAttribute("list",country);
- request.getSession().setAttribute("pa",pd);
- }
- if(action=="previousPage"||action.equals("previousPage")) {
- List<Country> country=pd.getPreviousPage();
- request.getSession().setAttribute("list",country);
- request.getSession().setAttribute("pa",pd);
- }
- }
- return mapping.findForward("success");
- }
- }
- jsp:
- <html>
- <head>
- <title></title>
- </head>
- <body>
- <table border="1">
- <logic:present name="list">
- <logic:iterate id="li" name="list" scope="session">
- <logic:present name="li">
- <tr>
- <td><bean:write name="li" property="countryID"/></td>
- <td><bean:write name="li" property="description"/></td>
- </tr>
- </logic:present>
- </logic:iterate>
- </logic:present>
- </table>
- <logic:equal name="pa" property="hasNextPage" value="true">
<html:link page="/pageListAction.do?action=nextPage">nextPage</html:link>
</logic:equal>
<logic:notEqual name="pa" property="hasPreviousPage" value="true">
PreviousPage
</logic:notEqual>
<logic:equal name="pa" property="hasPreviousPage" value="true">
<html:link page="/pageListAction.do?action=previousPage">PreviousPage</html:link>
</logic:equal>
<logic:notEqual name="pa" property="hasNextPage" value="true">
nextPage
</logic:notEqual> - 共有数据总数<bean:write name="pa" property="totalRows"/>;
- 共分<bean:write name="pa" property="totalPages"/>页,当前是第
- <bean:write name="pa" property="currentPage"/>页
- </body>
- </html>
- 查询类:
- public static List<Country> getBindTable()
- {
- Country country=null;
- List<Country> list=new ArrayList<Country>();
- try {
- Class.forName("org.gjt.mm.mysql.Driver");
- Connection c = DriverManager.getConnection
- ("jdbc:mysql://192.168.0.11/database_name?user=root&password=0");
- Statement s = c.createStatement();
- ResultSet r =s.executeQuery("select ID,Country_ID,Description from
- country_");
- while(r.next()) {
- country=new Country();
- country.setID(r.getInt("ID"));
- country.setCountryID(r.getString("Country_ID"));
- country.setDescription(r.getString("Description"));
- list.add(country);
- //System.out.println(r.getString("Country_ID"));
- }
- s.close(); // Also closes ResultSet
- } catch(Exception e) {e.printStackTrace();}
- return list;
- }