Hibernate3+Spring3+Struts2 项目 Hibernate3实现分页查询出错
背景如题。出错信息:每次访问mygive.jsp时,都会提示错误:com.kevin.dao.util.PageList.getListForPage(PageList.java:31)
除此之外,没有任何其他信息。
代码如下:
1.mygive.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>商品</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/css.css"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jQuery-1.8.3.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/setImg.js"></script>
<script type="text/javascript">
function setGiveDttmDiv(dttmValue,divTag)
{
divTag.html("dttmValue");
}
function conformDel()
{
if(confirm("您确定要删除该记录吗?"))
{
event.returnValue=true;
}
else
{
event.returnValue=false;
}
}
</script>
</head>
<body>
<%@ include file="../../header.jsp" %>
<table width="980px" align="center">
<tr>
<td width="200px" valign="top">
<%@ include file="inc/userleft.jsp" %>
</td>
<td width="780px" valign="top">
<table border="0px">
<tr>
<td height="25px"><a href="con_mygive">商品</a></td>
</tr>
</table>
<!--列表-->
<table border="1px">
<tr>
<td width="400px" align="center">物品</td>
<td width="100px" align="center">发布时间</td>
<td width="80px" align="center">状态</td>
<td width="80px" align="center">点击</td>
<td width="120px" align="center">操作</td>
</tr>
<s:iterator value="gives">
<tr height="100px">
<td>
<table>
<tr>
<td width="150px">
<s:hidden value="id"></s:hidden>
<s:if test="pictures.size==0">
<img src="upload/nothavepic_sl.jpg" onload="javascript:ResizePic(this,100,100)" />
</s:if>
<s:else>
<s:iterator value="pictures" id="pic">
<s:if test="#pic.mainFlag==1">
<img src="${pic.slurl}" onload="javascript:ResizePic(this,100,100)" />
</s:if>
</s:iterator>
</s:else>
</td>
<td width="250px">
<table>
<tr>
<td width="250px" style="word-wrap: break-word;word-break:break-all;"><s:property value="itemName"></s:property></td>
</tr>
<tr>
<td>
<s:if test="itemOldcls==10">全新</s:if>
<s:elseif test="itemOldcls==9">9成新</s:elseif>
<s:elseif test="itemOldcls==8">8成新</s:elseif>
<s:elseif test="itemOldcls==7">7成新</s:elseif>
<s:elseif test="itemOldcls==6">6成新</s:elseif>
<s:elseif test="itemOldcls==5">5成新</s:elseif>
<s:elseif test="itemOldcls==4">4成新</s:elseif>
<s:elseif test="itemOldcls==3">3成新</s:elseif>
<s:elseif test="itemOldcls==2">2成新</s:elseif>
<s:else>1成新</s:else>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td>
<s:property value="giveDttm"></s:property>
</td>
<td>
<s:if test="giveStatus==2">
<s:property value="giveCustomer.username"/>
</s:if>
<s:elseif test="giveStatus==12">
<s:property value="itemSuoquTimes"/>人购买
</s:elseif>
<s:elseif test="giveStatus==3">
卖给<s:property value="giveCustomer.username"/>,物品已发出
</s:elseif>
<s:elseif test="giveStatus==4">
卖给<s:property value="giveCustomer.username"/>,物品已收到
</s:elseif>
<s:else>还无人购买</s:else>
</td>
<td><s:property value="itemClickTimes"></s:property>次</td>
<td>
<s:if test="giveStatus==11">
<a href="editGive?giveid=${id}">编辑</a>
<br>
</s:if>
<s:if test="giveStatus==11 || giveStatus==12">
<a href="deleteGivePro?giveid=${id}" onclick="conformDel()">删除</a>
<br>
</s:if>
</td>
</tr>
</s:iterator>
</table>
</td>
</tr>
</table>
</body>
</html>
2.Struts.xml相关代码:
<action name="con_mygive" class="com.kevin.action.ViewMyGiveAction" method="viewMyGiving">
<result name="success">/WEB-INF/content/mygive.jsp</result>
<interceptor-ref name="defaultAuthority"></interceptor-ref>
</action>
3.ViewMyGiveAction.java:
package com.kevin.action;
import java.util.List;
import com.kevin.domain.Give;
import com.kevin.service.GiveService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ViewMyGiveAction extends ActionSupport
{
private GiveService giveService;
private List<Give> gives;
public List<Give> getGives()
{
return gives;
}
public void setGives(List<Give> gives)
{
this.gives = gives;
}
public void setGiveService(GiveService giveService)
{
this.giveService = giveService;
}
public String viewMyGiving()
{
ActionContext ctx = ActionContext.getContext();
//此处直接取前3条记录,用于测试
this.gives = giveService.getGivingByUsername((String)ctx.getSession().get("username"),1,0,3);
System.out.println(this.gives.size());
if(this.gives.get(0).getPictures()==null)
{
System.out.println("Picture is null");
}
else
{
System.out.println(this.gives.get(0).getPictures().size());
}
System.out.println(this.gives.size());
return SUCCESS;
}
}
4.GiveService.java:
package com.kevin.service;
import java.util.List;
import com.kevin.domain.Give;
import com.kevin.domain.Picture;
public inte易做图ce GiveService
{
boolean saveGivePictures(List<Picture> pictures);
Long addGive(Give give);
//获取某用户所有出售
List<Give> getGivingByUsername(String username);
//获取某用户特定类别的出售:
//0:所有;
//1:出售中;
//2:出售成功;
//3:审核中;
//4:审核失败;
//5:已删除
List<Give> getGivingByUsername(String username, int giveType, int offset, int length);
void deleteGiveById(Long giveid);
}
5.GiveDao.java:
package com.kevin.dao;
import com.kevin.domain.*;
import java.util.List;
public inte易做图ce GiveDao
{
/**
* 加载Give实例
* @param id 需要加载的Give实例的主键值
* @return 返回加载的Give实例
*/
Give get(Long id);
/**
* 保存Give实例
* @param Give 需要保存的Give实例
* @return 刚刚保存的Give实例的标识属性值
*/
Long save(Give give);
/**
* 修改Give实例
* @param give 需要修改的Give实例
*/
void update(Give give);
/**
* 删除Give实例
* @param id 需要删除的Give实例的标识属性值
*/
void delete(Long id);
/**
* 删除Give实例
* @param give 需要删除的Give实例
*/
void delete(Give give);
/**
* 根据用户名查找Give
* @param name 查询的人名
* @return 指定用户名对应的全部Give
*/
List<Give> findByName(String name);
/**
* 查询全部Give实例
* @return 全部Give实例
*/
public List<Give> findAllGive();
List<Give> findGivingByUsername(String username);
void deleteGiveById(Long giveid);
List<Give> findGivingByUsername(String username, int giveType, int offset, int length);
}
6.GiveDaoHibernate.java:
package com.kevin.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.kevin.dao.GiveDao;
import com.kevin.domain.Give;
import com.kevin.dao.util.*;
public class GiveDaoHibernate extends HibernateDaoSupport implements GiveDao
{
@Override
public Give get(Long id)
{
return getHibernateTemplate().get(Give.class, id);
}
@Override
public Long save(Give give)
{
return (Long)getHibernateTemplate().save(give);
}
@Override
public void update(Give give)
{
getHibernateTemplate().update(give);
}
@Override
public void delete(Long id)
{
getHibernateTemplate().delete(get(id));
}
@Override
public void delete(Give give)
{
getHibernateTemplate().delete(give);
}
@Override
public List<Give> findByName(String name)
{
return (List<Give>)getHibernateTemplate().find("from Give g where g.itemName like ?",name);
}
@Override
public List<Give> findAllGive()
{
return (List<Give>)getHibernateTemplate().find("from Give");
}
@Override
public List<Give> findGivingByUsername(String username)
{
return (List<Give>)getHibernateTemplate().find("from Give g where g.customer.username=? order by giveDttm desc",username);
}
@Override
public List<Give> findGivingByUsername(String username,int giveType,int offset,int length)
{
String hql = "from Give"; //测试使用,简化了写法
PageList pageList = new PageList();
return (List<Give>)pageList.getListForPage(hql, offset, length);
}
@Override
public void deleteGiveById(Long giveid)
{
String hql="UPDATE Give g SET g.giveStatus=-31 where g.id="+String.valueOf(giveid);
getHibernateTemplate().bulkUpdate(hql);
}
}
--------------------编程问答-------------------- 【接主贴】
7.上面引用到的PageList.java:
package com.kevin.dao.util;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class PageList extends HibernateDaoSupport
{
public PageList()
{
super();
// TODO Auto-generated constructor stub
}
/**
* 使用hql 语句进行操作
* @param hql:要执行的语句
* @param offset
* @param length
* @return List
*/
public List getListForPage(final String hql, final int offset,final int length)
{
List list = getHibernateTemplate().executeFind
(
new HibernateCallback()
{
public Object doInHibernate(Session session) throws HibernateException, SQLException
{
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();
return list;
}
}
);
return list;
}
}
当不使用分页时,也就是根本不调用PageList里的分页方法,而是直接查询所有记录,mygive.jsp运行正常,没有任何错误。当使用Hibernate分页时,就报错:com.kevin.dao.util.PageList.getListForPage(PageList.java:31)。
其中的PageList.java:31是指
List list = getHibernateTemplate().executeFind
这行。
到底是怎么回事呢?高手帮忙看一下吧。多谢。 --------------------编程问答-------------------- 把全部的异常信息贴出来! --------------------编程问答-------------------- 建议贴关键部分代码,你这样一堆,大家看着很费事 --------------------编程问答--------------------
异常信息就是:com.kevin.dao.util.PageList.getListForPage(PageList.java:31)
没有别的了,控制台里也没有任何异常信息 --------------------编程问答--------------------
你try catch一下!
补充:Java , Java EE