Struts数据库列表无法在JSP页面显示问题求助
Hey, let's break down the issues keeping your product data from showing up in the JSP—you're so close, just a few JavaBean convention and variable scope fixes needed:
1. Local Variable Shadowing in Your Action Class
In your GetProductList.execute() method, you’re creating a local productlist variable that "shadows" the class-level member variable. This means all the data you load from the database gets stored in the local variable, not the one the Struts2 framework will access via your getter method.
Fix it by removing the local variable declaration and using the class-level variable directly:
// Replace this local variable declaration: // List<Product> productlist = new ArrayList<Product>(); // With this (uses the class-level productlist): productlist = new ArrayList<Product>();
2. JavaBean Getter Naming Mismatches
Struts2 relies strictly on JavaBean conventions to map action properties to your JSP. Your getter methods need to follow the getXxx() pattern (where Xxx starts with a capital letter) to match the property names you’re using in the JSP.
In the Product class:
You’re using <s:property value="name"/> in the JSP, but your getter is named getProductname(). Struts2 looks for getName() to map to the name property. Update the getter/setter:
// Replace these: // public String getProductname() { return name; } // public void setProductname(String productname) { this.name = productname; } // With these: public String getName() { return name; } public void setName(String name) { this.name = name; }
In the GetProductList action class:
Your getter for the product list is getproductlist(), which doesn’t follow camel-case conventions. Struts2 expects the getter to start with get followed by a capitalized property name. Fix the getter/setter:
// Replace these: // public List<Product> getproductlist(){ return productlist; } // public void setproductlist(List<Product> productlist) { this.productlist = productlist; } // With camel-case compliant methods: public List<Product> getProductlist() { return productlist; } public void setProductlist(List<Product> productlist) { this.productlist = productlist; }
(Pro tip: For better code readability, rename the class-level variable to productList (proper camel case), then update the getter/setter to getProductList()/setProductList()—just remember to change the JSP iterator value to productList too.)
3. Quick Sanity Check: JSP Location
Double-check that products.jsp is in the correct location relative to your web root. If it’s in a subfolder (like /WEB-INF/jsp/), update the result path in struts.xml to match (e.g., <result name="success">/WEB-INF/jsp/products.jsp</result>).
Modified Working Code Snippets
Updated GetProductList.java:
public class GetProductList extends ActionSupport { private static final long serialVersionUID = 1L; private List<Product> productlist; // Fixed camel-case getter/setter public List<Product> getProductlist() { return productlist; } public void setProductlist(List<Product> productlist) { this.productlist = productlist; } public String execute() throws IOException{ // Use class-level variable instead of local 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; } } }
Updated Product.java name getter/setter:
public String getName() { return name; } public void setName(String name) { this.name = name; }
Once you make these changes, Struts2 will correctly access your product list from the action and iterate over it in the JSP, displaying all your product details as expected.
内容的提问来源于stack exchange,提问作者Naveen Roy

