๐Ÿ’ป Programming/JSP

[JSP] JSTL ( JSP Standard Tag Library )

JSTL ๋Š” ์œ ์šฉํ•œ JSP ํƒœ๊ทธ๋“ค์„ ๋ชจ์•„๋†“์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ์ปค์Šคํ…€ ํƒœ๊ทธ๋ฅผ JSTL tags์™€ ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋„ ์ œ๊ณตํ•˜๋ฏ€๋กœ ์•Œ์•„๋‘์‹œ๋ฉด ์ข‹์„๊ฑฐ์—์š”. 

JSTLํƒœ๊ทธ๋Š” ๊ธฐ๋Šฅ๋ณ„๋กœ ๋ถ„๋ฅ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.  

  • Core Tags

  • Formatting tags

  • SQL tags

  • XML tags

  • JSTL Functions


JSTL ์„ค์น˜ํ•˜๊ธฐ

Apache Tomcat container ๋ฅผ ์‚ฌ์šฉ์ค‘์ด๋ฉด ์•„๋ž˜ ๋‘๊ฐ€์ง€ ์Šคํ…์œผ๋กœ ์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋ฉ๋‹ˆ๋‹ค.

  • Apache Standard Taglib ์ด๊ณณ์—์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์••์ถ•์„ ํ’€์–ด์ฃผ์„ธ์š”.

  • ์••์ถ•์ด ํ’€๋ฆฐ ๋””๋ ‰ํ† ๋ฆฌ๋‚ด์˜ lib๋””๋ ‰ํ† ๋ฆฌ์˜ JAR ํŒŒ์ผ๋“ค์„ ์—ฌ๋Ÿฌ๋ถ„์ด ์‚ฌ์šฉํ•˜๋ ค๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ webapps\ROOT\WEB-INF\lib ๋””๋ ‰ํ† ๋ฆฌ์•ˆ์— ๋„ฃ์–ด์ฃผ์„ธ์š”. ( ์ด๊ฑฐ๋Š” ์ดํด๋ฆฝ์Šค์—์„œ ์ง์ ‘ ํ•˜์‹ค ์ˆ˜ ์žˆ๋Š”๊ฑด ์•„์‹œ์ฃ ? )  

ํƒœ๊ทธ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด <taglib> ๋””๋ ‰ํ‹ฐ๋ธŒ๋ฅผ JSP ํŽ˜์ด์ง€ ์ œ์ผ ์œ„์ชฝ์— ์„ ์–ธํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.  


Core Tags:

JSTL ํƒœ๊ทธ ์ค‘์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ํƒœ๊ทธ์ž…๋‹ˆ๋‹ค.  

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

 

TagDescription
<c:out >Like <%= ... >, but for expressions.
<c:set >Sets the result of an expression evaluation in a 'scope'
<c:remove >Removes a scoped variable (from a particular scope, if specified).
<c:catch>Catches any Throwable that occurs in its body and optionally exposes it.
<c:if>Simple conditional tag which evalutes its body if the supplied condition is true.
<c:choose>Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>
<c:when>Subtag of <choose> that includes its body if its condition evalutes to 'true'.
<c:otherwise >Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'.
<c:import>Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'.
<c:forEach >The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality .
<c:forTokens>Iterates over tokens, separated by the supplied delimeters.
<c:param>Adds a parameter to a containing 'import' tag's URL.
<c:redirect >Redirects to a new URL.
<c:url>Creates a URL with optional query parameters


Formatting tags:

ํฌ๋งคํŒ… ๊ด€๋ จ ํƒœ๊ทธ๋“ค์ž…๋‹ˆ๋‹ค.๋‹ค๊ตญ์  Web ์‚ฌ์ดํŠธ๋ฅผ ๋งŒ๋“ค๋•Œ ์œ ์šฉํ•˜๋‹ค๊ณ  ํ•˜๋„ค์š”.

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

 

