struts2.1.2+spring2+hibernate3 web实例(第二章)

来源:互联网 发布:大数据 风控 编辑:程序博客网 时间:2024/05/17 22:43

二、       建立公共类

1AbstractAction

Struts2Struts1.x的差别,最明显的就是Struts2是一个pull-MVC架构。Struts1.x 必须继承org.apache.struts.action.Action或者其子类,表单数据封装在FormBean中。Struts 2无须继承任何类型或实现任何接口,表单数据包含在Action中,通过GetterSetter获取。

虽然,在理论上Struts2Action无须实现任何接口或者是继承任何的类,但是,在实际编程过程中,为了更加方便的实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并且重载(Override

package com.sterning.commons;

public class PagerService {
    
public Pager getPager(String currentPage,String pagerMethod,int totalRows) {
        
//    定义pager对象,用于传到页面
        Pager pager = new Pager(totalRows);
        
//    如果当前页号为空,表示为首次查询该页
        
//    如果不为空,则刷新pager对象,输入当前页号等信息
        if (currentPage != null{
            pager.refresh(Integer.parseInt(currentPage));
        }

        
//    获取当前执行的方法,首页,前一页,后一页,尾页。
        if (pagerMethod != null{
            
if (pagerMethod.equals("first")) {
                pager.first();
            }
 else if (pagerMethod.equals("previous")) {
                pager.previous();
            }
 else if (pagerMethod.equals("next")) {
                pager.next();
            }
 else if (pagerMethod.equals("last")) {
                pager.last();
            }

        }

        
return pager;
    }

}

 

package com.sterning.commons;

import com.opensymphony.xwork2.ActionSupport;

public class AbstractAction extends ActionSupport {
}

 

com.sterning.commons.AbstractAction.java

参考JavaDoc,可知ActionSupport类实现了接口:

com.opensymphony.xwork2.Action

com.opensymphony.xwork2.LoaleProvider

com.opensymphony.xwork2.TextProvider

com.opensymphony.xwork2.Validateable

com.opensymphony.xwork2.ValidationAware

com.uwyn.rife.continuations.ContinuableObject

java.io.Searializable

java.lang.Cloneable

2Pager分页类

为了增加程序的分页功能,特意建立共用的分页类。

 

package com.sterning.commons;

import java.math.*;

public class Pager {
    
private int totalRows; //总行数
    private int pageSize = 5//每页显示的行数
    private int currentPage; //当前页号
    private int totalPages; //总页数
    private int startRow; //当前页在数据库中的起始行
    
    
public Pager() {
    }

    
    
public Pager(int _totalRows) {
        totalRows 
= _totalRows;
        totalPages
=totalRows/pageSize;
        
int mod=totalRows%pageSize;
        
if(mod>0){
            totalPages
++;
        }

        currentPage 
= 1;
        startRow 
= 0;
    }

    
    
public int getStartRow() {
        
return startRow;
    }

    
public int getTotalPages() {
        
return totalPages;
    }

    
public int getCurrentPage() {
        
return currentPage;
    }

    
public int getPageSize() {
        
return pageSize;
    }

    
public void setTotalRows(int totalRows) {
        
this.totalRows = totalRows;
    }

    
public void setStartRow(int startRow) {
        
this.startRow = startRow;
    }

    
public void setTotalPages(int totalPages) {
        
this.totalPages = totalPages;
    }

    
public void setCurrentPage(int currentPage) {
        
this.currentPage = currentPage;
    }

    
public void setPageSize(int pageSize) {
        
this.pageSize = pageSize;
    }

    
public int getTotalRows() {
        
return totalRows;
    }

    
public void first() {
        currentPage 
= 1;
        startRow 
= 0;
    }

    
public void previous() {
        
if (currentPage == 1{
            
return;
        }

        currentPage
--;
        startRow 
= (currentPage - 1* pageSize;
    }

    
public void next() {
        
if (currentPage < totalPages) {
            currentPage
++;
        }

        startRow 
= (currentPage - 1* pageSize;
    }

    
public void last() {
        currentPage 
= totalPages;
        startRow 
= (currentPage - 1* pageSize;
    }

    
public void refresh(int _currentPage) {
        currentPage 
= _currentPage;
        
if (currentPage > totalPages) {
            last();
        }

    }

}

 

com.sterning.commons.Pager.java
同时,采用PagerService类来发布成为分页类服务PagerService,代码如下:


 

原创粉丝点击