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

Struts database list not showing in JSP page

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

product.java

这是产品

的型号class
   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> 

GetProductList.java

我正在从 DBtool class 的数据库中获取值,并且我已成功获取所有值,但值未显示在 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>

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

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