第1个回答 2011-01-01
上个星期也遇到与你一样的问题
其实我们自己考虑不周的原因,平时一般点击“下一页”,只是把当前第几页传过去,而查找的内容没有传过去,当时不能显示我们想像的效果
所以:
1、当第一次查找时,记得一定也要把查找的内容传到页面
2、在点击“下一页”里(其它按钮也一样),要把查找的内容也传过去,如果没有,传个null也行;
3、在第二个页里,接收信息时判断一下查找的内容是否为空,从而进行是模糊查询还是列出所有信息,取得数据后,判断查找的内容如果不为空的话,再把查找的内容传到页面
这样就没有问题了
另外,代码写得缺乏可重用性本回答被网友采纳
第3个回答 2011-01-02
这是dao层,db层你自己写下
/**
* 新品营地
* 返回与当前时间相关months个月的商品条数
* @param months
* @return
*/
public int getAllCount(int months)
{
int allCount=0;
ConnDB connDB=new ConnDB();
Connection conn=connDB.getConn();
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select count(*) from goods where MONTHS_BETWEEN(sysdate,goodsTime)<=?";
try {
ps=conn.prepareStatement(sql);
ps.setInt(1, months);
rs=ps.executeQuery();
if(rs.next())
{
allCount=rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
connDB.closeDB(conn, ps, rs);
}
return allCount;
}
/**
* 新品营地
* @param curPage
* @param perPage
* @param months
* @return
*/
public List<GoodsBean> selectNewGoods(int curPage,int perPage,int months)
{
List<GoodsBean> list=new ArrayList<GoodsBean>();
ConnDB connDB=new ConnDB();
Connection conn=connDB.getConn();
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select * from (select * from goods where MONTHS_BETWEEN(sysdate,goodsTime)<=?) where GoodsName not in(select GoodsName from goods where MONTHS_BETWEEN(sysdate,goodsTime)<=? and rownum<=?) and rownum<=?";
GoodsBean g=null;
try {
ps=conn.prepareStatement(sql);
ps.setInt(1, months);
ps.setInt(2, months);
ps.setInt(3, curPage*perPage);
ps.setInt(4, perPage);
rs=ps.executeQuery();
while(rs.next())
{
g=new GoodsBean();
g.setGoodsId(rs.getInt("goodsId"));
g.setGoodsDispatcherName(rs.getString("goodsDispatcherName"));
g.setGoodsName(rs.getString("goodsName"));
g.setGoodsPrice(rs.getFloat("goodsPrice"));
g.setGoodsDiscount(rs.getFloat("goodsDiscount"));
g.setGoodsAmount(rs.getInt("goodsAmount"));
g.setGoodsType(rs.getString("goodsType"));
g.setGoodsIntroduce(rs.getString("goodsIntroduce"));
g.setGoodsPhoto(rs.getString("goodsPhoto"));
g.setGoodsCliks(rs.getInt("goodsCliks"));
g.setGoodsSalAmount(rs.getInt("goodsSalAmount"));
g.setGoodsRecommentNum(rs.getInt("goodsRecommentNum"));
g.setGoodsTime(rs.getString("goodsTime"));
list.add(g);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
connDB.closeDB(conn, ps, rs);
}
return list;
}
这是控制层
int months = 2;// 离当前时间为2个月为新品
GoodsSearchDao gsd = new GoodsSearchDao();
int allCount = gsd.getAllCount(months);//数据总条数
int perPage = 2;//每页显示多少
int numPage = 0;//总共多少页
if (allCount % perPage == 0) {
numPage = allCount / perPage;
} else {
numPage = allCount / perPage + 1;
}
request.setAttribute("numPage", numPage);
String cur = request.getParameter("cur");
int curPage = 0;
if (cur != null && !cur.equalsIgnoreCase("")) {
curPage = Integer.parseInt(cur);
}
request.setAttribute("curPage", curPage);
List<GoodsBean> goodsSearch = gsd.selectNewGoods(curPage, perPage,
months);
request.setAttribute("goodsSearch", goodsSearch);
页面层
<c:choose>
<c:when test="${curPage==0}">
首页 上一页
</c:when>
<c:otherwise>
<a href="../goodsServlet?add=${add}&type=${type}">首页</a> <a
href="../goodsServlet?cur=${curPage-1}&add=${add}&type=${type}">上一页</a>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${curPage==numPage-1}">
下一页 末页
</c:when>
<c:otherwise>
<a href="../goodsServlet?cur=${curPage+1}">下一页</a> <a
href="../goodsServlet?cur=${numPage-1}">末页</a>
</c:otherwise>
</c:choose>
这是我以前写过的,有些地方不完整