我是一个从C#转java的初学者,我在做SSH框架的时候遇到个分页查询的问题。具体算是数据库查询数据后存放在list return回页面,页面接收后是空白的· package com.wh.actions; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.wh.beans.Productlist; import com.wh.service.ProductService; import com.wh.util.Pager; @SuppressWarnings("serial") public class Product_listAction extends ActionSupport { private String Msg; private List resultList=new ArrayList();//返回结果集 public List getResultList() { return resultList; } public void setResultList(List resultList) { this.resultList = resultList; } public String getMsg() { return Msg; } public void setMsg(String msg) { Msg = msg; } private String product_id; private String product_name; private String product_type; private String Suppliername; private Integer currentPage=1; public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } List<Productlist> prolist = new ArrayList(); ; private ProductService proService; private Productlist pro; public String getProduct_id() { return product_id; } public void setProduct_id(String product_id) { this.product_id = product_id; } public String getProduct_name() { return product_name; } public void setProduct_name(String product_name) { this.product_name = product_name; } public String getProduct_type() { return product_type; } public void setProduct_type(String product_type) { this.product_type = product_type; } public String getSuppliername() { return Suppliername; } public void setSuppliername(String suppliername) { Suppliername = suppliername; } public List<Productlist> getProlist() { return prolist; } public void setProlist(List<Productlist> prolist) { this.prolist = prolist; } public ProductService getProService() { return proService; } public void setProService(ProductService proService) { this.proService = proService; } public Productlist getPro() { return pro; } public void setPro(Productlist pro) { this.pro = pro; } /** 分页 */ @SuppressWarnings("unchecked") public String PageList() throws Exception{ int totalSize = proService.GettotalSize(product_id, product_name, product_type, Suppliername); Pager pager = new Pager(currentPage,totalSize); prolist =proService.GetProduct_list2(product_id, product_name, product_type, Suppliername,currentPage, pager.getPageSize()); Map request = (Map)ActionContext.getContext().get("request"); //Map session = ActionContext.getContext().getSession(); request.put("pager",pager); return SUCCESS; } } dao: @SuppressWarnings("unchecked") public List<Productlist> GetProduct_list2(String product_id,String product_name,String product_type,String supplier,int currentPage,int pageSize){ String sql ="select ProductID as Product_id ,ProductName as Product_name,SupplierID as Suppliername, "; sql+=" SmallClass as Product_type,price as Product_pice,LargeClass as Reamk"; sql+=" from productmaster"; sql+=" where 1=1"; if(!(isInvalid(product_id))) { sql+=" and ProductID="""+product_id+""""; } if(!(isInvalid(product_name))) { sql+=" and ProductName="""+product_name+""""; } if(!(isInvalid(product_type))) { sql+=" and SmallClass="""+product_type+""""; } if(!(isInvalid(supplier))) { sql+=" and SupplierID="""+supplier+""""; } sql+=" order by ProductID"; Session session = getsession(); Query query=session.createSQLQuery(sql); int startRow = (currentPage - 1) * pageSize; query.setFirstResult(startRow); query.setMaxResults(pageSize); List<Productlist> list = query.list(); session.close(); return list; } bean: package com.wh.beans; /** * Book generated by MyEclipse - Hibernate Tools */ public class Productlist { private String Product_id; private String Product_name; private String Product_type; private String Suppliername; private double Product_pice; private String Reamk; public String getProduct_id() { return Product_id; } public void setProduct_id(String product_id) { Product_id = product_id; } public String getProduct_name() { return Product_name; } public void setProduct_name(String product_name) { Product_name = product_name; } public String getProduct_type() { return Product_type; } public void setProduct_type(String product_type) { Product_type = product_type; } public String getSuppliername() { return Suppliername; } public void setSuppliername(String suppliername) { Suppliername = suppliername; } public double getProduct_pice() { return Product_pice; } public void setProduct_pice(double product_pice) { Product_pice = product_pice; } public String getReamk() { return Reamk; } public void setReamk(String reamk) { Reamk = reamk; } } jsp页面: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <%@ page import="com.wh.beans.Productlist" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>商品查询</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link href="Style/Style.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> function btn_Submit(){ var myform = document.forms["form_pro"]; myform.action="roduct_List.action"; myform.method="post"; myform.submit(); } </script> </head> <body> <center> <table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <%@ include file="Top.jsp"%> </td> </tr> <tr> <td height="500" align="center" valign="top"> <table width="900" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="191" height="500" align="center" valign="top" background="Images/leftbg.jpg"> <%@ include file="Left.jsp"%> </td> <td width="709" align="center" valign="top" bgcolor="#F6F9FE"> <form name="form_pro"> <table width="709" border="1" cellspacing="0" cellpadding="0"> <tr> <td colspan="6" height="30" width="100%" background="Images/mainMenuBg.jpg" style="padding-left:25px;">商品查询</td> </tr> <tr> <td class="leftsize"> 商品编码: </td> <td class="rightsize" > <input type="text" name="product_id" value="${product_id}" /> </td> <td class="leftsize"> 商品名称: </td> <td class="rightsize"> <input type="text" name="product_name" value="${product_name}" /> </td> <td class="leftsize"> 商品分类: </td> <td class="rightsize"> <input type="text" name="product_type" value="${product_type}"/> </td> </tr> <tr> <td class="leftsize"> 供应商: </td> <td class="rightsize"> <input type="text" name="Suppliername" value="${Suppliername}"/> </td> <td colspan="4" align="right"> <input type="button" name="btn_Submint" value="查询" OnClick="btn_Submit()"> </td> </tr> </table> </form> <s:set name="pager" value="#request.pager"/> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr align="center" class="t1"> <td height="25" bgcolor="#D5E4F4"><strong>商品编号</strong></td> <td bgcolor="#D5E4F4"><strong>商品名称</strong></td> <td bgcolor="#D5E4F4"><strong>商品分类</strong></td> <td bgcolor="#D5E4F4"><strong>供应商名称</strong></td> <td bgcolor="#D5E4F4"><strong>商品价格</strong></td> <td bgcolor="#D5E4F4"><strong>备注</strong></td> </tr> <s:iterator value="prolist"> <tr> <td height="25" align="center"><s:property value="Product_id"/></td> <td><s:property value="Product_name"/></td> <td><s:property value="Product_type"/></td> <td><s:property value="Suppliername"/></td> <td><s:property value="Product_pice"/></td> <td><s:property value="Reamk"/></td> </tr> </s:iterator> </table> <table> <s:if test="#pager.hasFirst"> <a href="page_product.action?currentPage=1">首页</a> </s:if> <s:if test="#pager.hasPrevious"> <a href="page_product.action?currentPage=<s:property value="#pager.currentPage-1"/>">上一页</a> </s:if> <s:if test="#pager.hasNext"> <a href="page_product.action?currentPage=<s:property value="#pager.currentPage+1"/>">下一页</a> </s:if> <s:if test="#pager.hasLast"> <a href="page_product.action?currentPage=<s:property value="#pager.totalPage"/>">尾页</a> </s:if> <br> 当前第<s:property value="#pager.currentPage"/>页,总共<s:property value="#pager.totalPage"/>页 </table> </td> </tr> </table> </td> </tr> <tr> <td height="35" background="Images/bootBg.jpg"> </td> </tr> </table> </center> </body> </html> |
|
25分 |
你现在Action遍历打印一下prolist,看看你数据都拿到了没~~
还有把prolist定义成public的 感觉你封装的不太对,分页数据的List没有封装到Pager对象中去吗? |
楼上正解!
|
|
我试着遍历了下 出了个问题 for(int i = 0;i<prolist.size();i++){ System.out.print(prolist.get(i).getProduct_id()+","); System.out.print(prolist.get(i).getProduct_name()+","); System.out.print(prolist.get(i).getProduct_pice()+","); System.out.print(prolist.get(i).getProduct_type()+","); System.out.print(prolist.get(i).getSuppliername()+","); System.out.print(prolist.get(i).getReamk()+","); System.out.println("--------------------------------"); } [Ljava.lang.Object; cannot be cast to com.wh.beans.Productlist pager里面封装的是 当前页数,总页数,合上一页下一页这些信息 |
|
25分 |
prolist里面不是放的ProductList对象实例吗? 如果对象数组里面东西没问题,页面用 |
我研究出来了,谢谢你 对于我一个半道进java的帮助! List<Object[]> list = query.list(); session.close(); List<Productlist> prolist = new ArrayList<Productlist>(); for(int i =0;i<list.size();i++){ Object[] obs=list.get(i); Productlist po = new Productlist(); po.setProduct_id((String)obs[0]); po.setProduct_name((String)obs[1]); po.setSuppliername((String)obs[2]); po.setProduct_type((String)obs[3]); po.setProduct_pice(Double.parseDouble(obs[4].toString())); po.setReamk((String)obs[5]); prolist.add(po); } return prolist; |
|
我研究出来了,谢谢你 对于我一个半道进java的帮助! List<Object[]> list = query.list(); session.close(); List<Productlist> prolist = new ArrayList<Productlist>(); for(int i =0;i<list.size();i++){ Object[] obs=list.get(i); Productlist po = new Productlist(); po.setProduct_id((String)obs[0]); po.setProduct_name((String)obs[1]); po.setSuppliername((String)obs[2]); po.setProduct_type((String)obs[3]); po.setProduct_pice(Double.parseDouble(obs[4].toString())); po.setReamk((String)obs[5]); prolist.add(po); } return prolist; |