티스토리 뷰

 [jsp13강] JSTL Function  태그 사용법



이번글에서는 JSTL Function 태그에 대해서 알아보겠습니다.

JSTL Function 태그들은 다른 개발언어에서도 많이 지원하는 내장함수 의 기능을 보여줍니다.

많이 사용하는 기능들이기에 예제를통해서도 알아보려고합니다.

우선 Function 태그설명에 들어가기전 JSTL 태그에대해 잘모르시면 이전글을 참조해주세요.

JSTL 태그에 대한 이전글들을 하단에 달아놓았습니다.


 이전 글 링크 


 length, toUpperCase, toLowerCase, substring, substringAfter, substringBefore, trim, replace, indexOf, startsWith, endsWith, contains, containslgnoreCase, split, join, escapeXml


length : 오브젝트가 Conllection 인 경우 항목의 개수를, 문자인 경우 문자길이를 반환해줍니다.

toUpperCase : 문자를 대문자로 변환해 줍니다.

toLowerCase : 문자를 소문자로 변환해 줍니다.

substring : 각종언어의 substring 과 동일한 기능

substringAfter(str1, str2) : str1에서 str1에 포함되어 있는 str2 이후의 문자열을 구함

substringBefore(str1, str2) : str1에서 str1에 포함되어 있는 str2 이전의 문자열을 구함

trim : str 좌우의 공백 문자를 제거 

replace(str, src, dest) : str에 있는 src를 dest로 변환

indexOf(str1, str2) : str1에서 str2가 위치한 인덱스를 구함

startsWith(str1, str2) : str1이 str2로 시작할 경우 true, 그렇지 않을 경우 false를 반환

endsWith(str1, str2) : str1이 str2로 끝나는 경우 true, 그렇지 안을 경우 false를 반환

contains(str1, str2) : st1이 str2를 포함하고 있을 경우 true를 반환

containslgnoreCase(str1, str2) : 대소문자 구분없이 str1이 str2를 포함하고 있을 경우 true를 반환

split(str1, str2) :  str2로 명시한 글자를 기준으로 str1을 분리해서 배열로 반환 

join(array, str2) :  array에 저장된 문자열을 합침, 각 문자열의 사이에는 str2가 붙음

escapeXml :  XML의 객체 참조에 해당하는 특수문자를 처리함 


 사용 문법 

${fn: length(obj 또는 str)}

${fn: toUpperCase(str)}

${fn: toLowerCase(str)}

${fn: substring(str, idx1, idx2)}

${fn: substringAfter(str1, str2)}

${fn: substringBefore(str1, str2)}

${fn: trim(str)}

${fn: replace(str, src, dest)}

${fn: indexOf(str1, str2)}

${fn: startsWith(str1, str2)}

${fn: endsWith(str1, str2)}

${fn: contains(str1, str2)}

${fn: containslgnoreCase(str1, str2)}

${fn: split(str1, str2)}

${fn: join(array, str2)}

${fn: escapeXml(str)}


str : 문자

dest : 문자

idx1 : 숫자

idx2 : 숫자

array: 배열

 예제 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 
<title>Insert title here</title>
</head>
<body>
    length: ${fn:length('123123')}
    <br/>
    toUpperCase: ${fn: toUpperCase('sss')}
    <br/>
    toLowerCase: ${fn: toLowerCase('SSS')}
    <br/>
    substring: ${fn: substring('test', 1, 3)}
    <br/>
    substringAfter: ${fn: substringAfter('testtest', 'tt')}
    <br/>
    substringBefore: ${fn: substringBefore('testtest', 'tt')}
    <br/>
    trim: ${fn: trim('  test  ')}
    <br/>
    replace: ${fn: replace('test', 't', 's')}
    <br/>
    indexOf: ${fn: indexOf('test', 'e')}
    <br/>
    startsWith: ${fn: startsWith('test', 'e')}
    <br/>
    endsWith: ${fn: endsWith('test', 'e')}
    <br/>
    contains: ${fn: contains('test', 'e')}
    <br/>
    containsIgnoreCase: ${fn: containsIgnoreCase('test', 'e')}
    <br/>
    <c:set var="testlist" value="${fn: split('j,s,t,l',',')}" />
    split: 
    <c:forEach var="test" items="${testlist}" varStatus="i">
    ${test}
    </c:forEach
    <br/>
    join: ${fn: join(testlist, ',')}
    <br/>
    escapeXml: ${fn: escapeXml('<abc>is second String.</abc>')}
