struts2.1.2+spring2+hibernate3 web实例(第二章)
来源:互联网 发布:大数据 风控 编辑:程序博客网 时间:2024/05/17 22:43
二、 建立公共类
1、AbstractAction类
Struts2和Struts1.x的差别,最明显的就是Struts2是一个pull-MVC架构。Struts1.x 必须继承org.apache.struts.action.Action或者其子类,表单数据封装在FormBean中。Struts 2无须继承任何类型或实现任何接口,表单数据包含在Action中,通过Getter和Setter获取。
虽然,在理论上Struts2的Action无须实现任何接口或者是继承任何的类,但是,在实际编程过程中,为了更加方便的实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并且重载(Override)
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;
}
}
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
2、Pager分页类
为了增加程序的分页功能,特意建立共用的分页类。
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();
}
}
}
- struts2.1.2+spring2+hibernate3 web实例(第二章)
- struts2.1.2+spring2+hibernate3 web实例(第一章)
- struts2+hibernate3+spring2+myclipse6+mysql实例
- Struts2+spring2+hibernate3实例源码-java源码
- Ajax+Struts2+Spring2+Hibernate3入门实例
- Struts2+Spring2+Hibernate3 web应用示例(一)
- Struts2+Spring2+Hibernate3 web应用示例(二)
- Struts2+Spring2+Hibernate3 web应用示例(三)
- Struts2+Spring2+Hibernate3 web应用示例(四)
- Struts2+Spring2+Hibernate3 web应用示例(五)
- Struts2+Spring2+Hibernate3 web应用示例(六)
- Struts2+Spring2+Hibernate3 web应用示例(七)
- Struts2+Spring2+Hibernate3 web应用示例------源代码
- Struts2+Spring2+Hibernate3 web应用示例(一)
- Struts2+Spring2+Hibernate3 web应用示例(二)
- Struts2+Spring2+Hibernate3 web应用示例(三)
- Struts2+Spring2+Hibernate3 web应用示例(四)
- Struts2+Spring2+Hibernate3 web应用示例(五)
- [Sharepoint]设置任意时间的筛选
- datagrid删除是弹出确认对话框
- Ad-tech Conference
- Web 2.0 Expo and Conference
- 通用文件操作类的设计
- struts2.1.2+spring2+hibernate3 web实例(第二章)
- Schtasks 计划任务参数详解
- 简单的SQL数据备份C#+XML
- 错误是:未能找到回调的目标“__Page”或未实现 ICallbackEventHandler
- Affiliate Program(分销联盟计划)业务研究
- 为什么????抽奖程序的代码
- 使用subclipse遇到的更新问题
- Eclipse实用快捷键大全 -- 解放右手
- 公司技术管理角度看C++游戏程序员发展