Hello, I have a project using JSP and JSTL. I have to forward one JSP to another. The problem that I am having is understanding what JSTL tags to loop through and get the information from the java class by using the
<c:when>, <c:choose> tags. I have index.jsp, plants.jsp and a Customers.java file. The customer has to what type of plant they want, enter a quanity, and put in their id number. I have 2 errorr messages, one if that they need to order over a quarnity of 24 and one is not a valid id. When I get to retrieving the customer id I don't know how to use the correct tags to get the customer Id. It just keeps giving me the error message. Can someone tell me how I would write the the tags to get the customer id and list the name of the customer on the output page.
Here is my code for index.jsp
Here is my code for plants.jsp
Here is my customers.java
Here is my showpurchase.js p
<c:when>, <c:choose> tags. I have index.jsp, plants.jsp and a Customers.java file. The customer has to what type of plant they want, enter a quanity, and put in their id number. I have 2 errorr messages, one if that they need to order over a quarnity of 24 and one is not a valid id. When I get to retrieving the customer id I don't know how to use the correct tags to get the customer Id. It just keeps giving me the error message. Can someone tell me how I would write the the tags to get the customer id and list the name of the customer on the output page.
Here is my code for index.jsp
Code:
%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="pl" uri = "/jsp/plants.tld" %>
<c:choose>
<c:when test="${empty param.action}">
<jsp:forward page="plants.jsp"/>
</c:when>
<c:when test="${param.action eq 'purchased'}">
<jsp:useBean id="mistake" class="plants.Mistake" scope="request">
<jsp:setProperty name="mistake" property="msg" value="Sorry, you must buy at least two dozen. Please enter again."/>
</jsp:useBean>
<c:if test="${(param.number <= 24) }">
<jsp:forward page="plants.jsp"/>
</c:if>
<c:set var= "present" value = "${param.custId}" scope="application" />
${pl:unloadCust()}
${pl:loadCust()}
${pl:checkCust(present)}
<c:if test="${!pl:checkCust(present)}">
<jsp:setProperty name="mistake" property="msg" value="Sorry, invalid ID. Please enter again."/>
<jsp:forward page="plants.jsp"/>
</c:if>
<jsp:useBean id="purchase" class="plants.Purchase" scope="request">
<jsp:setProperty name="purchase" property="*"/>
</jsp:useBean>
<jsp:forward page="showPurchase.jsp"/>
</c:when>
</c:choose>
Here is my code for plants.jsp
Code:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<link rel=stylesheet type="text/css" href="sales.css">
<title>Plant Sales</title></head>
<body>
<table class="mainBox" width="386">
<tr>
<c:if test="${!(empty mistake)}">
<tr>
<td class="errorText" colspan="2">
${mistake.msg}
</td>
</tr>
</c:if>
<td width="352" colspan="2" class="boxTitle" align="center"><h1>Plant Sales</h1></td>
</tr>
<tr><td>
<form action="index.jsp" method="get">
<table style="height: 215px; width: 405px">
<tr>
<td width="145" style="width: 177px">Plants to purchase :<td width="175">
<select name="item">
<option>Hosta</option>
<option>Orchid</option>
<option>Alyssum</option>
</select>
<input type="hidden" name="action" value="purchased"/>
</td>
</tr>
<tr>
<td>Number desired:</td>
<td><label>
<input type="text" name="number" id="number">
</label>
</td></tr>
<tr>
<td>Customer Id :</td>
<td><label>
<c:forEach var="present" items="${param.custId}" >
<c:choose>
<c:when test="${pl:checkCust(present)}">
</c:choose>
<c:otherwise>
<c:if test="${!pl:checkCust(present)}">
</c:if>
</c:otherwise>
</c:choose>
</c:when>
</c:forEach>
<input type="text" name="action" id="${param.custId}">
</label>
</td></tr>
<tr style="height: 51px"><td colspan="2" align="center">
<input type="submit" value="OK"/>
</td></tr>
</table>
</form>
</td></tr></table>
</body>
</html>
Code:
package plants;
import java.util.*;
public class Customers
{
private static ArrayList cust = new ArrayList();
// write an empty constructor
public Customers()
{
}
public static void loadCust()
{
cust.add(new Customer(123, "Donald", "Duck"));
cust.add (new Customer(124, "Mickey", "Mouse"));
cust.add(new Customer (125, "Minnie", "Mouse"));
}
public static void unloadCust()
{
cust.clear();
// write code to clear out all the customers from the ArrayList
}
public static boolean checkCust(int id)
{
boolean found = false;
int i=0;
// note a while loop since we want to stop once we find them
// each one should be present only once!
while (!found && i<cust.size()) // two ways to end - we find it or we get to the end
{
Customer c = (Customer) cust.get(i);
int Num = c.getCustId();
if(Num==id) // found it!!
{
found = true; // set found to true to end the loop
return true; // print it out
}
else
i++; // look at the next one
}
if (!found) // check to see if it ever was found
return found;
return false;
}
// write code to look for a Customer with the id parameter
// return true or false
public static String getCust(int id)
{
boolean found = false;
int i=0;
// note a while loop since we want to stop once we find them
// each one should be present only once!
while (!found && i<cust.size()) // two ways to end - we find it or we get to the end
{
Customer c = (Customer) cust.get(i);
int Num = c.getCustId();
if(Num==id) // found it!!
{
found = true ; // set found to true to end the loop
c.getName() ; // print it out
}
else
i++; // look at the next one
}
if (!found);
return "Nobody";
// check to see if it ever was found
}
public static String print()
{
return print();
}
}
Code:
<html>
<head>
<link rel=stylesheet type="text/css" href="sales.css">
<title>Plant Purchased</title></head>
<body>
<table class="mainBox" width="600">
<tr><td class="boxTitle" colspan="2">
Your Order
</td></tr>
<tr><td colspan="2"> </td></tr>
<tr><td class="tableLabel" width="30%">
Items Purchased
</td>
<td class="tableCell">
${purchase.item}</td>
</tr>
<tr><td class="tableLabel">
Item Number
</td>
<td class="tableCell">
${purchase.number}</td>
</tr>
<tr>
<td>Customer Id :</td>
<td><input type="text" name="Id" id="Id"></td>
</tr>
</table>
</body>
</html>
Comment