jsp+struts1分页

后台数据库是mysql5.X,使用mysql的分页语句,如:select * from test limit 0,10;

Dao中分页取数据的方法:

public List<Users> getPage(int start,int pageRecord)
{
conn = DBUtil.getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Users> userList = new ArrayList<Users>();

String sql = "select login_name,email,address,tel from users limit ?,?";
try
{
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, start);
pstmt.setInt(2, pageRecord);
rs = pstmt.executeQuery();
Users user = null;
while(rs.next())
{
user = new Users();
user.setLoginName(rs.getString("login_name"));
user.setEmail(rs.getString("email"));
user.setAddress(rs.getString("address"));
user.setTel(rs.getString("tel"));
userList.add(user);
}
} catch (SQLException e)
{

e.printStackTrace();
}
finally
{

DBUtil.close(rs);
DBUtil.close(pstmt);
DBUtil.close(conn);
}
return userList;
}


分页的action:

public class PageAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");

//当前页
String currentPageStr = request.getParameter("pageNo");
int currentPage=0;
//每页显示的记录数
int pageSize = 10;
try
{
currentPage =Integer.parseInt(currentPageStr);
}
catch(NumberFormatException e)
{
currentPage = 1;
}
//在数据库中从第几条开始
int start = (currentPage -1) * 10;
//在数据库中已分页的数据集合
List<Users> list = DaoFactory.getUsersDaoInstance().getPage(start, pageSize);
//所有记录的集合
List<Users> allRecordList = DaoFactory.getUsersDaoInstance().getAll();
//所有记录数
int allRecord = allRecordList.size();

//总页数
int totalPage = allRecord % pageSize==0?allRecord/pageSize : allRecord/pageSize + 1;
List<Users> l = new ArrayList<Users>();
Users user = null;

for(Users u : list)
{
user = new Users();

user.setLoginName(u.getLoginName());
user.setEmail(u.getEmail());
user.setTel(u.getTel());
user.setAddress(u.getAddress());

l.add(user);
}
//总记录数
request.getSession().setAttribute("allRecord", allRecord);
//总页数
request.getSession().setAttribute("totalPage", totalPage);
//当前页数
request.getSession().setAttribute("currentPage", currentPage);
request.getSession().setAttribute("list", l);
return mapping.findForward("success");
}
}


前台页面代码:


<table align="center" border="0">
<tr>
<td width="76%">
<font size=4>总共${allRecord}条记录 - 当前页${currentPage }/总页数${totalPage }:</font>
</td>
</tr>
<tr>
<c:if test="${currentPage<=1}">
首页
</c:if>
<c:if test="${currentPage>1}">
<a href="page.do?pageNo=1">首页</a>
</c:if>
<c:if test="${currentPage<totalPage}">
<a href="page.do?pageNo=${currentPage +1}"+>下一页</a>
</c:if>
<c:if test="${currentPage>=totalPage}">
下一页
</c:if>
<c:if test="${currentPage<=1}">
上一页
</c:if>
<c:if test="${currentPage>1}">
<a href="page.do?pageNo=${currentPage -1 }">上一页</a>
</c:if>

<c:if test="${currentPage>=totalPage}">
尾页
</c:if>
<c:if test="${currentPage<totalPage}">
<a href="page.do?pageNo=${totalPage}">尾页</a>
</c:if>
</tr>
</table>
<table border="2" align="center">

<tr>
<td>用户名</td>
<td>email</td>
<td>电话</td>
<td>地址</td>
</tr>
<c:forEach items="${list}" var="l">
<tr>

<td>${l.loginName}</td>
<td>${l.email }</td>
<td>${l.tel }</td>
<td>${l.address }</td>
</tr>
</c:forEach>
</table>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值