TagDescription
<fmt:formatNumber>To render numerical value with specific precision or format.
<fmt:parseNumber>Parses the string representation of a number, currency, or percentage.
<fmt:formatDate>Formats a date and/or time using the supplied styles and pattern
<fmt:parseDate>Parses the string representation of a date and/or time
<fmt:bundle>Loads a resource bundle to be used by its tag body.
<fmt:setLocale>Stores the given locale in the locale configuration variable.
<fmt:setBundle>Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable.
<fmt:timeZone>Specifies the time zone for any time formatting or parsing actions nested in its body.
<fmt:setTimeZone>Stores the given time zone in the time zone configuration variable
<fmt:message>To display an internationalized message.
<fmt:requestEncoding>Sets the request character encoding


SQL tags:

JSTL SQL ํƒœ๊ทธ๋Š” ๊ด€๊ณ„ํ˜• ๋ฐ์ดํƒ€๋ฒ ์ด์Šค ์‹œ์Šคํ…œ์„ ํ•ธ๋“ค๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ( Oracle, MySQL, Microsoft SQL Server )

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

 

TagDescription
<sql:setDataSource>Creates a simple DataSource suitable only for prototyping
<sql:query>Executes the SQL query defined in its body or through the sql attribute.
<sql:update>Executes the SQL update defined in its body or through the sql attribute.
<sql:param>Sets a parameter in an SQL statement to the specified value.
<sql:dateParam>Sets a parameter in an SQL statement to the specified java.util.Date value.
<sql:transaction >Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction.


XML tags:

JSTL XML ํƒœ๊ทธ๋Š” JSP์—์„œ XML ๋ฌธ์„œ๋ฅผ ์ƒ์„ฑ ๋ฐ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ XML์„ ํŒŒ์‹ฑํ•˜๊ฑฐ๋‚˜ ๋ฐ์ดํƒ€๋ฅผ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ XPath ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ถ”๊ฐ€์ ์ธ ์ปค์Šคํ…€ ํƒœ๊ทธ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.  

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

์•„๋ž˜์˜ ๋‘ jarํŒŒ์ผ๋„ <Tomcat Installation Directory>\lib ์— ์ถ”๊ฐ€ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

 

TagDescription
<x:out>Like <%= ... >, but for XPath expressions.
<x:parse>Use to parse XML data specified either via an attribute or in the tag body.
<x:set >Sets a variable to the value of an XPath expression.
<x:if >Evaluates a test XPath expression and if it is true, it processes its body. If the test condition is false, the body is ignored.
<x:forEach>To loop over nodes in an XML document.
<x:choose>Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>
<x:when >Subtag of <choose> that includes its body if its expression evalutes to 'true'
<x:otherwise >Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'
<x:transform >Applies an XSL transformation on a XML document
<x:param >Use along with the transform tag to set a parameter in the XSLT stylesheet


JSTL Functions:

JSTL ์€ ํ‘œ์ค€ ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„ ์ŠคํŠธ๋ง๊ด€๋ จ ํ•จ์ˆ˜๋“ค์ด์ฃ .

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

 

FunctionDescription
fn:contains()Tests if an input string contains the specified substring.
fn:containsIgnoreCase()Tests if an input string contains the specified substring in a case insensitive way.
fn:endsWith()Tests if an input string ends with the specified suffix.
fn:escapeXml()Escapes characters that could be interpreted as XML markup.
fn:indexOf()Returns the index withing a string of the first occurrence of a specified substring.
fn:join()Joins all elements of an array into a string.
fn:length()Returns the number of items in a collection, or the number of characters in a string.
fn:replace()Returns a string resulting from replacing in an input string all occurrences with a given string.
fn:split()Splits a string into an array of substrings.
fn:startsWith()Tests if an input string starts with the specified prefix.
fn:substring()Returns a subset of a string.
fn:substringAfter()Returns a subset of a string following a specific substring.
fn:substringBefore()Returns a subset of a string before a specific substring.
fn:toLowerCase()Converts all of the characters of a string to lower case.
fn:toUpperCase()Converts all of the characters of a string to upper case.
fn:trim()Removes white spaces from both ends of a string.

 

 

 

Reference : http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm