pageUtils类
public class PageUtils {
//当前页
private int currentPage;
//上一页
private int prevPage;
//下一页
private int nextPage;
//尾页
private int lastPage;
//数据总条数
private int count;
//每页条数
private int pageSize;
//分页计入数
private int pageRecord;
//分页
private String page;
public PageUtils(String currentPage,int count,int pageSize) {
init(currentPage, count, pageSize);
initPrevPage();
initLastPage();
initNextPage();
initPageRecord();
initPage();
}
//初始化变量的值
private void init(String currentPage,int count,int pageSize) {
if(currentPage==null||currentPage.equals("")) {
currentPage="1";
}
this.currentPage = Integer.parseInt(currentPage);
this.count = count;
this.pageSize = pageSize;
}
//计算上一页
private void initPrevPage() {
if(currentPage==1) {
prevPage = 1;
}else {
prevPage = currentPage-1;
}
}
//计算最后一页
private void initLastPage() {
if(count%pageSize==0) {
lastPage=count/pageSize;
}else {
lastPage=count/pageSize+1;
}
}
//计算下一页
private void initNextPage() {
if(currentPage==lastPage) {
nextPage=lastPage;
}else {
nextPage=currentPage+1;
}
}
//计算计入数
private void initPageRecord() {
pageRecord = (currentPage-1)*pageSize;
}
private void initPage() {
page = "第"+currentPage+"/"+lastPage+"页,共"+count+"条数据。";
page +="<input type='button' value='首页' onclick='page(1)' >";
page +="<input type='button' value='上一页' onclick='page("+prevPage+")' >";
page +="<input type='button' value='下一页' onclick='page("+nextPage+")' >";
page +="<input type='button' value='尾页' onclick='page("+lastPage+")' >";
}
public int getCurrentPage() {
return currentPage;
}
public int getPrevPage() {
return prevPage;
}
public int getNextPage() {
return nextPage;
}
public int getLastPage() {
return lastPage;
}
public int getCount() {
return count;
}
public int getPageSize() {
return pageSize;
}
public int getPageRecord() {
return pageRecord;
}
public String getPage() {
return page;
}
}
list页面
<script type="text/javascript">
function page(i){
$("[name='currentPage']").val(i);
$("form").submit();
}
</script>
<body>
<form action="list.do" method="post">
<input type="hidden" name="currentPage">
<input type="submit" value="查询">
</form>
<table>
<tr>
<td colspan="20">${page }</td>
</tr>
</table>
</body>
controller层 list分支 solr分页 高亮等
@RequestMapping("list.do")
public String list(@RequestParam(defaultValue="0")int typeid,String currentPage,Model model,@RequestParam(defaultValue="")String name) throws SolrServerException, IOException {
Map map = new HashMap();
//solr查询条件对象
SolrQuery query = new SolrQuery();
//query.set("q", "*:*");
if(name.equals("")) {
name="*";
}
//设置查询条件
query.setQuery(name);
//默认域
query.set("df","name");
//高亮
query.setHighlight(true);
query.addHighlightField("name");
query.setHighlightSimplePre("<span style='color:red'>");
query.setHighlightSimplePost("</span>");
//下拉列表的查询
if(typeid ==0 || typeid ==1) {
}else {
query.setFilterQueries("typeid:"+typeid);
}
//分页
int pageSize=2;
//总页数
int count = (int) sc.query(query).getResults().getNumFound();
//分页对象
PageUtils pageUtils = new PageUtils(currentPage, count, pageSize);
//设置分页
query.setStart(pageUtils.getPageRecord());
query.setRows(pageUtils.getPageSize());
//执行查询
QueryResponse response = sc.query(query);
//封装到java集合
List<Contract> list = response.getBeans(Contract.class);
//取出高亮的值进行替换
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
for (Contract contract : list) {
if(highlighting.get(contract.getId()).size()==0) {
continue;
}
Map<String, List<String>> map2 = highlighting.get(contract.getId());
List<String> list2 = map2.get("name");
String name2 = contract.getName();
if(list2!=null && list2.size()>0) {
name2=list2.get(0);
}
contract.setName(name2);
}
if(name.equals("*")) {
name="";
}
map.put("name", name);
map.put("typeid", typeid);
model.addAttribute("map", map);
model.addAttribute("list", list);
model.addAttribute("page", pageUtils.getPage());
return "list";
}