Action
public ActionForward pageList(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
String name=request.getParameter("name");
String posttime=request.getParameter("posttime");
String pNo=request.getParameter("pageNo");
Note_ApplyDAO noteDAO=new Note_ApplyDAO();
Pagination page=noteDAO.pageList(name, posttime, pNo);
request.setAttribute("pageBean", page);
List list = page.getDataList();
request.setAttribute("name", name);
request.setAttribute("posttime", posttime);
request.setAttribute("list", list);
return mapping.findForward("list");
}
DAO:
public Pagination pageList(String name, String posttime, String pNo) {
int pageNo = 0;
Pagination page = new Pagination();
StringBuffer strBuffer = new StringBuffer();
if (name != null && name.trim().length() > 0) {
strBuffer.append("where name ='" + name + "'");
}
if (posttime != null && posttime.trim().length() > 0) {
Date posttime1 = Date.valueOf(posttime);
strBuffer.append("where posttime='" + posttime1 + "'");
}
//system.out.println(strbuffer.tostring());
if (pNo != null && pNo.length() > 0) {
pageNo = Integer.valueOf(pNo);
}
page = page.getData(Note_Apply.class,strBuffer.toString(), pageNo);
return page;
}
Pagination:
public Pagination getData(Class c,String whereSql,int PageNo){
Session session = null;
List list = null;
int allCount = 0;
Pagination page = new Pagination();
session = HibernateUtils.getCurrentSession();
// 组装sql语句,查询总记录数
StringBuffer sqlbuffer = new StringBuffer( "select count(*) from " + c.getName( ) );
if ( whereSql != null ) {
sqlbuffer.append( whereSql );
}
session = HibernateUtils.getCurrentSession();
try {
Query qRows = session.createQuery( sqlbuffer.toString() );
Iterator iterator = qRows.iterate( );
// 得到总记录数
allCount =(Integer)iterator.next( );
sqlbuffer = new StringBuffer( "from " + c.getName( ) );
if ( whereSql != null ) {
sqlbuffer.append( whereSql );
}
// 得到数据集
Query queryList = session.createQuery( sqlbuffer.toString( ) );
queryList.setFirstResult( pageSize*PageNo-pageSize );
queryList.setMaxResults( pageSize );
list =queryList.list( );
page.setDataList(list);
//设置总记录数
page.setRowsCount(allCount);
//设置当前页
if(PageNo==0){
page.setCurPage( 1 );
}else{
page.setCurPage( PageNo );
}
// 设置总页数
if ( page.getRowsCount()% this.pageSize == 0 ) {
page.setMaxPage( page.rowsCount/ this.pageSize );
} else {
page.setMaxPage( page.rowsCount / this.pageSize + 1 );
}
}catch(HibernateException e){
e.printStackTrace();
}
return page;
}
问题:Query queryList = session.createQuery( sqlbuffer.toString( ) );如果有条件的话这句话就会出错。
javax.servlet.ServletException: javax.servlet.ServletException: java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)