日期:2014-05-20  浏览次数:20732 次

struts hibernate 查询分页问题!
出现的错误:java.lang.ClassCastException: java.lang.Long
at com.ls.dao.SiteDAO.getPetsCount(SiteDAO.java:300)
at com.ls.struts.action.SearchSiteAction.execute(SearchSiteAction.java:56)

我的jsp页面分页代码:
<script type="text/javascript">
function setPageSize()
{
if(${pageResult.recordCount} < ${pageResult.pageSize})
{
document.forms[0].elements["pageResult.pageSize"].value = ${pageResult.recordCount};
}
}
window.document.body.onload = setPageSize;
</script>
共${pageResult.recordCount}条记录
每页显示<input name="pageResult.pageSize" value="${pageResult.pageSize}" size="3" />条
第<input name="pageResult.pageNo" value="${pageResult.pageNo}" size="3" />页
/ 共${pageResult.pageCount}页 
<a href="javascript:page_first();">第一页</a> 
<a href="javascript:page_pre();">上一页</a>
<a href="javascript:page_next();">下一页</a> 
<a href="javascript:page_last();">最后一页</a>
<button onClick="javascript:page_go();">GO</button>

<script>
function page_go()
{
page_validate();
document.forms[0].submit();
}
function page_first()
{
document.forms[0].elements["pageResult.pageNo"].value = 1;
document.forms[0].submit();
}
function page_pre()
{
var pageNo = document.forms[0].elements["pageResult.pageNo"].value;
document.forms[0].elements["pageResult.pageNo"].value = parseInt(pageNo) - 1;
page_validate();
document.forms[0].submit();
}
function page_next()
{
var pageNo = document.forms[0].elements["pageResult.pageNo"].value;
document.forms[0].elements["pageResult.pageNo"].value = parseInt(pageNo) + 1;
page_validate();
document.forms[0].submit();
}
function page_last()
{
document.forms[0].elements["pageResult.pageNo"].value = ${pageResult.pageCount};
document.forms[0].submit();
}
function page_validate()
{
var pageTotal = ${pageResult.pageCount};
var pageNo = document.forms[0].elements["pageResult.pageNo"].value;
if (pageNo<1)pageNo=1;
if (pageNo>pageTotal)pageNo=pageTotal;
document.forms[0].elements["pageResult.pageNo"].value = pageNo;

var recTotal = ${pageResult.recordCount};
var pageSize = document.forms[0].elements["pageResult.pageSize"].value;
if (pageSize<1)pageSize=10;
if(pageSize>recTotal)pageSize=recTotal;
document.forms[0].elements["pageResult.pageSize"].value = pageSize;
}

</script>


Action里的代码:Integer petsCount = siteDAO.getPetsCount(site);
pageResult.setRecordCount(petsCount);

Integer pageCount = null;

if (petsCount % pageResult.getPageSize() == 0) {
pageCount = petsCount / pageResult.getPageSize();
} else {
pageCount = petsCount / pageResult.getPageSize() + 1;
}
pageResult.setPageCount(pageCount);

DAO里的代码:public Integer getPetsCount(Site site )
{
Integer petsCount = null;

Session session = super.getSession();

String hql = "select count(*) from Site site where 1=1";

Map params = null;

try
{
if(site != null)
{
params = new HashMap();

if(site.getName()!= null && !site.getName().equals(""))
{
hql += " and site.name like :name";
params.put("name", site.getName());