//分页显示所有的日志信息 showArticleUI
public ActionForward showArticleUI(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//得到当前用户[从session取出]
Users loginUser = (Users) request.getSession().getAttribute("loginuser");
//获取页码
String pageStr = request.getParameter("page");
if(pageStr == null){
pageStr = String.valueOf(request.getSession().getAttribute("page"));
}else{
request.getSession().setAttribute("page", pageStr);
}
int page = 1;
try{
page = Integer.parseInt(pageStr);
}catch(NumberFormatException e){
}
//获得总记录数
//int count = articleService.getResult("from Article where users.id=?", new Object[]{loginUser.getId()}).size();
int count = articleService.getResultBySql("select * from article where authorId=?", new Object[]{loginUser.getId()}).size();
int maxPage = count / NUMBER;
if(count%NUMBER != 0){
maxPage ++;
}
//获得列表
//List list = articleService.getResult("from Article where users.id=? order by indate limit ? offset ?", new Object[]{loginUser.getId(),NUMBER,(page-1)*NUMBER});
List<Article> list = articleService.getResultBySql("select * from article where authorId=? order by indate limit ? offset ?", new Object[]{loginUser.getId(),NUMBER,(page-1)*NUMBER});
if(count != 0 && list == null){
//list = articleService.getResult("from Article where users.id=? order by indate limit ? offset ?", new Object[]{loginUser.getId(),NUMBER,0});
list = articleService.getResultBySql("select * from article where authorId=? order by indate limit ? offset ?", new Object[]{loginUser.getId(),NUMBER,(page-1)*NUMBER});
}
//request.setAttribute("articlelist", list);
if(list != null){
PageData data = new PageData(list,page,maxPage);
request.setAttribute("articles", data);
}
//分页部分
TurnPageForm tForm = new TurnPageForm();
tForm.setCurrentPage(page);
tForm.setMaxpage(maxPage);
request.setAttribute("turnPageForm", tForm);
//用户日志的信息
//request.setAttribute("articlelist", articleService.getResult("from Article where users.id=?", new Object[]{loginUser.getId()}));
return mapping.findForward("goShowArticleUI");
}
JSP部分:
<td>
<logic:present name="articles">
<table width="900px" cellpadding="6" cellspacing="1" bgcolor="#CCCCCC">
<logic:iterate name="articles" property="list" id="entry">
<tr>
<td bgcolor="#FFFFFF">
<font> <!-- 文章标题 -->
<a><bean:write name="entry" property="title"/></a>
<hr size="1" noshade color="#CCCCCC"><br>
<div align="right" class="smalltxt" height="32px">
发表于:<bean:write name="entry" property="indaet"/>
</div>
</font>
</td>
</tr>
</logic:iterate>
</table>
</logic:present>
</td>
我用的是Struts1.