struts2 显示分页 简例
使用ssh整合实现分页显示部分核心代码:
jsp实现:
<s:property value="str" />
<br />
记录共有:
<s:property value="listcount" />
<br />
<s:iterator var="counter" begin="0" end="listu.size()-1">
姓名: <s:property value="listu.get(#counter).getName()" />
<br />
地址:<s:property value="listu.get(#counter).getAddress()" />
<br />
</s:iterator>
共有<s:property value="pageNub"/>页
<s:form action="fylist" method="post">
<s:textfield label="now page" name="nowpage"></s:textfield>
<s:submit value=" up page " method="uppage"></s:submit><s:submit value="next page" method="nextpage"></s:submit>
</s:form>
当前页<s:property value="fr/mr+1"/>
dao实现:
public List<Users> fyList(int fr,int mr){
Session s = sessionFactory.getCurrentSession();
String sql="from Users";
Query q = s.createQuery(sql);
q.setFirstResult(fr);
q.setMaxResults(mr);
this.list=(List<Users>)q.list();
return list;
}
server实现:
@Transactional
private void fyreaduser() {
fr=0;
fr=(nowpage-1)*mr;
this.str = "this is fylist!";
// select count(*) from Users
Iterator<Users> it = userdao.fyList(fr, mr).iterator();
while (it.hasNext()) {
this.listu.add((Users) it.next());
}
for (int i = 0; i < listu.size(); i++) {
System.out.print(listu.get(i).getName() + " ");
}
}