본문 바로가기

WEB_Programming/JSTL

9. JSTL에서 JavaBeans 사용

1. JSTL에서 Java Bean 이용
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/core-rt" prefix="c-rt" %>
<html>
  <head>
    <title>If Caseless</title>
  </head>

  <body>
    <c:set var="str" value="jStL" />

    <jsp:useBean id="str" type="java.lang.String" />

    <c-rt:if test='<%=str.equalsIgnoreCase("JSTL")%>'> They are
    equal</c-rt:if>
  </body>
</html>


2. 하나 이상의 Bean이용

2.1 Bid.java 빈 작성

2.2 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;
    }
   
}
       

2.3 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;
    }
   
}

       
2.4 빈 처리 jsp
<%@ 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>

       
2.5 Bid 처리 JSP

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

       
2.6 Bid 빈에 데이터 입력 JSP

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

     
3. 빈과 Output에 잘될 폼 데이터 설정
3.1 index.jsp
<html>
  <head><title>Create Person</title></head>
  <body>
    <h1>Enter your details</h1>
    <form action="listPageParameters.jsp" method="post">
      <table>
        <tr><td>First name:</td> <td><input type="text" name="firstName" /></td></tr>
        <tr><td>Last name:</td>  <td><input type="text" name="lastName"  /></td></tr>
        <tr><td>Age:</td>        <td><input type="text" name="age"       /></td></tr>
      </table>
      <input type="submit" value="Submit details" />
    </form>
  </body>
</html>


3.2 listPageParameters.jsp 작성
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<html>
  <head><title> Display details</title></head>
  <body>
    <h1>Your details (or, the details that you entered!)</h1>
    <table>
      <tr><td>First name</td>
          <td><c:out value="${param.firstName}" /></td>
      </tr>
      <tr><td>Last name</td>
          <td><c:out value="${param.lastName}" /></td>
      </tr>
      <tr><td>Age</td>
          <td><c:out value="${param.age}" /></td>
      </tr>
      <%-- The following two values are not passed from the HTML form
           and are present to show the syntax for specifying default
           values --%>
      <tr><td>Partner's name</td>
          <td><c:out value="${param.partnerName}" default="Unknown name" /></td>
      </tr>
      <tr><td>Partner's age</td>
          <td><c:out value="${param.partnerAge}">
                Unknown age
              </c:out>
          </td>
      </tr>
    </table>
  </body>
</html>


'WEB_Programming > JSTL' 카테고리의 다른 글

11. JSTL Cookie 처리  (0) 2008.06.20
10. JSTL의 Scope  (0) 2008.06.20
8. JSTL Set  (0) 2008.06.20
7. JSTL Collection 예제  (0) 2008.06.20
6. JSTL foreach 문 처리  (0) 2008.06.20