JSTL Working With More Than One Bean : Java Beans « JSTL « Java Tutorial






Bid.java

package beans;
public class Bid {
    
    /** Holds value of property price. */
    private long price;
    
    /** Holds value of property item. */
    private String item;
    
    /** Creates a new instance of Bid */
    public Bid() {
    }
    
    /** Getter for property price.
     * @return Value of property price.
     *
     */
    public long getPrice() {
        return this.price;
    }
    
    /** Setter for property price.
     * @param price New value of property price.
     *
     */
    public void setPrice(long price) {
        this.price = price;
    }
    
    /** Getter for property item.
     * @return Value of property item.
     *
     */
    public String getItem() {
        return this.item;
    }
    
    /** Setter for property item.
     * @param item New value of property item.
     *
     */
    public void setItem(String item) {
        this.item = item;
    }
    
}

Bidder.java

package beans;

public class Bidder {
    
    /** Holds value of property item. */
    private String item;
    
    /** Holds value of property price. */
    private long price;
    
    /** Holds value of property result. */
    private String result;
    
    /** Creates a new instance of Bidder */
    public Bidder() {
    }
    
    /** Getter for property item.
     * @return Value of property item.
     *
     */
    public String getItem() {
        return this.item;
    }
    
    /** Setter for property item.
     * @param item New value of property item.
     *
     */
    public void setItem(String item) {
        this.item = item;
    }
    
    /** Getter for property price.
     * @return Value of property price.
     *
     */
    public long getPrice() {
        return this.price;
    }
    
    /** Setter for property price.
     * @param price New value of property price.
     *
     */
    public void setPrice(long price) {
        this.price = price;
    }
    
    /** Getter for property result.
     * @return Value of property result.
     *
     */
    public String getResult() {
        /* simulate bid result */
        this.result = "Sorry your bid did not win. The successful bidder was joe1233.";
        return this.result;
    }
    
}

BidError.java

package beans;

public class BidError {
    
    /** Holds value of property msg. */
    private String msg;
    
    /** Creates a new instance of BidError */
    public BidError() {
    }
    
    /** Getter for property msg.
     * @return Value of property msg.
     *
     */
    public String getMsg() {
        return this.msg;
    }
    
    /** Setter for property msg.
     * @param msg New value of property msg.
     *
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }
    
}
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:choose>
  <c:when test="${empty param.action}">
     <jsp:forward page="enterbid.jsp"/>
  </c:when>
  <c:when test="${param.action eq 'bid'}">
   <!--  validation code -->
   <c:if test="${(param.price <= 0) ||  (param.price >= 999)}">
       <jsp:useBean id="biderror" class="beans.BidError" scope="request">
        <jsp:setProperty name="biderror" property="msg" value="Sorry, your bid is not in range. Please enter again."/>
     </jsp:useBean>
     <jsp:forward page="index.jsp"/>
  </c:if>
  <!-- data validated -->
       <jsp:useBean id="bidinfo" class="beans.Bid" scope="request">
        <jsp:setProperty name="bidinfo" property="*"/> 
     </jsp:useBean>

  <!-- perform bidding -->
      <jsp:useBean id="bidder" class="beans.Bidder" scope="request">
        <jsp:setProperty name="bidder" property="item"/>
        <jsp:setProperty name="bidder" property="price"/>
     </jsp:useBean>
     <c:set var="bidresult" value="${bidder.result}" scope="request"/>


     <jsp:forward page="showbid.jsp"/>   
</c:when>
</c:choose>
<html>
<head>
<title>Show the Bid</title></head>
<body>
<table class="mainBox" width="600">
<tr><td class="boxTitle" colspan="2">
Your Auction Bid
</td></tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr><td class="tableLabel" width="30%">
  Item bid for
</td>
<td class="tableCell">
<c:out value="${bidinfo.item}"></td>
</tr>
<tr><td class="tableLabel">
  Bid price
</td>
<td class="tableCell">
<c:out value="${bidinfo.price}"/></td>
</tr>
<tr><td class="tableLabel">
  Bid result
</td>
<td class="resultCell">
<c:out value="${bidresult}"/></td>
</tr>


</table>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
<title>Enter Your Bid</title></head>
<body>
<table class="mainBox" width="400">
<tr><td class="boxTitle" colspan="2">
Wrox JSP Auction
</td></tr>
<c:if test="${!(empty biderror)}">
  <tr>
    <td class="errorText" colspan="2">
       ${biderror.msg}
    </td>
  </tr>
</c:if>

<tr><td colspan="2">&nbsp;</td></tr>
<tr><td>
<form  action="action.jsp" method="get">
<table>
<tr>
<td width="200">Item to bid on</td><td>
<select name="item">
<option>27 inch TV</option>
<option>DVD Player</option>
<option>Digital Camera</option>
</select>
<input type="hidden" name="action" value="bid"/>
</td></tr>
<tr>
<td>Bid Price:</td>
<td><input name="price" type="text" width="10"/>
</td></tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr><td colspan="2" align="center">
<input type="submit" value="Bid now!"/>
</td></tr>
</table>
</form>
</td></tr></table>
</body>
</html>
  Download:  JSTLWorkingWithMoreThanOneBean.zip( 1,071 k)








24.10.Java Beans
24.10.1.Use Java Bean in JSTL
24.10.2.JSTL Working With More Than One Bean
24.10.3.Set Form Data to Bean and Output