Struts2+Hibernate+Spring带条件的分页查询

 

 Action类

public class ShopAction extends ActionSupport{

//省略get,set方法

@Resource ShopDao shop;  //接口注入

private List list; //集合

private int PageNow=1;  //当前页数,又称开始页

private int PageSize=6;  //当前页数需要显示的条数
private int PageCount=1; //总页数

private String bname; 书名

public String cha(){
 
 PageCount=this.shop.Count(bname);  //根据输入的名字,传入这个方法中
  list=this.shop.getname(bname, PageNow, PageSize);  );//根据输入的名字,传入这个方法中

 
 return SUCCESS;

}

 

}

另一个bean类

@Service @Transactional    //事务的提交

public class Shopbean implements ShopDao{

private Connection ct=null;
 private PreparedStatement ps=null;
 private ResultSet rs=null;

 @Resource private SessionFactory sessionFactory;  //注入

public List getname(String bname, int PageNow, int PageSize) { //执行上面传过来的参数
  Query query=this.sessionFactory.getCurrentSession().createQuery("from Book where bname like '%"+bname+"%'");  //带条件的查询语句
  query.setFirstResult((PageNow-1)*PageSize);
  query.setMaxResults(PageSize);
  List list=query.list();
  return list;
 }

public int Count(String bname) {   //根据上面传过来的参数,执行这个方法
 int rowCount=0;
 int PageSize=6;
 int PageCount=0;
 try{
  ct=new Conn().getconn();
  ps=ct.prepareStatement("select count(*) from  book where bname like '%"+bname+"%'"); //根据传过来的参数,来查询总共有多少条数据符合这个参数。
  rs=ps.executeQuery();
  if(rs.next()){
   rowCount=rs.getInt(1);
   }
  if(rowCount%PageSize==0){
   PageCount=rowCount/PageSize;    //总条数%条数=总页数;
  }else{
   PageCount=rowCount/PageSize+1;
  }
 }catch(Exception e){
  e.printStackTrace();           //关闭数据库省略,你们自己加上。
 }
  return PageCount;
 }

}


页面Find.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>  //Struts2标签

<%
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>My JSP 'Find.jsp' starting page</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">
 -->

  </head>
  
  <body>
  
<jsp:include page="First.jsp"></jsp:include>

   <s:iterator value="list">
   
<s:url action="work.action" id="a">
<s:param value="bid" name="bid"></s:param>
</s:url>
<s:url action="Su.action" id="q">
   <s:param name="bid" value="bid"/>
 <s:param name="num" value="num"/>
   
   </s:url>
<table width="100%" border="0">
  <tr>
    <th height="246" scope="col"><table width="100%" border="0">
      <tr>
        <th width="25%" rowspan="4" scope="col"><s:a href="%{a}"><img src="img/<s:property value='photo'/>" width="111" height="153" border="0"/></s:a></th>
        <th width="75%" scope="col"><div align="left"><s:property value="bname"/></div></th>
      </tr>
      <tr>
        <td><br><br></td>
      </tr>
      <tr>
        <td><div align="left"><font color="#FF0000">价格:¥<s:property value="price"/>元</font></div></td>
      </tr>
      <tr>
        <td height="47"><s:a href="%{q}"><img src="img/<s:property value='photos'/>" width="191" height="77" border="0"/></s:a></td>
      </tr>
    </table></th>
  </tr>
  <input type="hidden" name="bid" value="<%=request.getParameter("bid")%>"/>
</table>
 
   </s:iterator>
   
<s:url action="Query.action" id="u">
<s:param name="PageNow" value="1"></s:param>
<s:param name="bname" value="bname"/>


</s:url>
<s:url action="Query.action" id="w">
<s:param name="PageNow" value="PageNow-1"></s:param>
<s:param name="bname" value="bname"></s:param>

</s:url>
<s:url action="Query.action" id="e">
<s:param name="PageNow" value="PageNow+1"></s:param>
<s:param name="bname" value="bname"></s:param>
</s:url>
<s:url id="l" action="Query.action">
<s:param name="PageNow" value="PageCount"></s:param>
<s:param name="bname" value="bname"/>
</s:url>

<s:property value="PageNow"/>/<s:property value="PageCount"/> 
<s:a href="%{u}">首页</s:a>


<s:a href="%{i}"></s:a>
<s:if test="PageNow!=1">
<s:a href="%{w}">上一页</s:a>
 </s:if>

 

<s:if test="PageNow!=PageCount">
<s:a href="%{e}">下一页</s:a>
</s:if>


<s:a href="%{l}">末页</s:a>
<s:debug></s:debug>

   
  </body>
</html>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值