Wednesday, 20 July 2016

JSTL Tags and Functions

Hello everybody!

While working on one of my projects, I tried to use JSTL tags and found it very useful in terms of efficiency. So I thought getting into more details and preparing a post about JSTL would be nice.

JSTL stands for "JSP Standard Tag Library". These tags and functions help you write your codes more simple and efficient. JSTL provides a faster development as it has many simplified tags. We also benefit from code reusability.


To be able to use JSTL in your project, you should download jstl jar file from here and put it under the "lib" folder in your project directory.

After that you must include a <taglib> directive at the top of each JSP that uses JSTL library. <taglib> directive changes according to each tag type. There are 5 types of JSTL tags.(core tags, sql tags, xml tags, internationalisation/formatting tags, function tags).


Basic (and probably most commonly used) tags are listed below with their explanations. But of course you can find more tags in the links I added at the end of the post.

Core Tags

<c:catch>: used in exception handling.
<c:choose>: it is like switch statement.
<c:if>: used for testing conditions.
<c:import>: used for importing the content from another file/page to the current JSP page.
<c:forEach>: used for executing the same set of statements for a finite number of times.
<c:out>: displays the content
<c:otherwise>: It works like default attribute in switch-case statements.
<c:param>: This JSTL tag is mostly used with <c:url> and <c:redirect> tags. It adds parameter and their values to the output of these tags.
<c:redirect>: used for redirecting the page.
<c:remove>: used for removing an attribute from a specified scope or from all scopes. By default it removes all of them.
<c:set>: to set up a variable value in a specified scope.
<c:when>: It’s like case statement in Java.



SQL Tags

<sql:setDataSource>: used to creates a simple DataSource according to given prototype.
<sql:query>: used to execute the SQL query defined in its body or through the sql attribute.
<sql:update>: used to execute the SQL update defined in its body or through the sql attribute.

XML Tags

The JSTL XML tags provide a JSP-centric way of creating and manipulating XML documents. We need to add two more libraries that are related to XML and XPath.(XercesImpl.jar, xalan.jar)

<x:choose>: Simple conditional tag, marked by <when> and <otherwise>.
<x:out>: Like <%= ... >, but for XPath expressions.
<x:if>: XML conditional tag.
<x:forEac>: XML iteration tag.
<x:otherwise>: Subtag of <choose> that follows <when> tags.
<x:parse>: Parses XML content from 'source' attribute or 'body'
<x:set>: Saves the result of an XPath expression evaluation in a 'scope'
<x:transform>: Conducts a transformation given a source XML document and an XSLT stylesheet

Internationalisation/Formatting Tags

<fmt:setLocale>: Stores the given locale in the locale 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:bundle>: Loads a resource bundle to be used by its tag body
<fmt:message>: Maps key to localized message and performs parametric replacement
<fmt:param>: Supplies an argument for parametric replacement to a containing <message> tag
<fmt:formatNumber>: Formats a numeric value as a number, currency, or percentage
<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

Function Tags

fn:contains(): checks whether the given string is present in the input as sub-string. (case sensitive)
fn:length(): used for computing the length of a string or to find out the number of elements in a collection.
fn:startsWith(): checks the specified string is a prefix of given string.
fn:trim(): removes spaces from beginning and end of a string and function.
fn:toUpperCase(): the opposite of fn:toLowerCase() function. It converts input string to a uppercase string.
fn:substring(): used for getting a substring from the provided string.


I have found a very useful resource to get information about other JSTL tags and functions. tutorialspoint.com is a good resource for many many subjects in programming especially you are a beginner(But they have non-programming tutorials as well). And here is their detailed tutorial about JSTL tags and functions. And you can also get information on Oracle website.

Hope to see you in the next blog post.

Selen

No comments:

Post a Comment