Action类:
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport; import com.service.IStudentServices;
public class QueryStudentAction extends ActionSupport { private Integer pageSize = 3; //声明固定的行数 private Integer pageIndex = 1; //页码 private IStudentServices iss; //调用service层对象 private Map map=new HashMap(); //一个容器 @Override public String execute() throws Exception { map.put("list", iss.queryList(pageSize, pageIndex)); map.put("count", iss.getCount(pageSize)); return SUCCESS; }
public Integer getPageSize() { return pageSize; }
public void setPageSize(Integer pageSize) { this.pageSize = pageSize; }
public Integer getPageIndex() { return pageIndex; }
public void setPageIndex(Integer pageIndex) { this.pageIndex = pageIndex; }
public IStudentServices getIss() { return iss; }
public void setIss(IStudentServices iss) { this.iss = iss; }
public Map getMap() { return map; }
public void setMap(Map map) { this.map = map; }
}
--------------------------------------------------------------------------------------------------------------------
jsp显示层:
<body>
<s:debug></s:debug>
<form action="" method="post" name="myfrom">
<table>
<tr>
<td>ID</td>
<td>姓名</td>
</tr>
<!-- 使用OGNL迭代标签获取map里的值 -->
<s:iterator value="map.list" var="item">
?<tr>
<td><s:property value="#item.id" /></td>
<td><s:property value="#item.name" /></td>
</tr>
</s:iterator>
<!-- 将总页码数和当前页码放到隐藏域,注意页码必须和action里的一致为了替换掉之前的页码 -->
<input type ="hidden" id="count" value="<s:property value='map.count' />" />
<input type ="hidden" name="pageIndex" id="index" value="<s:property value='pageIndex' />" />
</table>
<input type="button" value="首页" onclick="fnFirst()" />
<input type="button" value="上一页" onclick="fnBack()" />
<input type="button" value="下一页" onclick="fnNext()" />
<input type="button" value="尾页" onclick="fnlast()" />
<SCRIPT type="text/javascript">
//获取隐藏域的总页数
var count=document.getElementById("count").value;
//获取当前的页码
var index=document.getElementById("index").value;
//上页操作
function fnBack(){
index--;
document.getElementById("index").value=index;
if(index<1){
document.getElementById("index").value=1;
}
alert(document.getElementById("index").value);
document.myfrom.action="query";
document.myfrom.submit();
}
//下页操作
function fnNext(){
index++;
document.getElementById("index").value=index;
if(index>count){
document.getElementById("index").value=count;
}
alert(document.getElementById("index").value);
document.myfrom.action="query";
document.myfrom.submit();
}
//首页操作
function fnFirst(){
document.getElementById("index").value=1;
}
//尾页操作
function fnlast(){
document.getElementById("index").value=count;
}
</SCRIPT>
</form>
</body>