Strurs2中简单的分页

在数据特别多时,一般都会有分页,分页的做法特别多,在做以前一个项目时,我们小组一个同学用tag标签写的一个分页,那套标签麻烦,功能少,只有上一页和下一页,套起来比较麻烦,这次在网上花店中用到了,就自己写了一下,感觉写的还是比较好的,当然,条条大路通罗马,程序的优化和代码量的减少一般来只靠一些好的思路和想法。

下面是页面分页的代码:

<div__ id="mycsspage" align="right">

              当前第<a style="color: red;">${sessionScope.manapage.nowpage}a>   <a href="${pageContext.request.contextPath}/shopmana/pagebackorfordard.action?pagenum=${sessionScope.manapage.nowpage}&state=forwerdpage">上一页a> / 

              <a href="${pageContext.request.contextPath}/shopmana/pagebackorfordard.action?pagenum=${sessionScope.manapage.nowpage}&state=nextpage">下一页a>   <a style="color: green;">${sessionScope.manapage.allpage}a>  转到第<input

                    value="${sessionScope.manapage.allpage}" type="text" size="2" name="pagenum" id="gotomainpage" /> <a href="javascript:void(0)" onclick="gotomainpage();">确定a>   

       div>

       <script__ type="text/javascript">

       function gotomainpage(){

              var page=document.getElementByIdx_x("gotomainpage").value;

              if(page==null||page==""){

                     page=1;

              }

              location.href="${pageContext.request.contextPath}/shopmana/pagebackorfordard.action?state=jumppage&pagenum="+page;

       }

       script>

//博客把有些html标记直接解释了,所以我用了div__

可以看到,有当前第几页,有上一页,有下一页,有总共多少页,有跳转到第几页,跳转到第几页和那个···4,5,6,7,8,9···的功能差不多是一样的。下面是配置文件:

<action name="pagebackorfordard"class="com.defu.shopaction.PageBackorFordardAction">

                     <interceptor-ref name="manaStack" />

                     <result name="success">/manager/flowerslistshow.jspresult>

                     <result name="mananull">/manager/manaloging.jspresult>

              action>

配置文件没什么说的。

下面是Action代码:

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.defu.service.Mainservice;

import com.defu.vo.Flowers;

import com.defu.vo.Page;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class PageBackorFordardAction extends ActionSupport{

       String pagenum;//传过来的当前页

       String state;//表示是什么状态,是上一页还是下一页,还是跳页

       public String getState() {

              return state;

       }

       public void setState(String state) {

              this.state = state;

       }

              public int getPagenum() {

              return Integer.parseInt(pagenum);

       }

       public void setPagenum(String pagenum) {

              this.pagenum = pagenum;

       }

       @Override

       public String execute() throws Exception {

              HttpServletRequest request=(HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);

              Mainservice service=new Mainservice();

              Listflowers=null;

              Page manapage=(Page) request.getSession().getAttribute("manapage");//第一次查询时保存的page对象

              if(this.getState().equals("forwerdpage")){//向前翻页,上一页

                     int forwerdpagenum;

                     if(this.getPagenum()==1){//已经是第一页,则显示第一页内容

                            forwerdpagenum=1;

                     }else{

                            forwerdpagenum=this.getPagenum()-1;

                            manapage.setNowpage(forwerdpagenum);

                     }

                     flowers=service.getAllflowers(forwerdpagenum-1);

              }else if(this.getState().equals("nextpage")){//向后翻页,下一页

                     int forwerdpagenum;

                     if(this.getPagenum()==manapage.getAllpage()){//已经是最后一页,则显示最后一页

                            forwerdpagenum=manapage.getAllpage();

                     }else{

                            forwerdpagenum=this.getPagenum()+1;

                            manapage.setNowpage(forwerdpagenum);

                     }

                     flowers=service.getAllflowers(forwerdpagenum-1);

              }else if(this.getState().equals("jumppage")){// 跳到第几页

                     int forwerdpagenum;

                     if(this.getPagenum()>=1&&this.getPagenum()<=manapage.getAllpage()){//在最大页和最小页之间

                            forwerdpagenum=this.getPagenum();

                            manapage.setNowpage(forwerdpagenum);

                     }else{//若用户输入的页数超过最大页,则显示第一页

                            forwerdpagenum=1;

                            manapage.setNowpage(1);

                     }

                     flowers=service.getAllflowers(forwerdpagenum-1);//查询方法

              }

              request.setAttribute("flowers",flowers);//保存一下

              return SUCCESS;

       }

 

}

注释已经写了,有读者不明白的地方,可以留言,我看到后会回复!

   以上代码是自己在项目中用过的,若使用,请注明出处!

你可能感兴趣的:(javaEE)