用struts+spring+hibernate怎么实现分页啊
麻烦提供一下最简单的实现代码Queryquery=session.createQuery("fromCourse");query.setFirstResult(5);qu...
麻烦提供一下最简单的实现代码
Query query = session.createQuery("from Course");
query.setFirstResult(5);
query.setMaxResults(10);
return query.list();
这样好像没什么问题啊,可出现异常
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:630)
org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:343)
我要是去掉屏蔽分页的两行代码
query.setFirstResult(5);
query.setMaxResults(10);
就好使了,为什么呀,要是去掉还怎么分页啊,怎么搞的? 展开
Query query = session.createQuery("from Course");
query.setFirstResult(5);
query.setMaxResults(10);
return query.list();
这样好像没什么问题啊,可出现异常
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:630)
org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:343)
我要是去掉屏蔽分页的两行代码
query.setFirstResult(5);
query.setMaxResults(10);
就好使了,为什么呀,要是去掉还怎么分页啊,怎么搞的? 展开
2个回答
展开全部
用标签加hibernate就可以了,hibernate实现分页检索,就是findbypage,,页面就靠标签了,下面是标签类
public class PageTag extends BodyTagSupport {
/**
* serialVersionUID.
*/
private static final long serialVersionUID = -4686685719382532353L;
/**
* 控件ID.
*/
private String styleId = null;
/**
* 每页的禅轿页数.
*/
private int pageSize = 20;
/**
* 显桥禅示的贺消肆最大滚动页数.
*/
private int showPageCnt = 5;
/**
* request记录的PageView名.
*/
private String name = "";
/*
* 样式.
*/
private String styleClass = "";
/**
* 宽度.
*/
private String width = "100%";
/**
* @return the styleClass
*/
public final String getStyleClass() {
return this.styleClass;
}
/**
* @return the width
*/
public final String getWidth() {
return this.width;
}
/**
* @param aStyleClass
* the styleClass to set
*/
public final void setStyleClass(String aStyleClass) {
this.styleClass = aStyleClass;
}
/**
* @param aWidth
* the width to set
*/
public final void setWidth(String aWidth) {
this.width = aWidth;
}
/**
* 开始.
*/
public final int doStartTag() throws JspException {
// 变量声明
final JspWriter writer = pageContext.getOut();
ServletRequest request = pageContext.getRequest();
String tagJsp = "";
// 控件生成
tagJsp = makePageViewJsp((PageView) request.getAttribute(name));
try {
// 加入标签内容
writer.println(tagJsp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// 提示JSP错误
JspException exception = new JspException(
"[MatrixTag getOut().print]:" + e.getMessage());
throw exception;
}
// 正常完了
return SKIP_BODY;
}
/**
* 结束.
*/
public final int doEndTag() {
// 正常完了
return EVAL_PAGE;
}
/**
* 生成控件.
*
* @param src
* @return String
*/
private String makePageViewJsp(PageView src) {
// 变量声明
StringBuffer outStr = new StringBuffer();
int minPage = 1;
int maxPage = 1;
// 样式
outStr.append("<table id='").append(styleId).append(
"' cellspacing='0' width='").append(this.width).append(
"' border='1' class='").append(this.styleClass).append("'>")
.append("\n");
outStr.append("<tr>").append("\n");
outStr.append("<td>").append("\n");
// 首页 上一页
if (src.getPage() > 1) {
outStr
.append(
" <a href=\"#\" onclick=\"peraSubmit('paging','styleId=")
.append(styleId)
.append("&page=1');\">")
.append(PeraContants.APP_BUNDLE.getString("pro.page.first"))
.append("</a>").append("\n");
outStr.append(
" <a href=\"#\" onclick=\"peraSubmit('paging','styleId=")
.append(styleId).append("&page=").append(src.getPage() - 1)
.append("');\">").append(
PeraContants.APP_BUNDLE.getString("pro.page.prev"))
.append("</a>").append("\n");
} else {
outStr.append(" ").append(
PeraContants.APP_BUNDLE.getString("pro.page.first"))
.append("\n");
outStr.append(" ").append(
PeraContants.APP_BUNDLE.getString("pro.page.prev")).append(
"\n");
}
// 页码
if (this.showPageCnt > src.getPageCnt()) {
minPage = 1;
maxPage = src.getPageCnt();
} else {
if (src.getPage() > (this.showPageCnt - 1) / 2) {
minPage = src.getPage() - (this.showPageCnt - 1) / 2;
} else {
minPage = 1;
}
if (src.getPageCnt() > minPage + this.showPageCnt - 1) {
maxPage = minPage + this.showPageCnt - 1;
} else {
maxPage = src.getPageCnt();
minPage = src.getPageCnt() - this.showPageCnt + 1;
}
}
for (int i = minPage; i <= maxPage; i++) {
// 当前页
if (i != src.getPage()) {
outStr
.append(
" <a href=\"#\" onclick=\"peraSubmit('paging','styleId=")
.append(styleId).append("&page=").append(i).append(
"');\">").append(i).append("</a>").append("\n");
} else {
outStr.append("<span class='menu_active'>").append(i).append(
"</span>").append("\n");
}
}
// 下一页 尾页
if (src.getPage() < src.getPageCnt()) {
outStr.append(
" <a href=\"#\" onclick=\"peraSubmit('paging','styleId=")
.append(styleId).append("&page=").append(src.getPage() + 1)
.append("');\">").append(
PeraContants.APP_BUNDLE.getString("pro.page.next"))
.append("</a>").append("\n");
outStr.append(
" <a href=\"#\" onclick=\"peraSubmit('paging','styleId=")
.append(styleId).append("&page=").append(src.getPageCnt())
.append("');\">").append(
PeraContants.APP_BUNDLE.getString("pro.page.end"))
.append("</a>").append("\n");
} else {
outStr.append(" ").append(
PeraContants.APP_BUNDLE.getString("pro.page.next")).append(
"\n");
outStr.append(" ").append(
PeraContants.APP_BUNDLE.getString("pro.page.end")).append(
"\n");
}
// 结束
outStr.append("</td>").append("\n");
outStr.append("</tr>").append("\n");
outStr.append("</table>").append("\n");
// 隐藏
outStr.append("<input type='hidden' id='page_").append(styleId).append(
"' name='page_").append(styleId).append("' value='").append(
src.getPage()).append("'/>").append("\n");
// 返回
return outStr.toString();
}
/**
* @return the name
*/
public final String getName() {
return this.name;
}
/**
* @return the pageSize
*/
public final int getPageSize() {
return this.pageSize;
}
/**
* @return the showPageCnt
*/
public final int getShowPageCnt() {
return this.showPageCnt;
}
/**
* @return the styleId
*/
public final String getStyleId() {
return this.styleId;
}
/**
* @param aName
* the name to set
*/
public final void setName(String aName) {
this.name = aName;
}
/**
* @param aPageSize
* the pageSize to set
*/
public final void setPageSize(int aPageSize) {
this.pageSize = aPageSize;
}
/**
* @param aShowPageCnt
* the showPageCnt to set
*/
public final void setShowPageCnt(int aShowPageCnt) {
this.showPageCnt = aShowPageCnt;
}
/**
* @param aStyleId
* the styleId to set
*/
public final void setStyleId(String aStyleId) {
this.styleId = aStyleId;
}
}
public class PageTag extends BodyTagSupport {
/**
* serialVersionUID.
*/
private static final long serialVersionUID = -4686685719382532353L;
/**
* 控件ID.
*/
private String styleId = null;
/**
* 每页的禅轿页数.
*/
private int pageSize = 20;
/**
* 显桥禅示的贺消肆最大滚动页数.
*/
private int showPageCnt = 5;
/**
* request记录的PageView名.
*/
private String name = "";
/*
* 样式.
*/
private String styleClass = "";
/**
* 宽度.
*/
private String width = "100%";
/**
* @return the styleClass
*/
public final String getStyleClass() {
return this.styleClass;
}
/**
* @return the width
*/
public final String getWidth() {
return this.width;
}
/**
* @param aStyleClass
* the styleClass to set
*/
public final void setStyleClass(String aStyleClass) {
this.styleClass = aStyleClass;
}
/**
* @param aWidth
* the width to set
*/
public final void setWidth(String aWidth) {
this.width = aWidth;
}
/**
* 开始.
*/
public final int doStartTag() throws JspException {
// 变量声明
final JspWriter writer = pageContext.getOut();
ServletRequest request = pageContext.getRequest();
String tagJsp = "";
// 控件生成
tagJsp = makePageViewJsp((PageView) request.getAttribute(name));
try {
// 加入标签内容
writer.println(tagJsp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// 提示JSP错误
JspException exception = new JspException(
"[MatrixTag getOut().print]:" + e.getMessage());
throw exception;
}
// 正常完了
return SKIP_BODY;
}
/**
* 结束.
*/
public final int doEndTag() {
// 正常完了
return EVAL_PAGE;
}
/**
* 生成控件.
*
* @param src
* @return String
*/
private String makePageViewJsp(PageView src) {
// 变量声明
StringBuffer outStr = new StringBuffer();
int minPage = 1;
int maxPage = 1;
// 样式
outStr.append("<table id='").append(styleId).append(
"' cellspacing='0' width='").append(this.width).append(
"' border='1' class='").append(this.styleClass).append("'>")
.append("\n");
outStr.append("<tr>").append("\n");
outStr.append("<td>").append("\n");
// 首页 上一页
if (src.getPage() > 1) {
outStr
.append(
" <a href=\"#\" onclick=\"peraSubmit('paging','styleId=")
.append(styleId)
.append("&page=1');\">")
.append(PeraContants.APP_BUNDLE.getString("pro.page.first"))
.append("</a>").append("\n");
outStr.append(
" <a href=\"#\" onclick=\"peraSubmit('paging','styleId=")
.append(styleId).append("&page=").append(src.getPage() - 1)
.append("');\">").append(
PeraContants.APP_BUNDLE.getString("pro.page.prev"))
.append("</a>").append("\n");
} else {
outStr.append(" ").append(
PeraContants.APP_BUNDLE.getString("pro.page.first"))
.append("\n");
outStr.append(" ").append(
PeraContants.APP_BUNDLE.getString("pro.page.prev")).append(
"\n");
}
// 页码
if (this.showPageCnt > src.getPageCnt()) {
minPage = 1;
maxPage = src.getPageCnt();
} else {
if (src.getPage() > (this.showPageCnt - 1) / 2) {
minPage = src.getPage() - (this.showPageCnt - 1) / 2;
} else {
minPage = 1;
}
if (src.getPageCnt() > minPage + this.showPageCnt - 1) {
maxPage = minPage + this.showPageCnt - 1;
} else {
maxPage = src.getPageCnt();
minPage = src.getPageCnt() - this.showPageCnt + 1;
}
}
for (int i = minPage; i <= maxPage; i++) {
// 当前页
if (i != src.getPage()) {
outStr
.append(
" <a href=\"#\" onclick=\"peraSubmit('paging','styleId=")
.append(styleId).append("&page=").append(i).append(
"');\">").append(i).append("</a>").append("\n");
} else {
outStr.append("<span class='menu_active'>").append(i).append(
"</span>").append("\n");
}
}
// 下一页 尾页
if (src.getPage() < src.getPageCnt()) {
outStr.append(
" <a href=\"#\" onclick=\"peraSubmit('paging','styleId=")
.append(styleId).append("&page=").append(src.getPage() + 1)
.append("');\">").append(
PeraContants.APP_BUNDLE.getString("pro.page.next"))
.append("</a>").append("\n");
outStr.append(
" <a href=\"#\" onclick=\"peraSubmit('paging','styleId=")
.append(styleId).append("&page=").append(src.getPageCnt())
.append("');\">").append(
PeraContants.APP_BUNDLE.getString("pro.page.end"))
.append("</a>").append("\n");
} else {
outStr.append(" ").append(
PeraContants.APP_BUNDLE.getString("pro.page.next")).append(
"\n");
outStr.append(" ").append(
PeraContants.APP_BUNDLE.getString("pro.page.end")).append(
"\n");
}
// 结束
outStr.append("</td>").append("\n");
outStr.append("</tr>").append("\n");
outStr.append("</table>").append("\n");
// 隐藏
outStr.append("<input type='hidden' id='page_").append(styleId).append(
"' name='page_").append(styleId).append("' value='").append(
src.getPage()).append("'/>").append("\n");
// 返回
return outStr.toString();
}
/**
* @return the name
*/
public final String getName() {
return this.name;
}
/**
* @return the pageSize
*/
public final int getPageSize() {
return this.pageSize;
}
/**
* @return the showPageCnt
*/
public final int getShowPageCnt() {
return this.showPageCnt;
}
/**
* @return the styleId
*/
public final String getStyleId() {
return this.styleId;
}
/**
* @param aName
* the name to set
*/
public final void setName(String aName) {
this.name = aName;
}
/**
* @param aPageSize
* the pageSize to set
*/
public final void setPageSize(int aPageSize) {
this.pageSize = aPageSize;
}
/**
* @param aShowPageCnt
* the showPageCnt to set
*/
public final void setShowPageCnt(int aShowPageCnt) {
this.showPageCnt = aShowPageCnt;
}
/**
* @param aStyleId
* the styleId to set
*/
public final void setStyleId(String aStyleId) {
this.styleId = aStyleId;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询