</body>
</html>
cs

 실행결과 및 설명  

 


length: ${fn:length('123123')} 

"123123" 이라는 문자의 길이가 6이므로 6이 출력됩니다.


toUpperCase: ${fn: toUpperCase('sss')} 

"sss" 를 대문자로 변경하여 "SSS" 를 출력됩니다.


toLowerCase: ${fn: toLowerCase('SSS')}

 "SSS" 대문자를 소문자로 변경하여 "sss" 가 출력됩니다.


substring: ${fn: substring('test', 1, 3)} 

"test" 문자 1에 해당하는 "t" 뒤에서부터 3에 해당하는 "t"를 뺴고 "es" 가 출력됩니다.


substringAfter: ${fn: substringAfter('testtest', 'tt')}

 "tt" 라는 문자를 찾으면 "ttest" 가 되는대 시작부분을 제거하면 "est"가 남아 위와같이 출력됩니다.


substringBefore: ${fn: substringBefore('testtest', 'tt')}

"tt" 라는 문자를 찾아 앞부분을 출력하기 떄문에 "tes" 가 출력됩니다.


trim: ${fn: trim('  test  ')} 

공백을 제거한후 "test"만 출력됩니다.


replace: ${fn: replace('test', 't', 's')} 

"test"라는 문장에서  "t"를 모두 "s" 로 바꿔 "sess"가 출력됩니다.


indexOf: ${fn: indexOf('test', 'e')}

"test" 문자열에서 e 의 첫번째 위치를 알려줍니다. 그래서 1이 출력됩니다.


startsWith: ${fn: startsWith('test', 'e')} 

논리반환형이고  "test" 에 시작문자가 "e" 가 아니기때문에 false 를 출력합니다.


endsWith: ${fn: endsWith('test', 'e')}

 논리반환형이고  "test" 에 끝문자가 "e" 가 아니기때문에 false 를 출력합니다.


contains: ${fn: contains('test', 'e')}

 논리반환형이고  "test" 에 "e" 가 포함되었기때문에 true 를 출력합니다.


containsIgnoreCase: ${fn: containsIgnoreCase('test', 'e')}

 논리반환형이고  "test" 에 대소문관계없이 "e" 가 포함되었기때문에 true 를 출력합니다.


<c:set var="testlist" value="${fn: split('j,s,t,l',',')}" />

split: 

<c:forEach var="test" items="${testlist}" varStatus="i">

${test}

</c:forEach> 

JSTL Core 태그를 활용해 list 형 변수를 "testlist" 라고 만들어주고, 값으로는 "${fn: split('j,s,t,l',',')}" 넣어줍니다. (split 반환형이 리스트임)

그다음 JSTL Core 태그 forEach 를 사용하여 리스트의 내용을 하나씩 확인해보면..

j  s  t  l

이렇게 4개의 값을 확인할수 있습니다.


join: ${fn: join(testlist, ',')}

위에서 만들어놓은 배열형 변수 'testlist' 를 넣어주고 붙일때 구분자로 "," 넣어주었습니다.

그결과 문자열로 "j,s,t,l" 출력됩니다


escapeXml: ${fn: escapeXml('<abc>is second String.</abc>')}

"<abc>is second String.</abc>" 라고 문자열을 넣어도 태그형식으로 읽히지 않고 문자열로 출력되는것을 확인할수 있습니다.


 글을 마치며..


JSTL Function  태그는 실무에서도 많이 사용할정도로 유용한 태그입니다.

한번씩 사용해보시고 이런기능도 지원을 하는구나! 정도로 알고 넘어가면 좋을것 같습니다.

모두 수고하세요!


도움이 되셨다면 로그인 없이 가능한

아래 하트♥공감 버튼을 꾹 눌러주세요! 



댓글