java web分页查询初试 (四)

2014-11-24 10:24:24 · 作者: · 浏览: 3
ttpServletResponse response;

public Integer getPage()
{// 获得当前页信息
return page = (page == null || page < 1) 1 : page;
}

public void setPage(Integer page)
{// 设置当前页信息
this.page = page;
}

public String getQuery()
{// 获得query信息
return query;
}

public void setQuery(String query)
{// 设置query信息
this.query = query;
}

@Override
public void setServletResponse(HttpServletResponse arg0)
{
this.response = arg0;

}

@Override
public void setServletRequest(HttpServletRequest arg0)
{
this.request = arg0;

}
QueryAction.java


[html]
public class QueryAction extends BaseAction
{

public StudentService studentService;

public StudentService getStudentService()
{
return studentService;
}

public void setStudentService(StudentService studentService)
{
this.studentService = studentService;
}

public String query()
{
PageView pageView = new PageView(5, getPage());
pageView.setQueryResult(studentService.getScrollData(
pageView.getFirstResult(), pageView.getMaxresult()));// 查询所有记录
request.setAttribute("pageView", pageView);// 保存到request范围
return this.SUCCESS;
}
}

public class QueryAction extends BaseAction
{

public StudentService studentService;

public StudentService getStudentService()
{
return studentService;
}

public void setStudentService(StudentService studentService)
{
this.studentService = studentService;
}

public String query()
{
PageView pageView = new PageView(5, getPage());
pageView.setQueryResult(studentService.getScrollData(
pageView.getFirstResult(), pageView.getMaxresult()));// 查询所有记录
request.setAttribute("pageView", pageView);// 保存到request范围
return this.SUCCESS;
}
}
Util,分页工具类:


[html]
public class PageIndex {
private long startindex;
private long endindex;

public PageIndex(long startindex, long endindex) {
this.startindex = startindex;
this.endindex = endindex;
}
public long getStartindex() {
return startindex;
}
public void setStartindex(long startindex) {
this.startindex = startindex;
}
public long getEndindex() {
return endindex;
}
public void setEndindex(long endindex) {
this.endindex = endindex;
}

public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){
long startpage = currentPage-(viewpagecount%2==0 viewpagecount/2-1 : viewpagecount/2);
long endpage = currentPage+viewpagecount/2;
if(startpage<1){
startpage = 1;
if(totalpage>=viewpagecount) endpage = viewpagecount;
else endpage = totalpage;
}
if(endpage>totalpage){
endpage = totalpage;
if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1;
else startpage = 1;
}
return new PageIndex(startpage, endpage);
}
}

public class PageIndex {
private long startindex;
private long endindex;

public PageIndex(long startindex, long endindex) {
this.startindex = startindex;
this.endindex = endindex;
}
public long getStartindex() {
return startindex;
}
public void setStartindex(long startindex) {