1 / 16

CS320 Web and Internet Programming JSP Standard Tag Library (JSTL)

CS320 Web and Internet Programming JSP Standard Tag Library (JSTL). Chengyu Sun California State University, Los Angeles. Simple JSTL Example. <%@ page contentType="text/html" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html><head><title>JSTL Hello</title></head>

jolene
Download Presentation

CS320 Web and Internet Programming JSP Standard Tag Library (JSTL)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS320 Web and Internet ProgrammingJSP Standard Tag Library (JSTL) Chengyu Sun California State University, Los Angeles

  2. Simple JSTL Example <%@ page contentType="text/html" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html><head><title>JSTL Hello</title></head> <body> <c:out value="Hello World in JSTL." /> </body> </html>

  3. taglib Directive • URI • A unique identifier for the tag library • NOT a real URL • Prefix • A short name for the tag library • Could be an arbitrary name

  4. JSP Standard Tag Library (JSTL) http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html

  5. Flow control <c:if> <c:choose> <c:when> <c:otherwise> <c:forEach> <c:forToken> Variable support <c:set> <c:remove> URL <c:param> <c:redirect> <c:import> <c:url> Output <c:out> Exception handling <c:catch> JSTL Core

  6. JSTL Usage Examples • ShoppingCart.jsp • Login.jsp and Members.jsp

  7. Branch Tags <c:if test=“${!cart.notEmpty}”> The cart is empty.</c:if> <c:choose> <c:when test=“${!cart.notEmpty}”> The cart is emtpy. </c:when> <c:otherwise> <%-- do something --%> </c:otherwise> </c:choose>

  8. Loop Tags <%-- iterator style --%> <c:forEach items=“${cart.items}” var=“i”> ${i} <br> </c:forEach> <%-- for loop style --%> <c:forEach begin=“0” end=“${cart.size}” step=“1” var=“i”> ${cart.items[i]} </c:forEach> <forToken ....> Exercise

  9. Set and Remove Scope Variables In Login.jsp <c:set var=“authorized” value=“true” scope=“session”/> In CheckLogin.jsp <c:if test=“${empty sessionScope.authorized}”> <c:redirect url=“Login.jsp” /> </c:if> In Logout.jsp <c:remove var=“authorized” scope=“session” />

  10. Set Bean Property • <jsp:setProperty> • name attribute must be a bean declared by <jsp:useBean> • <c:set> • target attribute can be any object with a setter • Example: <c:set target=“${bb.a}” property=“a” value=“${param.a}” />

  11. URL Tags <c:import url=“/books.xml” var=“something” /> <x:parse doc=“${something}” var=“booklist” scope=“application” /> <c:url var="url" value="/catalog" > <c:param name="Add" value="${bookId}" /> </c:url> <a href="${url}">Get book</a>

  12. Output • You want to use <c:out> if • escapeXML=true • value is a Java.io.Reader object <c:out value=“100” /> <c:out value=“${price}” /> ${100} ${price}

  13. Character Conversion • When escapeXML=true

  14. Exception Handling • <c:catch> • see Error Page and Exception Handing

  15. fn:length() fn:contains() fn:containsIgnoreCase() fn:startWith() fn:endsWith() fn:indexOf() fn:replace() fn:trim() fn:toUpperCase() fn:toLowerCase() fn:substring() fn:substringAfter() fn:substringBefore() fn:split() fn:join() fn:escapeXML() JSTL Functions

  16. Function Example • Exercise

More Related