자바(JSP)
JSTL 문법 한방에 끝내기
에스크리토
2018. 6. 22. 16:14
반응형
<%@ page language="java" isELIgnored="false" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!-- set, out -->
<c:set var="country" value="china" />
<c:set var="intArray" value="<%=new int[] {1,2,3,4,5}%>" />
<p><c:out value="${country}" default="Korea" escapeXml="true"/></p>
<p>${country}</p>
<p>${intArray[0]}</p>
<!-- if -->
<p>---------- if -----------</p>
<c:set var="login" value="false" />
<c:if test="${!login}">
<p><a href="/login.ok">로그인</a></p>
</c:if>
<c:if test="${login}">
<p><a href="/logout.ok">로그아웃</a></p>
</c:if>
<c:if test="${!empty country}"><p><b>${country}</b></p></c:if>
<!-- choose, when, otherwise -->
<c:choose>
<c:when test="${login}">
<p><a href="/logout.ok">로그아웃</a></p>
</c:when>
<c:otherwise>
<p><a href="/login.ok">로그인</a></p>
</c:otherwise>
</c:choose>
<!-- forEach 정수 범위내의 반복 -->
<c:forEach var="i" begin="0" end="10" step="2" varStatus="x">
<p> i = ${i}, i*i = ${i * i} <c:if test="${x.last}">, last = ${i}</c:if> </p>
</c:forEach>
<!-- forEach 컬렉션 범위내의 반복 -->
<%
java.util.List list = new java.util.ArrayList();
java.util.Map map = new java.util.HashMap();
map.put("color","red");
list.add(map);
map = new java.util.HashMap();
map.put("color","blue");
list.add(map);
map = new java.util.HashMap();
map.put("color","green");
list.add(map);
request.setAttribute("list", list);
%>
<c:forEach var="map" items="${list}" varStatus="x">
<p> map(${x.index}) = ${map.color} </p>
${x.current}<br/> <!– 현재 아이템 –>
${x.index}<br/> <!– 0부터의 순서 –>
${x.count}<br/> <!– 1부터의 순서 –>
${x.first}<br/> <!– 현재 루프가 처음인지 반환 –>
${x.last}<br/> <!– 현재 루프가 마지막인지 반환 –>
${x.begin}<br/> <!– 시작값 –>
${x.end}<br/> <!– 끝값 –>
${x.step}<br/> <!– 증가값 –>
</c:forEach>
<!-- forTokens 예 -->
<b>
<c:forTokens var="color" items="빨|주|노|초|파|남|보" delims="|" varStatus="i" >
<c:if test="${i.first}">color : </c:if>
${color}
<c:if test="${!i.last}">,</c:if>
</c:forTokens>
</b>
<!-- remove -->
<c:remove var="country" />
<c:remove var="intArray" />
<!-- Set Locale -->
<fmt:setLocale value="ko_KR" />
<!-- formatNumber -->
<p>number : <fmt:formatNumber value="1234567.89" type="number"/></p>
<p>currency : <fmt:formatNumber value="1234567.89" type="currency" currencySymbol="₩" /> </p>
<p>percent : <fmt:formatNumber type="percent">0.159</fmt:formatNumber></p>
<p>pattern=".000" :<fmt:formatNumber value="1234567.1" pattern=".000" /></p>
<p>pattern="#,#00.0#":<fmt:formatNumber value="1234567.891" pattern="#,#00.0#"/></p>
<!-- parseNumber -->
<p>number : <fmt:parseNumber value="1,234,567.89" type="number"/></p>
<p>currency : <fmt:parseNumber value="12345abcdef" integerOnly="false" type="number" /></p>
<!-- formatDate -->
<c:set var="now" value="<%= new java.util.Date() %>" />
<p> date full : <fmt:formatDate value="${now}" type="date" dateStyle="full" /></p>
<p> date short : <fmt:formatDate value="${now}" type="date" dateStyle="short" /></p>
<p> time : <fmt:formatDate value="${now}" type="time" /></p>
<p> both full : <fmt:formatDate value="${now}" type="both" dateStyle="full" timeStyle="full" /></p>
<p> pattern 1 : <fmt:formatDate value="${now}" pattern="yyyy-MM-dd aa hh:mm:ss" /></p>
<p> pattern 2 : <fmt:formatDate value="${now}" pattern="yyyy-MM-dd hh:mm:ss" /></p>
<c:set var="name" value=" Oracle dbms 오라클클럽 "/>
<p>name: ${fn:replace(name, ' ', '*')}</p>
<c:set var="name" value="${fn:trim(name)}"/>
<p>trim name: ${fn:replace(name, ' ', '*')}</p>
<p>length(name): ${fn:length(name)}</p>
<p>toUpperCase(name): ${fn:toUpperCase(name)}</p>
<p>toLowerCase(name): ${fn:toLowerCase(name)}</p>
<p>substring(name,3,6): ${fn:substring(name,3,6)}</p>
<p>substringBefore(name,'dbms'): ${fn:substringBefore(name, 'dbms')}</p>
<p>substringAfter(name,'dbms'): ${fn:substringAfter(name, 'dbms')}</p>
<p>replace(name, '오라클클럽', 'OracleClub'): ${fn:replace(name, '오라클클럽', 'OracleClub')}</p>
<p>indexOf(name, 'dbms'): ${fn:indexOf(name,'dbms')}</p>
<p>contains(name, 'Oracle'): ${fn:contains(name, 'Oracle')}</p>
<p>containsIgnoreCase(name, 'opracle'): ${fn:containsIgnoreCase(name, 'oracle')}</p>
<p>startsWith(name, 'Oracle'): ${fn:startsWith(name, 'Oracle')}</p>
<p>startsWith(name, 'dbms'): ${fn:startsWith(name, 'dbms')}</p>
<p>endsWith(name, '오라클클럽'): ${fn:endsWith(name, '오라클클럽')}</p>
<c:remove var="name"/>
<c:set var="dbname" value="ORACLE, DB2, MSSQL, MYSQL, SYSBASE"/>
<c:set var="db_arr" value="${fn:split(dbname,',')}"/>
<c:forEach var="db" items="${db_arr}">
${db}<br/>
</c:forEach>
<p>join : ${fn:join(db_arr, ',')}</p>
이것만 알아도 ....
반응형