package com.hp.shangtongwei.serviceimpl;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.hp.shangtongwei.service.songsservice;
import com.hp.shangtongwei.vo.songs;
public class songsserviceimpl extends HibernateDaoSupport implements songsservice {
@SuppressWarnings("unchecked")
public List<songs> showsongs(int currentpage,int linesize)
throws Exception {
System.out.println("come from impl:"+currentpage);
List<songs>all=new ArrayList<songs>();
// String sql="from songs as s";
final int firstresult=(currentpage-1)*linesize;
final int linesize2=linesize;
//Query q=super.getSession().createQuery(sql);
//q.setMaxResults(linesize);
// q.setFirstResult(firstresult);
//try {
// all=q.list();
//} catch (Exception e) {
//e.printStackTrace();
// }
//super.getSession().flush();
// super.getSession().close();
// super.getSession().clear();
try {
all=this.getHibernateTemplate().executeFind(new HibernateCallback(){
public List doInHibernate(Session session) throws HibernateException{
Query query = session.createQuery("from songs as s");
query.setFirstResult(firstresult);
query.setMaxResults(linesize2);
return query.list();
}
}
);
} catch (Exception e) {
e.printStackTrace();
}
return all;
}
public int songscount() throws Exception {
long resultcount=0;
String hql = "SELECT COUNT(q.id) FROM songs AS q";
//Query q = super.getSession().createQuery(hql);
List all=this.getHibernateTemplate().find(hql);
this.getHibernateTemplate().flush();
try {
//resultcount=(Long) q.list().get(0);
resultcount=(Long)all.get(0);
} catch (Exception e1) {
e1.printStackTrace();
}
return (int) resultcount;
}
}
---------------------------------------------------------
package com.hp.shangtongwei.action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.hp.shangtongwei.service.songsservice;
import com.hp.shangtongwei.vo.songs;
import com.opensymphony.xwork2.ActionSupport;
public class songsaction extends ActionSupport implements ServletRequestAware,
ServletResponseAware, ServletRequestListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private HttpServletResponse response;
private int id;
private String sname;
private String zname;
private String cname;
private int currentpage=1;
private int linesize=5;
private int allrecorders=0;
private int pagesize=1;
private songsservice mysongsservice;
public songsservice getMysongsservice() {
return mysongsservice;
}
public void setMysongsservice(songsservice mysongsservice) {
this.mysongsservice = mysongsservice;
}
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public HttpServletResponse getResponse() {
return response;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getZname() {
return zname;
}
public void setZname(String zname) {
this.zname = zname;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public void setServletRequest(HttpServletRequest arg0) {
this.request=arg0;
}
public void setServletResponse(HttpServletResponse arg0) {
this.response=arg0;
}
public void requestDestroyed(ServletRequestEvent arg0) {
}
public void requestInitialized(ServletRequestEvent arg0) {
}
public String showsongs(){
String currpage=request.getParameter("currentpage");
if(currpage!=null){
System.out.println(currpage);
try {
currentpage=Integer.parseInt(currpage);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
System.out.println(currentpage);
List<songs> allsongs=new ArrayList<songs>();
try {
allrecorders=mysongsservice.songscount();
} catch (Exception e) {
e.printStackTrace();
}
pagesize=(allrecorders+linesize-1)/linesize;
request.setAttribute("allpageno", pagesize);
try {
allsongs=mysongsservice.showsongs(currentpage,linesize);
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("allsongs", allsongs);
request.setAttribute("cpage", currentpage);
return "suc";
}
}
-------------------------------------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
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="styles.css">
-->
<%
int cpage=(Integer)request.getAttribute("cpage");
int currentpage=1;
if(cpage>1){
currentpage=cpage;
}
%>
<script type="text/javascript">
function openPage(chosepage)
{
//document.spage.currentpage.value=document.spage.but.value;
document.getElementById("currentpage").value=chosepage;
alert(chosepage);
document.spage.submit() ;
}
function selOpenPage()
{
document.getElementById("currentpage").value= document.getElementById("selpage").value;
document.spage.submit() ;
}
</script>
</head>
<%
int pageno=(Integer)request.getAttribute("allpageno");
int intpage=1;
if(pageno>1){
intpage=pageno;
}
%>
<body>
<%=currentpage%>
<%=intpage%>
<center>
<s:iterator value="#request.allsongs" status="m">
<s:if test="#m.Odd">
<font color=blue><s:property value="sname" /> <s:property
value="zname" />
<s:property value="cname"/>
</font>
</s:if>
<s:if test="#m.Even">
<font color=red><s:property value="sname" /> <s:property
value="zname" />
<s:property value="cname"/>
</font>
</s:if>
<br />
</s:iterator>
<form name="spage" action="person/songs_showsongs.bit" method="post">
<%
if(intpage>0)
{
%>
<input id="but" type="button" value="首页" onClick="openPage(1)" <%=currentpage==1?"disabled":""%>>
<input id="but" type="button" value="上一页" onClick="openPage(<%=currentpage-1%>)" <%=currentpage==1?"disabled":""%>>
<input id="but" type="button" value="下一页" onClick="openPage(<%=currentpage+1%>)" <%=currentpage==intpage?"disabled":""%>>
<input id="but" type="button" value="尾页" onClick="openPage(<%=intpage%>)" <%=currentpage==intpage?"disabled":""%>>
<input type="hidden" id="currentpage" name="currentpage" value="">
<font color="red" size="5"><%=currentpage%></font>
/
<font color="red" size="5"><%=intpage%></font>
跳转到
<select name="selpage" id="selpage" onChange="selOpenPage()">
<%
for(int x=1;x<=intpage;x++)
{
%>
<option value="<%=x%>" <%=currentpage==x?"selected":""%>><%=x%></option>
<%
}
%>
</select>
页
<%
}
%>
</form>
</center>
</body>
</html>
---------------------------------------------------------
<value>com/hp/shangtongwei/vo/songs.hbm.xml</value>
<bean id="mysongsservice" class="com.hp.shangtongwei.serviceimpl.songsserviceimpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<bean id="mysongsaction" class="com.hp.shangtongwei.action.songsaction" scope="prototype" autowire="byName">
<property name="mysongsservice">
<ref bean="mysongsservice"/>
</property>
</bean>
---------------------------------------------------------
<action name="songs_*" class="mysongsaction" method="{1}">
<result name="suc">
/showsongs.jsp
</result>
<result name="fail">
/errorshow.jsp
</result>
</action>