jstl对象没有从jsp获得paramaters

时间:2016-03-07 01:38:48

标签: jsp jstl

我在我的JSP页面中正确链接了我的bean:

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

Heres是完整的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;
    }

}

1 个答案:

答案 0 :(得分:0)

这是我的测试代码。

package test;
public class Product {
    private String upc;
    public String getUpc() {
        return upc;
    }
    public void setUpc(String upc) {
        this.upc = upc;
    }
}

package test;
import java.util.ArrayList;
import java.util.List;
public class InventoryManager {
    public List<Product> getProductList(){
        ArrayList productList = new ArrayList();
        Product p1 = new Product();
        p1.setUpc("one");
        productList.add(p1);
        Product p2 = new Product();
        p2.setUpc("two");
        productList.add(p2);
        Product p3 = new Product();
        p3.setUpc("three");
        productList.add(p3);
        return productList;
    }
}

和我的测试JSP

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id = "productManager" scope = "session"  class = "test.InventoryManager" />
testing   
<c:forEach var="p" items="${productManager.productList}">
    ${p.upc}
</c:forEach>

我的测试工作正常并打印

测试一两三