티스토리 뷰

spring

JSTL

코딩하는 둥아 2020. 8. 6. 16:32
728x90

JSTL (JSP Standard Tag Library)

JSTL Core Tag

 

  • <c:out> Tag
    • <%= ... %>과 유사한 역할
    • 문자열 출력
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...

<c:out value="${'Welcome to 2020WebCamp!'}" />
  • <c:import> Tag
    • jsp include와 유사한 역할
<c:import url="./exam1.jsp">
   <c:param name="src1" value="www.naver.com" />
   <c:param name="src2" value="www.daum.net" />
</c:import>

// exam1.jsp
<a href="${param.src1}"> 네이버 바로가기 </a>
<a href="${param.src2}"> 다음 바로가기 </a>

  • <c:set> Tag
    • jsp:setProperty action tag와 유사한 태그
  • <c:remove> Tag
    • c:set으로 설정한 변수의 값을 없앨 수 있다
<c:set var="Income" scope="session" value="${4000*4}"/>
<p>c:remove 하기 전 income 값: <c:out value="${Income}"/></p>
<c:remove var="Income"/>
<p>c:remove 한 후 income 값: <c:out value="${Income}"/></p>

  • <c:catch> Tag
    • error handling에 사용
    • Throwable exceptions을 일으킴
<c:catch var ="catchtheException">  
   <% int x = 2/0;%>  
</c:catch>  

<c:if test = "${catchtheException != null}">  
	<p>The type of exception is: ${catchtheException}<br>There is an exception: ${catchtheException.message}</p>
</c:if>

  • <c:if>Tag
    • 조건문
    • outcome변수의 값이 8000이상이면 p 태그 문구 출력됨
<c:set var="outcome" scope="session" value="${4000*4}"/>  
<c:if test="${outcome > 8000}">  
   <p>My income is: <c:out value="${outcome}"/><p>  
</c:if> 
  • <c:choose> <c:when> <c:otherwise> Tag
    • <c:choose> : java의 switch문이랑 유사함
      • <c:when>은 choose의 subtag, condition이 트루일때
      • <c:otherwise> : 위의 조건들이 모두 false일 때, default와 비슷한 느낌
<c:set var="income" scope="session" value="${9000}"/>  
<p>Your income is : <c:out value="${income}"/></p>  
<c:choose>  
    <c:when test="${income <= 1000}">  
       Income is not good.  
    </c:when>  
    <c:when test="${income > 10000}">  
        Income is very good.  
    </c:when>  
    <c:otherwise>  
       Income is undetermined...  
    </c:otherwise>  
</c:choose>  
  • <c:forEach> Tag
    • java의 while, do-while, for loop이랑 같은 역할을 함
<c:forEach var="j" begin="1" end="3">
	Item <c:out value="${j}"/>
</c:forEach>
  • <c:forTokens> Tag
    • 설정한 delims에 맞게 items를 잘라줌
<h3>tokens</h3>
<c:forTokens items="SeungA-Seulgi-Yeeun" delims="-" var="name">
<c:out value="${ name }"/><p>
</c:forTokens>
  • <c:param> Tag
    • value attribute: parameter value
    • name attribute: parameter name
    • param으로 넘긴 변수를 쿼리로 받을 수 있음!
<h3>param</h3>
<c:url value="./index1.jsp" var="completeURL">
<c:param name="trackingId" value="786" />
<c:param name="user" value="SeungA"/>
</c:url>

${ completeURL }

  • <c:redirect> Tag
    • 새로운 URL로 브라우저를 redirect 시켜줌
    • 아래 예시의 경우, 실행시키면 네이버가 화면에 나옴
<h3>redirect</h3>
<c:set var="url" value="0" scope="request"/>
<c:if test="${ url<1 }">
	<c:redirect url="http://naver.com"/>
</c:if>
<c:if test="${ url>1 }">
	<c:redirect url="http://javatpoint.com"/>
</c:if>
  • <c:url> Tag
    • <c:url value="URL"/>

 

JSTL Function Tags

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  

// contain : 대소문자 상관있음. 포함된 문자열이 정확히 들어간 문자열 찾기
<c:set var="String" value="Welcome to javatpoint"/>  
  
<c:if test="${fn:contains(String, 'javatpoint')}">  
   <p>Found javatpoint string<p>  
</c:if>  

