100 likes | 154 Views
Explore JSTL basics & examples for better web programming using tag libraries in JSP. Learn setting values, conditional statements, database access, and more. Complete with code snippets and explanations. Enhance your skills with practical samples.
E N D
JSP Standard Tag Library (JSTL) Internet Computing Laboratory @ KUT Youn-Hee Han
JSTL 기본 • 문법 <prefix:tagName attributes/> OR <prefix:tagName attributes> some body content </ prefix:tagName> 다운로드 하여 lib 디렉토리에 추가 Web root (2006777888) WEB-INF lib jstl.jar standard.jar classes Web Programming
JSTL 기본 • JSP에 taglib Directive 추가 • import into your JSP page each JSTL library that the page will reference. • taglib directives • core: <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> • Basic scripting functions such as loops, conditionals, and input/output • xml: <%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %> • XML processing • fmt: <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> • Internationalization and formatting of values such as currency and dates • sql: <%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %> • Database access • fn: <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> • JSTL functions Web Programming
JSTL “Hello World” • JSTL 기본 예제 /2006777888/hellojstl.jsp <%@ page contentType = "text/html; charset=euc-kr" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> Setting the value: "Hello World!" <c:set var="hello" value="Hello World!"/> <p/> <c:out value="${hello}"/> Web Programming
JSTL Sample • JSTL Example 1 <% if (list.size() > 0) { for (int i=0; i<list.size(); i++) { Data data = (Data) list.get(i); %> <%= data.getTitle() %> …<% } } else {%> 데이터가 없습니다.<% } %> <c:if test=“${!empty list}”> <c:forEach var=“data” list=“${list}”> ${data.title} <c:forEach> </c:if> <c:if test=“${empty list}”>데이터가 없습니다.</c:if> Web Programming
JSTL Sample • JSTL Example 2 <% String op1 = request.getParameter("option1"); if ( op1 != null ) { %> <b><%= op1 %><br></b> <% } %> <c:if test="${! empty param.option1}"> <b>${param.option1}<br></b> </c:if> • JSTL Example 3 <% Map likeMap = request.getParameterMap(); String[] like = (String[])likeMap.get("like"); for(int i=0; i<like.length;i++){ %> <b><%= like[i] %><br></b> <% } %> <c:forEach var="i" items="${paramValues.like}"> <b>${i}<br></b> </c:forEach> Web Programming
JSTL Sample • JSTL Example 4-1 <%@ page import="java.util.*"%> <p><h1>Customer Names</h1></p> <% List addresses = (List)request.getAttribute("addresses"); Iterator addressIter = addresses.iterator(); while(addressIter.hasNext()) { String addr = (String)addressIter.next(); if ((addr != null) && (addr.getLastName() != null) && (addr.getLastName().length() > 0)) { %> <%=addr.getLastName()%><br/> <% } else { %> N/A<br/> <% } } %> <p><h5>Last Updated on: <%=new Date()%></h5></p> Web Programming
JSTL Sample • JSTL Example 4-2 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <p><h1>Customer Names</h1></p> <c:forEach items="${addresses}" var="address"> <c:choose> <c:when test="${!empty address.lastName}" > <c:out value="${address.lastName}"/><br/> </c:when> <c:otherwise> N/A<br/> </c:otherwise> </c:choose> </c:forEach> <jsp:useBean id="now" class="java.util.Date" /> <p><h5>Last Updated on: <c:out value="${now}"/></h5></p> Web Programming
JSTL Sample • JSTL Example 5 <%@ page contentType = "text/html; charset=euc-kr" %> <%@ page isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> <c:set var="a" value="<B>ABC</B>" /> ${a}<BR> <c:out value="${a}"/><BR> <c:out value="${a}" escapeXml="true"/><BR> <c:out value="${a}" escapeXml="false"/><BR> <c:out value="${b}" escapeXml="false" default=“DEF" /> • JSTL Example 6 (교재 433page 소스코드를 JSTL 코드로 변환) <%@ page contentType = "text/html; charset=euc-kr" %> <%@ page isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> <c:set var="ids" value="<%= java.util.TimeZone.getAvailableIDs() %>" /> <c:forEach var="i" items="${ids}"> ${i}<BR> </c:forEach> Web Programming