我正在尝试向JSP页面显示表值,但它没有显示。我试了一个星期。
这是产品的模型类
package com.practice.models;
public class Product {
private int id;
private String name;
private int qty;
private double price;
public Product(){}
public Product(int proId,String productName,int proQty,double proPrice){
this.id = proId;
this.name = productName;
this.qty = proQty;
this.price = proPrice;
}
public String getProductname() {
return name;
}
public void setProductname(String productname) {
this.name = productname;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
<struts>
<package name="default" extends="struts-default">
<action name="checkonce" class="com.practice.controllers.GetProductList">
<result name="success">products.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
我从DBtool类中获取数据库的值,并且我获得了所有值,但是JSP文件中没有显示值
public class GetProductList extends ActionSupport {
private static final long serialVersionUID = 1L;
private List<Product> productlist;
public List<Product> getproductlist(){
return productlist;
}
public void setproductlist(List<Product> productlist) {
this.productlist = productlist;
}
public String execute() throws IOException{
List<Product> productlist = new ArrayList<Product>();
try{
DBtool db = new DBtool();
System.out.println(1);
java.sql.ResultSet rs = db.getlist();
while(rs.next()){
int id = rs.getInt("id");
String proname = rs.getString("name");
int qty = rs.getInt("qty");
double price = rs.getDouble("price");
Product pro = new Product(id,proname,qty,price);
productlist.add(pro);
System.out.println(rs.getString("name"));
}
return SUCCESS;
}
catch(Exception ex){
System.out.println("Error : "+ex.getMessage());
return ERROR;
}
}
}
这是products.jsp
<%@ taglib uri="/struts-tags" prefix="s" %>
<table>
<thead>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Product Quantity</th>
<th>Product Price</th>
</tr>
</thead>
<tbody>
<s:iterator value="productlist" status="productlistStatus">
<tr>
<td><s:property value="id"/></td>
<td><s:property value="name"/></td>
<td><s:property value="qty"/></td>
<td><s:property value="price"/></td>
</tr>
</s:iterator>
</tbody>
</table>
答案 0 :(得分:1)
productlist
在execute()
中的范围错误。您再次声明它,因此变量的范围是该方法。你没有使用这个领域。要解决此问题,请删除execute()
方法中的声明,因此它只是说:
productlist = new ArrayList<>();
此外,Struts将寻找方法getProductlist()
。您应该调整Java Bean naming convention。