JSTL nedir?
JSTL yani JavaServer Pages Standard Tag Library, JSP sayfalarında basit taglar ile java işlemleri yapmaya yarar.Nasıl kullanılır?
JSP sayfasına bir taglib direktifi vererek kullanılabilir.<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
JSTL kütüphaneleri ve sağladıkları destekler
- Core: http://java.sun.com/jsp/jstl/core
- XML: http://java.sun.com/jsp/jstl/xml
- Internationalization: http://java.sun.com/jsp/jstl/fmt
- SQL: http://java.sun.com/jsp/jstl/sql
- Functions: http://java.sun.com/jsp/jstl/functions
Area
|
Subfunction
|
Prefix
|
---|---|---|
Core
|
Variable support
|
c
|
Flow control
| ||
URL management
| ||
Miscellaneous
| ||
XML
|
Core
|
x
|
Flow control
| ||
Transformation
| ||
I18N
|
Locale
|
fmt
|
Message formatting
| ||
Number and date formatting
| ||
Database
|
SQL
|
sql
|
Functions
|
Collection length
|
fn
|
String manipulation
|
Core library
Temel işlemler yapılabilir. foreach, if, try-catch, vb.
Area
|
Function
|
Tags
|
Prefix
|
---|---|---|---|
Core
|
Variable support
| remove set |
c
|
Flow control
| choose when otherwise forEach forTokens if | ||
URL management
| import param redirect param url param | ||
Miscellaneous
| catch out |
Core library örnekleri
a. Variable support / değişken destek tagları
<c:set var="foo" scope="session" value="..."/>
b. Flow control / akış kontrolü tagları<c:set var="foo"> ... </c:set><c:set var="bookId" value="${param.Remove}"/><c:remove var="cart" scope="session"/><c:set var="bookId" scope="page" value="#{BooksBean.books}"/> ... <h:inputText id="bookId" value="#{bookId}"/> ...
<% Iterator i = cart.getItems().iterator(); while (i.hasNext()) { ShoppingCartItem item = (ShoppingCartItem)i.next(); ... %> <tr> <td align="right" bgcolor="#ffffff"> ${item.quantity} </td> ... <% } %>...
c. Conditional / koşullu taglar
<c:if test="${!empty param.Add}"> <c:set var="bid" value="${param.Add}"/> <jsp:useBean id="bid" type="java.lang.String" /> <sql:query var="books" dataSource="${applicationScope.bookDS}"> select * from PUBLIC.books where id = ? <sql:param value="${bid}" /> </sql:query> <c:forEach var="bookRow" begin="0" items="${books.rows}"> <jsp:useBean id="bookRow" type="java.util.Map" /> <jsp:useBean id="addedBook" class="database.Book" scope="page" /> ... <% cart.add(bid, addedBook); %> ... </c:if>
...
<c:choose> <c:when test="${customer.category == ’trial’}" > ... </c:when> <c:when test="${customer.category == ’member’}" > ... </c:when> <c:when test="${customer.category == ’preferred’}" > ... </c:when> <c:otherwise> ... </c:otherwise> </c:choose>...
<c:choose> <c:when test="${count == 0}" > No records matched your selection. </c:when> <c:otherwise> ${count} records matched your selection. </c:otherwise> </c:choose>
<c:forEach var="item" items="${sessionScope.cart.items}"> ... <tr> <td align="right" bgcolor="#ffffff"> ${item.quantity} </td> ... </c:forEach>
d. Iterator tagları
<c:forEach var="book" items="#{BooksBean.books}"> ... <h:inputText id="quantity" value="#{book.quantity}"/> ... </c:forEach>
...
<acme:transform> <jsp:include page="/exec/employeesList"/> <acme:transform/>
...
<c:import url="/books.xml" var="xml" /> <x:parse doc="${xml}" var="booklist" scope="application" />
...
<c:url var="url" value="/catalog" > <c:param name="Add" value="${bookId}" /> </c:url> <p><strong><a href="${url}">
...
<c:out value="value" [escapeXml="{true|false}"] [default="defaultValue"] />
...
Hiç yorum yok:
Yorum Gönder