public class ViewContent1 extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ItemContentService ns = (ItemContentService) AppFactory.instance().getApp("contentService");
List pageList=null;//查询结果
int currentPage=1;//当前的页面
int total=0;//总页数
int pageLength = 4;//每页显示的记录数
String p=request.getParameter("page");
if(p!=null)
currentPage=Integer.parseInt(p);
try{
total=ns.getTotalRowByContent("公司介绍");//根据"公司介绍"查询到的结果集的
}catch(Exception e){
e.getMessage();
}
if(total>0){
Page page=new Page(total,pageLength,currentPage);
pageList=ns.getContentByContent("公司介绍",page.getPageStartRow(), page.getPageLength());//查询到的结果集
if(pageList.size()>0){
request.setAttribute("pageList",pageList);
request.setAttribute("totalRows",page.getTotalRows());
request.setAttribute("hasNextPage",page.isHasNextPage());
request.setAttribute("hasPreviousPage",page.isHasPreviousPage());
request.setAttribute("totalPages",page.getTotalPages());
request.setAttribute("currentPage",page.getCurrentPage());
request.setAttribute("page","不为空");
}else{
request.setAttribute("page","记录为空");
request.setAttribute("nullContent","记录为空");
}
}
return mapping.findForward("succ");
}
}
# viewContent1.jsp #
<logic:match value="记录为空" name="page" scope="request">
<tr align="center">
<bean:write name="nullContent" scope="request"/>
</tr>
</logic:match>
<tr><td bgcolor="#7E9BE5" align="center" colspan="5"><strong><a href="#" οnclick="del();">删除选中的栏目</a></strong></td></tr>
<form name="pagechange" action="viewContent1.do" method="post">
<tr align="center"><td colspan="6">共<bean:write name="totalRows" scope="request"/>条记录
<bean:write name="currentPage" scope="request"/>/<bean:write name="totalPages" scope="request"/>
<logic:present name="hasPreviousPage" scope="request">
<logic:equal name="hasPreviousPage" value="true">
<a href="viewContent1.do?page=${requestScope.currentPage-1}">上一页</a>
</logic:equal>
</logic:present>
<logic:present name="hasNextPage" scope="request">
<logic:equal name="hasNextPage" value="true">
<a href="viewContent1.do?page=${requestScope.currentPage+1}">下一页</a>
</logic:equal>
</logic:present>
跳到
<select name="page" οnchange="javascript:document.pagechange.submit();">
<%
int totalPages=((Integer)request.getAttribute("totalPages")).intValue();
for(int i=0;i<totalPages;i++){
%>
<option value="<%=i+1%>"<%if(i+1==((Integer)request.getAttribute("currentPage")).intValue()){%>selected<%}%>><%=i+1%></option>
<%}%>
</select>页
</td></tr>
</form>
public class Page {
private int currentPage=1;
private int totalRows=0;//总记录数
private int pageStartRow=0;//开始记录位置
private int pageLength=10;//页记录长度
private int totalPages=0;//总页数
private boolean hasPreviousPage=false; //是否有上一页
private boolean hasNextPage=false; //是否有下一页
public Page() {
}
public Page(int totalRows, int pageLength, int cPage) {
this.currentPage=cPage;
this.totalRows=totalRows;
this.pageLength=pageLength;
init();
}
private void init(){
if(totalRows%pageLength==0)
totalPages=totalRows/pageLength;
else
totalPages=totalRows/pageLength+1;
if(currentPage>totalPages)
currentPage=totalPages;
else if(currentPage<1)
currentPage=1;
if(currentPage<totalPages)
{
hasNextPage=true;
}else{
hasNextPage=false;
}
if(currentPage>1)
{
hasPreviousPage=true;
}else{
hasPreviousPage=false;
}
pageStartRow=(currentPage-1)*pageLength;
}
public int getPageLength() {
return pageLength;
}
public int getPageStartRow() {
return pageStartRow;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public void setPageLength(int pageLength) {
this.pageLength = pageLength;
}
public void setPageStartRow(int pageStartRow) {
this.pageStartRow = pageStartRow;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
}
# struts-config.xml #
<action path="/admin/viewContent1" type="com.cst.web.background.introduction.ViewContent1" scope="request" >
<forward name="succ" path="/WEB-INF/jsp/back/dynamic/viewContent1.jsp" />
</action>