gpt4 book ai didi

java - Struts 数据库列表未显示在 JSP 页面中

转载 作者:行者123 更新时间:2023-11-30 06:10:34 25 4
gpt4 key购买 nike

我试图向 JSP 页面显示表值,但它没有显示。我正在尝试一周。

产品.java

这是产品的模型类别

   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.xml

<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>

获取产品列表.java

我正在从 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>

最佳答案

productlist execute() 中的范围错误。您在那里再次声明它,因此变量的范围就是该方法。你没有使用该字段。要解决此问题,请删除 execute() 中的声明方法,所以它只是说: productlist = new ArrayList<>();

此外,Struts 将寻找方法 getProductlist() 。您应该调整 Java Bean naming convention .

关于java - Struts 数据库列表未显示在 JSP 页面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50329914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com