【发布时间】:2016-03-07 01:38:47
【问题描述】:
我的 bean 在我的 JSP 页面中正确链接:
<jsp:useBean id = "productManager" scope = "session" class = "Smithd81.InventoryManager">
<jsp:getProperty name = "productManager" property = "productList" />
</jsp:useBean>
我可以验证它是否有效,因为以下行允许我获取它的长度:
Products in List = <%= productManager.getProductList().size() %>
这里的最终目标是遍历列表中的对象以显示它们的值,但为什么会出现空白?!
欢迎任何潜在的帮助,但是,对于那些会提到我的包名称有一个大写字母的人,我已经尝试对其进行重构并更改它两次,但当我这样做时,它会破坏整个项目。
在下面的块中,第一行显示列表中有多少项。然而,接下来的几行却一无所获。
<p>It is ${fn:length(productManager.productList)}</p>
<c:forEach var="p" items="${productManager.productList}">
<div>
<form action="inventory" method="POST">
<label>
<span>UPC</span>
<input type="text" name="upc" value="${p.getUpc()}" readonly="readonly"/>
</label>
<label>
<span>Short Details</span>
<input type="text" name="shortDetails" value="${p.getShortDetails()}" />
</label>
<label>
<span>Long Details</span>
<input type="text" name="longDetails" value="${p.getLongDetails()}" />
</label>
<label>
<span>Price</span>
<input type="text" name="price" value="${p.getPrice()}" />
</label>
<label>
<span>Stock</span>
<input type="text" name="stock" value="${p.getStock()}" />
</label>
<input type="submit" name="button" value="Edit" />
<input type="submit" name="button" value="Delete" />
<input type="submit" name="button" value="Create" />
</form>
</div>
</c:forEach>
这是完整的 JSP
<%--
Document : inventory
Created on : Mar 6, 2016, 3:27:11 PM
Author : Barad-Dur
--%>
<%@page import="Smithd81.Product"%>
<jsp:useBean id = "productManager" scope = "session" class = "Smithd81.InventoryManager">
<jsp:getProperty name = "productManager" property = "productList" />
</jsp:useBean>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="http://www.w3.org/StyleSheets/Core/Oldstyle" type="text/css" />
<title>JSP Page</title>
</head>
<body>
<h1>Inventory Manager JSP!</h1>
<hr />
Products in List = <%= productManager.getProductList().size() %>
<h2>Products:</h2>
<c:if test="${empty productManager.productList}">
<p> its empty </p>
</c:if>
<c:forEach var="p" items="${productManager.productList}">
<div>
<form action="inventory" method="POST">
<label>
<span>UPC</span>
<input type="text" name="upc" value="${p.getUpc()}" readonly="readonly"/>
</label>
<label>
<span>Short Details</span>
<input type="text" name="shortDetails" value="${p.getShortDetails()}" />
</label>
<label>
<span>Long Details</span>
<input type="text" name="longDetails" value="${p.getLongDetails()}" />
</label>
<label>
<span>Price</span>
<input type="text" name="price" value="${p.getPrice()}" />
</label>
<label>
<span>Stock</span>
<input type="text" name="stock" value="${p.getStock()}" />
</label>
<input type="submit" name="button" value="Edit" />
<input type="submit" name="button" value="Delete" />
</form>
</div>
</c:forEach>
</body>
</html>
根据要求,这里是 InventoryManager bean 的代码:
/*****************************************************
* Class InventoryManager
* @author: Daniel Smith
* @version 1.0.0
* Date: 2/20/2016
* This class provides the intermediary step for getting
* Products, lists of products, adding, deleting, or
* updating products in the list.
*****************************************************/
package Smithd81;
import java.util.ArrayList;
import java.util.List;
import edu.lcc.citp.utility.CollectionFileStorageUtility;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
/**
*
* @author Barad-Dur
*/
public class InventoryManager {
public InventoryManager(){
}
/**
* The getProduct method gets a list of products and loops over the list
* until it finds one with a getUpc() value that matches the string passed
* to it. Once found, the method returns the object to the calling method.
*
* In the event no objects are found, a null Product is returned.
*
* @param s UPC to search for.
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public Product getProduct(String s) throws IOException, ClassNotFoundException {
List<Product> list = getProductList();
//search list for upc (s)
Product match = null;
/*
Search each item in the list. This loop checks each item, comparing the 's'
parameter to each items returned UPC fom the getUpc() method. When it finds a match
it returns the Product object, or null if none is found.
*/
for (Product p : list) {
if (p.getUpc().equals(s)) {
return p;
}
}
return match; // returns the matching object, or null.
}
/**
* The getProductList method creates an empty List, then creates a
* Collection and fills it with the values from the
* CollectionFileStorageUtility.load method. Assuming no exceptions occur
* here, it loads the Collection values into the List, and returns it to the
* calling method.
*
* @return The product list, or null in the case of an exception.
* @throws IOException
* @throws ClassNotFoundException
*/
public List<Product> getProductList() throws IOException, ClassNotFoundException {
return new ArrayList<>(CollectionFileStorageUtility.load(Product.class));
}
/**
* The addProduct method gets a list of products from the getProductList()
* method. It loops over the list to make sure no Product already exists
* with the same UPC. If no matching UPC is found, the item is added to the
* list, and the list is saved using the CollectionFileStorageUtility.save
* method.
*
* @param p the Product object to add to the list.
* @throws IOException
* @throws ClassNotFoundException
*/
public void addProduct(Product p) throws IOException, ClassNotFoundException {
List<Product> list = getProductList();
boolean containsProductAlready = false;
for (Product listProduct : list) {
if (listProduct.getUpc().equals(p.getUpc())) {
containsProductAlready = true;
System.out.println("Sorry, an item already exists with that UPC.");
break; //exit the loop
}
}
if (!containsProductAlready) {
list.add(p); //adds the product if it wasnt found in the list.
Collections.sort(list);
CollectionFileStorageUtility.save(list, Product.class); //saves updated list.
}
}
/**
* The updateProduct method creates a list of Products using the
* getProductList() method. It then iterates over them to find an object
* with a matching UPC. When a match is found, it changes updates the traits
* wherever they were different than the stored traits, except for when they
* were blank. CollectionFileStorageUtility.save is then called to save the
* updated list.
*
* This does not allow a user to change a UPC.
*
* @param p The product containing updated values.
* @throws IOException
* @throws ClassNotFoundException
*/
public void updateProduct(Product p) throws IOException, ClassNotFoundException {
List<Product> list = getProductList();
boolean containsProductAlready = false;
for (Product listProduct : list) {
if ((listProduct.getUpc().equals(p.getUpc()))) {
containsProductAlready = true;
//set new product info here.
//if new object Long Details is not blank, update it. on the
// current listProduct element.
if (!(p.getLongDetails().equals(""))) {
listProduct.setLongDetails(p.getLongDetails());
System.out.println(p.getLongDetails());
}
//if new object Short Details is not blank, update it. on the
// current listProduct element.
if (!(p.getShortDetails().equals(""))) {
listProduct.setShortDetails(p.getShortDetails());
}
if (!(p.getPrice() == null)) {
listProduct.setPrice(p.getPrice());
}
if (!(p.getUpc().equals(""))) {
listProduct.setUpc(p.getUpc());
}
if (!(p.getStock() == null)) {
listProduct.setStock(p.getStock());
}
Collections.sort(list);
CollectionFileStorageUtility.save(list, Product.class); //saves updated list.
break;
}
}
if (containsProductAlready = false) {
System.out.println("Sorry, no product was found with that UPC.");
}
}
/**
* The removeProduct method creates a list of Products using the
* getProductList method. It then searches the list looking for a Product
* with a UPC matching the one which was passed in. If found, it removes the
* item from the list and saves the updated list using the
* CollectionFileStorageUtility.save method.
*
* @param upc The upc to search for.
* @throws IOException
* @throws ClassNotFoundException
*/
public void removeProduct(String upc) throws IOException, ClassNotFoundException {
List<Product> list = getProductList();
boolean containsProductAlready = false;
for (Product listProduct : list) {
if ((listProduct.getUpc().equals(upc))) {
containsProductAlready = true;
list.remove(listProduct);//removes the matching object
Collections.sort(list);
CollectionFileStorageUtility.save(list, Product.class); //saves updated list.
System.out.println("Product was removed successfully.");
break;
}
}
if (!containsProductAlready) {
System.out.println("Sorry, no product was found with that UPC.");
}
}
}
产品类,每个请求:
/*****************************************************
* Class Product
* @author: Daniel Smith
* @version 1.0.0
* Date: 2/20/2016
* This product contains the class template to create
* products to be stored. It holds thier UPC, long and
* short descriptions, price and amount in stock.
* The class provides mutators and accessors to modify
* existing Products.
******************************************************/
package Smithd81;
import java.io.Serializable;
import java.math.BigDecimal;
/**
*
* @author Barad-Dur
*/
public class Product implements Comparable<Product>, Serializable {
private String upc;
private String shortDetails;
private String longDetails;
private BigDecimal price;
private Integer stock;
/**
* @return the upc
*/
public String getUpc() {
return upc;
}
/**
* @param upc the upc to set
*/
public void setUpc(String upc) {
this.upc = upc;
}
/**
* @return the shortDetails
*/
public String getShortDetails() {
return shortDetails;
}
/**
* @param shortDetails the shortDetails to set
*/
public void setShortDetails(String shortDetails) {
this.shortDetails = shortDetails;
}
/**
* @return the longDetails
*/
public String getLongDetails() {
return longDetails;
}
/**
* @param longDetails the longDetails to set
*/
public void setLongDetails(String longDetails) {
this.longDetails = longDetails;
}
/**
* @return the price
*/
public BigDecimal getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(BigDecimal price) {
this.price = price;
}
/**
* @return the stock
*/
public Integer getStock() {
return stock;
}
/**
* @param stock the stock to set
*/
public void setStock(Integer stock) {
this.stock = stock;
}
@Override
public int compareTo(Product p) {
int comparison = this.getUpc().compareTo(p.getUpc());
return comparison;
}
}
【问题讨论】:
-
我编辑了您的问题以删除粘贴箱链接,并将代码放在问题中。为了将来保留问题,StackOverflow 不鼓励使用外部链接到代码。仅供参考:)
-
你的bean中的字段是upc、shortDetails等等吗?如果是,那么您可以尝试使用 ${p.upc} 而不是使用 ${p.getUpc()} 我刚刚编辑了它。
-
不,这很可能是问题所在。 items="${productManager.productList} 行返回一个 List 对象,其中充满了序列化的 Product 对象。字段 upc、price 等属于 Product 类。我是否还需要在 bean 中使用它?如何那行得通吗?
-
@ug_ 谢谢你的提示。
-
我假设 p 是一个 Product 对象并且 ${productManager.productList} 返回一个 Product 对象列表。对吗?