转:传智播客—jbpm与OA项目(六)
今日的北京气温回升,昨天是降温。天气的变暖,让大家感觉十分温暖,课上睡意连绵。汤兄弟有发现大家的状况,所以今天拿出了一点时间与大家交流学习方法或技术上的一些问题。授课进度完全在掌握之中。
?
未来三天的内容,学习使用JBMP解决审批流转这一大模块的需求。今日的课程内容比较简单,但在实际项目中的应用却十分重要。把WEB基础搞的差不多了,这些框架并没什么难的。更多的是应该多使用,多熟悉他们。两大重点内容:通用超强分页功能、JBPM审批流程管理。
?
一、通用超强分页功能
1.分页效果
(图1.1)
图1.1中显示的分页功能是目前我见到的,功用最全的分页功能。当然也是论坛中比较常用的分页功能。我们今日就实现这个通用超级分页功能。
?
2.分页Bean
汤老师使用自己的讲课风格(应该也是他做项目时的编写风格),由广入微、由粗糙到细致。使用他的方法做分析比较好,这或许是通用的分析方式吧!
?
我们看图1.1中具有的属性:
?
OK,我们根据页面信息取出了分页Bean的属性,并设计分页Bean:
import java.util.List;
?
public?class PageView {
// 通过参数指定的信息
private?int?currentPage;// 当前页码
private?int?pageSzie;// 每页显示记录数量
// 通过查询数据库获取的信息,外部获取
private?int?recordTotal;// 总记录数
private?List?recordList;// 当前面记录信息列表
// 通过计算生成的信息
private?int?pageTotal;// 总页面数量
private?int?startIndex;// 起始页面索引
private?int?endIndex;// 结束页面索引
// 显示的页面数量
private?static?final?int?PAGE_INDEX_COUNT = 10;
?
// 在构造方法中生成各种需要的信息
public PageView(int currentPage, int pageSize, int recordTotal,
List recordList) {
this.currentPage = currentPage;
this.pageSzie = pageSize;
this.recordTotal = recordTotal;
this.recordList = recordList;
// 通过计算生成startIndex和endIndex
/*
* 因为显示的页面索引数量是有限的 我们不能把所以的页面索引一下子全列出来 我们需要动态显示页面索引列表
*/
this.pageTotal = (this.recordTotal + this.pageSzie - 1) / this.pageSzie;
// 如果页面总数<=显示页面索引数量
if (this.pageTotal <= PAGE_INDEX_COUNT) {
this.startIndex = 1;
this.endIndex = this.pageTotal;
} else {
// 根据当前页面索引生成,页面起始索引和结束索引。
// 区分偶数和奇数 页面索引数量
if (PAGE_INDEX_COUNT % 2 == 0) {
this.startIndex = this.currentPage - (PAGE_INDEX_COUNT / 2 - 1);
this.endIndex = this.currentPage + (PAGE_INDEX_COUNT / 2);
} else {
this.startIndex = this.currentPage - (PAGE_INDEX_COUNT / 2);
this.endIndex = this.currentPage + (PAGE_INDEX_COUNT / 2);
}
// 如果生成的起始索引小于1
if(this.startIndex < 1){
this.startIndex = 1;
this.endIndex = PAGE_INDEX_COUNT;
}
// 如果生成的结束索引大于总页面索引数量
if(this.endIndex > this.pageTotal){
this.endIndex = this.pageTotal;
this.startIndex = this.pageTotal - PAGE_INDEX_COUNT;
}
}
}
// ...getters AND setters
}
?
3.审批流程的DispathcAction
我们与上次课程一样,需要为审批流程编写一个DispatchAction。涉及到显示审批流程列表只有一个方法——list:
/**?
?*?显示审批流程列表?
?*/
public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
int pageNum = Integer.parseInt(request.getParameter("pageNum"));
// 调用ProcessDefinitionService接口的getPageView方法获取PageView对象
PageView pageView = processDefinitionService.getPageView(pageNum);
// 将PageView对象存放到request的pageView属性中
request.setAttribute("pageView", pageView);
return mapping.findForward("list"); // list.jsp
}
?
其中用到的“processDefinitionService.getPageView”方法:
/**
?*?获取pageView信息
?*/
public PageView getPageView(int pageNum) {
// 每面显示10条记录
int pageSize = 10;
// 查询数据库
// 使用Number防止不同数据库返回的数值类型不同,而引发的异常。
int count = ((Number) this.getSession().createQuery(
"SELECT COUNT(*) FROM " + ProcessDefinition.class.getName()
+ " pd").uniqueResult()).intValue();
// 第一条记录的索引
int firstRecoderIndex = (pageNum - 1) * pageSize;
// 获取记录列表
List list = this.getSession().createQuery(
"FROM " + ProcessDefinition.class.getName() + " pd")
.setFirstResult(firstRecoderIndex).setMaxResults(pageSize)
.list();
return?new PageView(pageNum, pageSize, count, list);
}
?
4.显示分页信息的pageView.jspf页面
多处使用到分页页面,所以我们将分页页面单独提取出来。如果有哪个页面需要显示分页信息,直接include就可以了。
<%@?page?language="java"?pageEncoding="utf-8"?%>
<%@?taglib?prefix="c"?uri="http://java.sun.com/jsp/jstl/core"?%>
?
<c:if?test="${pageView.totalPage gt 1 }">
?
<!-- 分页信息 -->
页码:${pageView.currentPage}/${pageView.totalPage}页
每页显示:${pageView.pageSize}条
总记录数:${pageView.recordCount}条
?
分页:
<a?href="javascript:gotoPage(1)">[首页]</a>?
<c:if?test="${pageView.currentPage gt 1}">
<a?href="javascript:gotoPage(${pageView.currentPage - 1})">[上一页]</a>?
</c:if>
<c:if?test="${pageView.currentPage lt pageView.totalPage}">
<a?href="javascript:gotoPage(${pageView.currentPage + 1})">[下一页]</a>?
</c:if>
<span style="font-family: 'Cour