//curPage:要查询的页数,pageSize:每页数据量
@Override
public
List getUserInfo(
int
curPage,
int
pageSize)
throws
Exception
{
// 用于存储条目列表
List list =
new
ArrayList();
AttributesMapper attributesMapper =
new
AttributesMapper() {
@Override
public
Object mapFromAttributes(Attributes attributes)
throws
NamingException {
return
attributes;
}
};
// 设置filter
String filter =
"objectclass=person"
;
// 声明搜索控件
SearchControls searchControls =
new
SearchControls();
// 指定检索范围
searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
/*
* 0:OBJECT_SCOPE,搜索指定的命名对象。
* 1:ONELEVEL_SCOPE,只搜索指定命名对象的一个级别,这是缺省值。
* 2:SUBTREE_SCOPE,搜索以指定命名对象为根结点的整棵树 **/
// 声明属性名集合,并由Map类型转换为String类型数组
// 设置要返回的属性,即所有
String[] s = {
"uid"
,
"initials"
,
"userPassword"
,
"employeeNumber"
,
"displayName"
,
"businessCategory"
,
"departmentNumber"
,
"homePhone"
,
" homePostalAddress"
,
"mail"
,
"telexNumber"
,
"telephoneNumber"
,
"postalCode"
,
"title"
,
"givenName"
,
"destinationIndicator"
,
"roomNumber"
,
"description"
,
"sn"
,
"cn"
};
searchControls.setReturningAttributes(s);
// 设置handler,handler里存储了条目的list
CollectingNameClassPairCallbackHandler handler = (CollectingNameClassPairCallbackHandler)
new
AttributesMapperCallbackHandler( attributesMapper);
// 设置页面大小
//PagedResultsControl pagedResultsControl = new PagedResultsControl(1, true);
PagedResultsResponseControl p =
new
PagedResultsResponseControl(PagedResultsResponseControl.OID,
true
,
null
);
PagedResultsCookie pagedResultsCookie =
new
PagedResultsCookie(p.getCookie());
PagedResultsRequestControl requestControl=
new
PagedResultsRequestControl(pageSize,pagedResultsCookie);
//PagedResultsRequestControl requestControl = new PagedResultsRequestControl(pageSize);
// 分为两种情况,第一种只取第一页,第二种取其他页数
if
(curPage ==
1
) {
try
{
// 只获取第一页
this
.getLdapTemplate().search(
""
, filter, searchControls, handler,requestControl);
list = handler.getList();
}
catch
(Exception e){
System.out.println(
"分页查询异常1"
+ e);
}
}
else
{
try
{
// 按分页搜索,并将结果存进handler,这里获取第一页
this
.getLdapTemplate().search(
""
, filter, searchControls, handler,requestControl);
// 循环获取其余的页数,直至目标页
for
(
int
i =
0
, cur_1 = curPage -
1
, cur_2 = curPage -
2
; i < cur_1; i++)
{
// 声明临时变量handlerTemp。用于每次存储该次搜索的页面信息
CollectingNameClassPairCallbackHandler handlerTemp =
new
AttributesMapperCallbackHandler(attributesMapper);
// 设置页面大小
// PagedResultsCookie pagedResultsCookie1 = new PagedResultsCookie(p.getCookie());
requestControl =
new
PagedResultsRequestControl(pageSize,requestControl.getCookie());
// 在上次查询的基础上继续往后查询
this
.getLdapTemplate().search(
""
, filter, searchControls,handlerTemp, requestControl);
// 检测为当前页,获取当前页的条目列表
if
(i == cur_2){
list = handlerTemp.getList();
}
}
}
catch
(Exception e){
System.out.println(
"分页查询异常2"
+ e);
}
}
return
list;
}