jstl object not getting paramaters from jsp -
i have bean linked correctly in jsp page:
<jsp:usebean id = "productmanager" scope = "session" class = "smithd81.inventorymanager"> <jsp:getproperty name = "productmanager" property = "productlist" /> </jsp:usebean> i can verify works, because following line allows me length:
products in list = <%= productmanager.getproductlist().size() %> the end goal here iterate on objects in list display values, why coming blank?!
any potential welcome, however, of mention package name has capital letter, i've attempted refactor , change twice now, , when breaks whole project.
in following block, first line shows how many items in list. next lines, however, nothing.
<p>it ${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 full 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> 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> by request, here code inventorymanager bean:
/***************************************************** * class inventorymanager * @author: daniel smith * @version 1.0.0 * date: 2/20/2016 * class provides intermediary step getting * products, lists of products, adding, deleting, or * updating products in 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(){ } /** * getproduct method gets list of products , loops on list * until finds 1 getupc() value matches string passed * it. once found, method returns object calling method. * * in event no objects found, null product returned. * * @param s upc search for. * @return * @throws ioexception * @throws classnotfoundexception */ public product getproduct(string s) throws ioexception, classnotfoundexception { list<product> list = getproductlist(); //search list upc (s) product match = null; /* search each item in list. loop checks each item, comparing 's' parameter each items returned upc fom getupc() method. when finds match returns product object, or null if none found. */ (product p : list) { if (p.getupc().equals(s)) { return p; } } return match; // returns matching object, or null. } /** * getproductlist method creates empty list, creates * collection , fills values * collectionfilestorageutility.load method. assuming no exceptions occur * here, loads collection values list, , returns * calling method. * * @return product list, or null in case of exception. * @throws ioexception * @throws classnotfoundexception */ public list<product> getproductlist() throws ioexception, classnotfoundexception { return new arraylist<>(collectionfilestorageutility.load(product.class)); } /** * addproduct method gets list of products getproductlist() * method. loops on list make sure no product exists * same upc. if no matching upc found, item added * list, , list saved using collectionfilestorageutility.save * method. * * @param p product object add list. * @throws ioexception * @throws classnotfoundexception */ public void addproduct(product p) throws ioexception, classnotfoundexception { list<product> list = getproductlist(); boolean containsproductalready = false; (product listproduct : list) { if (listproduct.getupc().equals(p.getupc())) { containsproductalready = true; system.out.println("sorry, item exists upc."); break; //exit loop } } if (!containsproductalready) { list.add(p); //adds product if wasnt found in list. collections.sort(list); collectionfilestorageutility.save(list, product.class); //saves updated list. } } /** * updateproduct method creates list of products using * getproductlist() method. iterates on them find object * matching upc. when match found, changes updates traits * wherever different stored traits, except when * blank. collectionfilestorageutility.save called save * updated list. * * not allow user change upc. * * @param p product containing updated values. * @throws ioexception * @throws classnotfoundexception */ public void updateproduct(product p) throws ioexception, classnotfoundexception { list<product> list = getproductlist(); boolean containsproductalready = false; (product listproduct : list) { if ((listproduct.getupc().equals(p.getupc()))) { containsproductalready = true; //set new product info here. //if new object long details not blank, update it. on // current listproduct element. if (!(p.getlongdetails().equals(""))) { listproduct.setlongdetails(p.getlongdetails()); system.out.println(p.getlongdetails()); } //if new object short details not blank, update it. on // 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 found upc."); } } /** * removeproduct method creates list of products using * getproductlist method. searches list looking product * upc matching 1 passed in. if found, removes * item list , saves updated list using * collectionfilestorageutility.save method. * * @param upc upc search for. * @throws ioexception * @throws classnotfoundexception */ public void removeproduct(string upc) throws ioexception, classnotfoundexception { list<product> list = getproductlist(); boolean containsproductalready = false; (product listproduct : list) { if ((listproduct.getupc().equals(upc))) { containsproductalready = true; list.remove(listproduct);//removes matching object collections.sort(list); collectionfilestorageutility.save(list, product.class); //saves updated list. system.out.println("product removed successfully."); break; } } if (!containsproductalready) { system.out.println("sorry, no product found upc."); } } } the product class, per request:
/***************************************************** * class product * @author: daniel smith * @version 1.0.0 * date: 2/20/2016 * product contains class template create * products stored. holds thier upc, long , * short descriptions, price , amount in stock. * class provides mutators , accessors 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 upc */ public string getupc() { return upc; } /** * @param upc upc set */ public void setupc(string upc) { this.upc = upc; } /** * @return shortdetails */ public string getshortdetails() { return shortdetails; } /** * @param shortdetails shortdetails set */ public void setshortdetails(string shortdetails) { this.shortdetails = shortdetails; } /** * @return longdetails */ public string getlongdetails() { return longdetails; } /** * @param longdetails longdetails set */ public void setlongdetails(string longdetails) { this.longdetails = longdetails; } /** * @return price */ public bigdecimal getprice() { return price; } /** * @param price price set */ public void setprice(bigdecimal price) { this.price = price; } /** * @return stock */ public integer getstock() { return stock; } /** * @param stock stock set */ public void setstock(integer stock) { this.stock = stock; } @override public int compareto(product p) { int comparison = this.getupc().compareto(p.getupc()); return comparison; } }
here test code.
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; } } and test 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> my test works fine , prints
testing 1 2 3
Comments
Post a Comment