ShowProductAction.java
上传用户:zhc3n3
上传日期:2022-07-30
资源大小:2750k
文件大小:2k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
JavaScript
- package com.t11.web.action;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.t11.biz.ProductService;
- import com.t11.entity.Product;
- import com.t11.web.Action;
- import com.t11.web.ActionForward;
- import com.t11.web.ActionMapping;
- public class ShowProductAction implements Action {
- ProductService product = new ProductService();
- public ActionForward execute(ActionMapping actionMapping,
- HttpServletRequest request, HttpServletResponse response) {
- ActionForward action = null;
- List<Product> list = product.showProduct();
- //记录在数据库读取出来总条数
- int records = list.size();
- //每页显示条数
- int pagesize = 5;
- //以有的总页数
- int pages = records%pagesize==0?records/pagesize:records%pagesize+1;
- //显示在当前页面的信息
- int indexpages = 1;
- if(request.getParameter("pg")!=null){
- indexpages = Integer.parseInt(request.getParameter("pg"));
- }
- //如果当前小于1或者大于最大页返回到第一页或者最后一页
- if(indexpages<1){
- indexpages=1;
- }else if(indexpages>pages){
- indexpages=pages;
- }
- //取出当前页的集合
- List<Product> curProducts=new ArrayList<Product>();
- int start =(indexpages-1)*pagesize;
- //读取出来的总条数比索引出来的少时!把records的数赋给end
- int end = indexpages*pagesize>=records?records:indexpages*pagesize;
- for(int i=start;i<end;i++){
- curProducts.add(list.get(i));
- }
- if (list != null) {
- //把商品别表传到对应的JSP中。。
- request.setAttribute("PRODUCT", curProducts);
- //把总页面传到对应的JSP中。。
- request.setAttribute("pages", pages);
- request.setAttribute("indexpages", indexpages);
- action = actionMapping.getForward().get("showProductList");
- } else {
- action = actionMapping.getForward().get("failed");
- }
- return action;
- }
- }