java struts2分页实例

几个步骤:
1.新建一个封装分页的类Page;
package com.ata.egroup.util;

public class Page {
//记录号 1
private int pageno=1;
//每页8条
private int pagesize=8;
public void setPageno(int pageno) {
this.pageno = pageno;
}
public int getPageno() {
return pageno;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public int getPagesize() {
return pagesize;
}


}

2.
// 订单表的查询;分页;将pageno,pagesize;作为参数传递;

public ArrayList<OrderInfo> showOrder(int userid,int pageno,int pagesize){

ArrayList<OrderInfo> listOrder = new ArrayList<OrderInfo>();
Connection conn = ManagerConnection.getConn();
String sql ="select "
+"o.order_id oid,o.user_id ouserid,o.order_time otime,o.order_status ostatus ,"
+"d.order_id did,d.food_id dfid ,d.food_count dcount ,d.total_price dtprice,d.detail_id ddtailid,"
+ "f.food_id fid ,f.food_name fname,f.food_price fprice "
+ "from order_info o ,order_detail d,food_info f "
+" where o.order_id = d.order_id and d.food_id = f.food_id and o.user_id='"+userid +"' order by order_time desc";
ResultSet rs = ManagerConnection.getRs(sql);

try {
rs.next();

pagetotal = rs.getInt(1);

pagesum = pagetotal % pagesize == 0 ? pagetotal / pagesize
: pagetotal / pagesize + 1;

if(pageno>0&&pagesize>0){

String sql2 = "select * from order_info limit "
+ (pageno * pagesize - pagesize) + "," + pagesize;
ResultSet rs2 = ManagerConnection.getRs(sql2);

while(rs2.next()){

OrderInfo orderinfo = new OrderInfo();
orderinfo.setOrder_id(rs.getInt("oid"));
orderinfo.setOrder_status(rs.getString("ostatus"));
orderinfo.setOrder_time(rs.getDate("otime"));
orderinfo.setUser_id(rs.getInt("ouserid"));



OrderDetail ordertail = new OrderDetail();

ordertail.setDetail_id(rs.getInt("did"));
ordertail.setFood_count(rs.getInt("dcount"));
ordertail.setTotal_price(rs.getFloat("dtprice"));
ordertail.setFood_id(rs.getInt("fid"));
ordertail.setOrder_id(rs.getInt("did"));



FoodInfo foodinfo = new FoodInfo();
foodinfo.setFood_id(rs.getInt("fid"));
foodinfo.setFood_name(rs.getString("fname"));
foodinfo.setFood_price(rs.getFloat("fprice"));

ordertail.setFoodinfo(foodinfo);

orderinfo.setDetail(ordertail);

listOrder.add(orderinfo);

}


}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("***");
return listOrder;
}

//Action中的处理

3.

import com.ata.egroup.util.Page;
import com.opensymphony.xwork2.ActionSupport;

public class FrontOrderAction extends ActionSupport {


private OrderInfo info;
private OrderDetail detail;
private OrderService service =new OrderService() ;
//必须
private Page pa = new Page();


private FoodInfo food;
private ArrayList<OrderInfo> listinfo;

public ArrayList<OrderInfo> getListinfo() {
return listinfo;
}

public void setListinfo(ArrayList<OrderInfo> listinfo) {
this.listinfo = listinfo;
}



public String view() {

UserInfo user = (UserInfo) ActionContext.getContext().getSession().get("user");

if( null == user){
return "login";
}




//
listinfo = service.showOrder(user.getUser_id(),pa.getPageno(),pa.getPagesize());


//返回显示视图
return "view";

}

public UserInfo getUser() {
return user;
}

public void setUser(UserInfo user) {
this.user = user;
}

public FoodInfo getFood() {
return food;
}

public void setFood(FoodInfo food) {
this.food = food;
}

public OrderInfo getInfo() {
return info;
}

public void setInfo(OrderInfo info) {
this.info = info;
}

public OrderDetail getDetail() {
return detail;
}

public void setDetail(OrderDetail detail) {
this.detail = detail;
}




public Page getPa() {
return pa;
}

public void setPa(Page pa) {
this.pa = pa;
}

public OrderService getService() {
return service;
}

public void setService(OrderService service) {
this.service = service;
}


}
//4 jsp 页面 调用 STRUTS2分页标签;
<tr>


<td height="25" colspan="10">

<div align="center">

<s:url id="url_first" value="order_view">

<s:param name="pa.pageno" value="1"></s:param>

</s:url>


<s:url id="url_pre" value="order_view">

<s:param name="pa.pageno" value="pa.pageno-1"></s:param>

</s:url>


<s:url id="url_next" value="order_view">

<s:param name="pa.pageno" value="pa.pageno+1"></s:param>

</s:url>

//提交给 显示的action这里为;FrontOrderAction( FrontOrderAction)
<s:url id="url_last" value="order_view">

<s:param name="pa.pageno" value="page.pagesum"></s:param>

</s:url>


<s:if test=" pa.pageno<page.pagesum && pa.pageno>1" >

<s:a href="%{url_first}">首页</s:a>

<s:a href="%{url_pre}">上一页</s:a>

<s:a href="%{url_next}">下一页</s:a>

<s:a href="%{url_last}">末页</s:a>

</s:if>

<s:elseif test="pa.pageno>1">

<s:a href="%{url_first}">首页</s:a>

<s:a href="%{url_pre}">上一页</s:a>

<s:a href="%{url_last}">末页</s:a>

</s:elseif>

<s:else>

<s:a href="%{url_first}">首页</s:a>

<s:a href="%{url_next}">下一页</s:a>

<s:a href="%{url_last}">末页</s:a>

</s:else>

</div>

</td>

</tr>

5.struts.xml;

<package name="order_manage" extends="struts-default">
<action name ="order_*" class ="com.ata.egroup.act.FrontOrderAction" method="{1}">
<result>success.jsp</result>
<result name="view">order_view.jsp</result>

</action>
</package>

可以简单完成分页显示;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值