1.我们以一个药品管理系统为例,当点击查看所有药品的时候,(项目开发>医药管理系统)
baseData/med.do?command=paging
此时struts配置文件为将此请求发送到指定action中:
public class MedicineAction extends DispatchAction {
//要用dispatchAction因为请求中指定了方法
protected int recPerPage = 3; // 分页中每页的记录数
// 分页查询药品信息
public ActionForward paging(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
// 获取页码
String currPage = request.getParameter("currPage");\\从页面中穿过来的当前页码
// 构建action地址
String action = request.getContextPath() + "/baseData/med.do?command=paging";
// HQL查询语句
String hql = "from Medicine";
// 分页查询,返回Map对象
Map map = this.getPage(hql, recPerPage, currPage, action, null);
//将结果集放到request中
request.setAttribute("list", map.get("list"));
//将结果集放到分页条中
request.setAttribute("pagingBar", map.get("bar"));
return mapping.findForward("findAllSuccess");
}
public Map getPage(String hql, int recPerPage, String currPage,
String action, Object[] where) {
// 实例化一个Map对象
Map map = new HashMap();
// 分页条
StringBuffer pagingBar = new StringBuffer();
List list = null; // 结果集
int iCurrPage = 1; // 当前页码
// 如果传递了页码则对当前页码赋值
if (currPage != null && !currPage.isEmpty()) {
iCurrPage = Integer.parseInt(currPage);
}
// 实例化SupperDao对象
SupperDao dao = new SupperDao();
int pages = 0; // 总页数
// 获取总记录数
Long l = (Long) dao.uniqueResult("select count(*) " + hql, where);
int count = l.intValue(); // 将总记录数转为int型
if (count > 0) {
// 计算总页数
if (count % recPerPage == 0) {
pages = count / recPerPage;
} else {
pages = count / recPerPage + 1;
}
if (iCurrPage > pages) {
iCurrPage = pages;
}
if (iCurrPage < 1) {
iCurrPage = 1;
}
// 分页查询获取结果集
list = dao.findPaging(hql, (iCurrPage - 1) * recPerPage,recPerPage, where);
// 构造分页条
pagingBar.append("<form name='pagingForm' action='" + action
+ "' method='post'>");
// 在分页条中添加总记录数
pagingBar.append("totalRecord"+ count);
pagingBar.append(" ");
pagingBar.append("total" + " "+ pages + " " + "page");
pagingBar.append(" ");
// 页数大于1显示上一页超链接,否则不显示超链接
if (iCurrPage > 1) {
pagingBar.append("<a href=" + action + "&currPage=1>" + "first" + "</a>");
pagingBar.append(" ");
pagingBar.append("<a href=" + action + "&currPage="+ (iCurrPage - 1) + ">"
+ "previous" + "</a>");
pagingBar.append(" ");
} else {
pagingBar.append( "first");
pagingBar.append(" ");
pagingBar.append("previous");
pagingBar.append(" ");
}
// 显示当前页码
pagingBar.append("<font color='red'>" + iCurrPage + "</font>");
pagingBar.append(" ");
// 页数小于总页数显示下一页超链接,否则不显示超链接
if (iCurrPage < pages) {
pagingBar.append("<a href=" + action + "&currPage="
+ (iCurrPage + 1) + ">"+ "next" + "</a>");
pagingBar.append(" ");
pagingBar.append("<a href=" + action + "&currPage=" + pages+ ">" + "last"+ "</a>");
} else {
pagingBar.append( "next");
pagingBar.append(" ");
pagingBar.append("last");
}
pagingBar.append(" ");
pagingBar.append("<input type='text' name='currPage' size='1'>");
pagingBar.append("<input type='submit' value='GO'>");
pagingBar.append("</form>");
}
map.put("list", list);// 结果集
map.put("bar", pagingBar.toString());// 分页条的字符串形式
return map;
}
}
上面涉及到了两个黑体字的方法,如下:
public Object uniqueResult(String hql,Object[] where){
Object obj = null;
try {
session = HibernateFilter.getSession(); //获取Session对象
session.beginTransaction(); //开启事物
Query query = session.createQuery(hql); //创建Query对象
//如果where不为空,则对HQL语句进行动态赋值
if(where != null && where.length > 0){
for (int i = 0; i < where.length; i++) {
if(where[i] != null){
query = query.setParameter(i, where[i]);
}
}
}
obj = query.uniqueResult(); //单值检索
session.getTransaction().commit(); //提交事物
} catch (Exception e) {
e.printStackTrace(); //打印异常信息
session.getTransaction().rollback(); //回滚事物
}
return obj;
}
public List findPaging(String hql,int offset,int length,Object[] where){
List list = null;
try {
session = HibernateFilter.getSession();//获取Session对象
session.beginTransaction(); //开启事物
Query query = session.createQuery(hql); //创建Query对象
//构建查询条件
if(where != null && where.length > 0){
for (int i = 0; i < where.length; i++) {
if(where[i] != null){
query = query.setParameter(i, where[i]);
}
}
}
list = query.setFirstResult(offset) //设置起始位置
.setMaxResults(length) //偏移量
.list(); //获取结果集
session.getTransaction().commit(); //提交事物
} catch (Exception e) {
e.printStackTrace(); //打印异常信息
session.getTransaction().rollback(); //回滚事物
}
return list;
}
分页显示的目标页面应如下:
<table border="1" align="center" width="700" >
<tr >
<td ><bean:message key="delete" /></td>
<td ><bean:message key="id" /></td>
<td ><bean:message key="medNo" /></td>
<td ><bean:message key="name" /></td>
</tr>
<logic:present name="list" scope="request">
<logic:notEmpty name="list" scope="request">
<form action='<html:rewrite page="/baseData/deleteMedicineAction.do"/>' method='post'>
<logic:iterate id="ele" name="list" scope="request">
<tr>
<td>
<input type="hidden" name="allId" value='<bean:write name="ele" property="id" />'>//增加了删除此条记录的功能,此处不多记录
<input type="checkbox" name="selectedId" value='<bean:write name="ele" property="id" />'>
</td>
<td><bean:write name="ele" property="id"/></td>
<td><bean:write name="ele" property="medNo"/></td>
<td>
<html:link href="med.do?command=view" paramId="id" paramName="ele" paramProperty="id">
<bean:write name="ele" property="name"/>
</html:link>\\此句功能为点击药品名就可以打开此药品的详细页面
</td>
</tr>
</logic:iterate>
<tr>
<td>
<table border="0" width="100%">
<tr>
<td>
<html:submit property="command">deleteSelected</html:submit>
<html:submit property="command">deleteAll</html:submit>
</td>
</form>
<td align="right">
<logic:present name="pagingBar" scope="request">
<bean:write name="pagingBar" filter="false" scope="request"/>
</logic:present>
</td>
</tr>
</table>
</td>
</tr>
</logic:notEmpty>
</logic:present>
</table>