// containsIgnoreCase : 대소문자 구분하지 않고 포함된 문자열 찾기
<c:if test="${fn:containsIgnoreCase(String, 'javatpoint')}">  
   <p>Found javatpoint string<p>  
</c:if>  
  
<c:if test="${fn:containsIgnoreCase(String, 'JAVATPOINT')}">  
   <p>Found JAVATPOINT string<p>  
</c:if>  

// endsWith : 특정 문자열로 끝나는지 판단
<c:set var="String" value="Welcome to JSP programming"/>  

<c:if test="${fn:endsWith(String, 'programming')}">  
   <p>String ends with programming<p>  
</c:if>  
  
<c:if test="${fn:endsWith(String, 'JSP')}">  
   <p>String ends with JSP<p>  
</c:if>  

//escapeXml
// 결과적으로 이 function을 사용하면 xml markup language가 해석되지 않고 그냥 문자열로 인식됨
<c:set var="string1" value="It is first String."/>  
<c:set var="string2" value="It is <xyz>second String.</xyz>"/>  
  
<p>With escapeXml() Function:</p>  
<p>string-1 : ${fn:escapeXml(string1)}</p>  
<p>string-2 : ${fn:escapeXml(string2)}</p>  
  
<p>Without escapeXml() Function:</p>  
<p>string-1 : ${string1}</p>  
<p>string-2 : ${string2}</p>  

// indexOf : 우리가 찾는 문자열이 전체 문자열에서 몇번째 index에 있는지 위치를 알려줌
<c:set var="string1" value="It is first String."/>  
<c:set var="string2" value="It is <xyz>second String.</xyz>"/>  
  
<p>Index-1 : ${fn:indexOf(string1, "first")}</p>  
<p>Index-2 : ${fn:indexOf(string2, "second")}</p>  

// trim: 문자열에서 2개 이상의 큰 여백들을 없애줌
<c:set var="str1" value="Welcome to JSP        programming         "/>  
<p>String-1 Length is : ${fn:length(str1)}</p>  
  
<c:set var="str2" value="${fn:trim(str1)}" />  
<p>String-2 Length is : ${fn:length(str2)}</p>  
<p>Final value of string is : ${str2}</p>  

// startWith : true or false return, 원하는 문자열로 시작하는지 판단
<c:set var="msg" value="The Example of JSTL fn:startsWith() Function"/>  
The string starts with "The": ${fn:startsWith(msg, 'The')}  
<br>The string starts with "Example": ${fn:startsWith(msg, 'Example')}  

//split: 지정한 문자를 기준으로 잘라줌
//join: 지정한 문자를 기준으로 합쳐줌
<c:set var="str1" value="Welcome-to-JSP-Programming."/>  
<c:set var="str2" value="${fn:split(str1, '-')}" />  
<c:set var="str3" value="${fn:join(str2, ' ')}" />  
  
<p>String-3 : ${str3}</p>  
<c:set var="str4" value="${fn:split(str3, ' ')}" />  
<c:set var="str5" value="${fn:join(str4, '-')}" />  
<p>String-5 : ${str5}</p>    
 
// toLowerCase
${fn:toLowerCase("HELLO")}  

// toUpperCase
${fn:toUpperCase("hello")}  

//substring: 지정한 인덱스를 기준으로 문자열을 자름
<c:set var="string" value="This is the first string."/>  
${fn:substring(string, 5, 17)}

// substringAfter: 지정한 문자열 다음으로 자름
// 결과: Jain
<c:set var="string" value="Nakul Jain"/>  
${fn:substringAfter(string, "Nakul")}  

//substringBefore: 지정한 문자열 이전까지 자름
<c:set var="string" value="Hi, This is JAVATPOINT.COM developed by SONOO JAISWAL."/>  
${fn:substringBefore(string, "developed")}  

//length: 문자열의 길이 반환
<c:set var="str1" value="This is first string"/>  
<c:set var="str2" value="Hello"/>  
Length of the String-1 is: ${fn:length(str1)}<br>  
Length of the String-2 is: ${fn:length(str2)}  

//replace : (input, search_for, replace_with)
<c:set var="author" value="Ramesh Kumar"/>  
<c:set var="string" value="pqr xyz abc PQR"/>  
${fn:replace(author, "Ramesh", "Suresh")}  
${fn:replace(string, "pqr", "hello")}  

 

728x90

'spring' 카테고리의 다른 글

JSP Spring 개발환경 구축하기  (0) 2020.08.04
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함