JSTL: Getting Started - JSTL: Understanding the Types
(Page 3 of 4 )
Since JSTL came as an answer to the problem of avoiding scriptlets, it should provide for nearly everything that could be achieved using scriptlets. To achieve this, the creators of JSTL provided four libraries containing forty-two actions/tags. These four libraries are Core, Formatting, XML and SQL. The libraries are categorized by their functions. Of these I will discuss the first two categories in this part. Here are the details:
Core
Core provides the basic functions such as branching and looping constructs, displaying values, creating and destroying scoped variables, manipulating properties of JavaBeans components and handling URLs. In short, it provides all the common routines that must be embedded using scriptlets. The three most commonly used tags of this library are out, foreach, and if. All of these provide the same kind of interface, even for different kinds of datatypes.
- Out evaluates an EL expression passed through the value attribute, coerces its output to the string type and outputs it (in reality the coerced value is sent to the current JSP Writer). The syntax of out is:
<c:out value [escapeXml] [default]/>
where value takes an expression, and escapeXml takes a Boolean value that specifies whether the following characters are converted to their corresponding character entity codes: < > & ' ". The default value is true. For example, to access the host address in the header of the request the statement would be:
<c:out value='${header.host}'/>
- Foreach can be used to iterate either over a range of integers or a collection. The syntax of foreach is:
<c:foreach items var [varStatus] [begin] [end] [step]></c:foreach>
where items represents the collection or range over which foreach has to iterate, var is the name of the scoped variable that references the current item of the iteration, varStatus references the object that contains the properties representing the current status of the iteration, begin represents the starting point (whether a value or an index), end represents the end point value for the index, and step takes the value by which the index must be incremented. For example, to iterate over the values between 1 and 10, inclusive, the code would be:
<c:forEach var='item' begin='5' end='10'>
value = <c:out value='${item}'/><br>
</c:forEach>
- If is the best choice when a simple condition must be applied. The syntax of if is:
<c:if test [var] [scope]>
body content that's processed if the value of the test
attribute is true
</c:if>
Test is the expression to be tested. Optional var and scope attributes specify a variable that stores the Boolean result of the expression specified with the test attribute, in the scope specified with the scope attribute. For example, to test whether a parameter named display in the request scope contains null or not, the code would be:
<c:if test='${not empty param.name}'>
<c:out value='${param.name}'/>
</c:if>
Now let's move on to the next category: formatting tags.
Formatting
Formatting provides support for internationalization and formatting. The formatting aspect includes date-time formatting, currency related formatting, number parsing and formatting as well as formatting percentages. The most commonly used tags in formatting are setLocale, message, formatNumber and formatDate.
The first two deal with the internationalization and the last two deal with formatting numbers and dates.
- setLocale sets the localization settings which are used by the message tag to do resource bundle look ups. The syntax of setLocale is:
<fmt:setLocale value />
where the value attribute takes the locale name as the value. For example, to set the locale to US English the code would be:
<fmt:setLocale value='en-US'/>
- message is responsible for retrieving localized messages from the resource bundles. The syntax is:
<fmt:message key [bundle] [var] [scope]/>
where key is the key specified in the resource bundle, the optional bundle attribute accepts a resource bundle, var is the variable into which the localized variable has to be placed and scope is either request, page, session or application. For example, to get a localized message greeting the code would be:
<fmt:message key='greeting'/>
- formatNumber can be used for formatting numbers according to the locale. The most commonly used syntax is:
<fmt:formatNumber value [pattern]/>
where the pattern is the optional attribute used to specify the pattern that has to be given as output. For example, to specify a grouping of integer digits and five fraction digits the code would be:
<fmt:formatNumber value='234682.155'
pattern='#,####.00000'/>
- formatDate does for dates what formatNumber does for numbers. The most common syntax is:
<fmt:formatNumber value [pattern]/>
where the pattern attribute is the custom pattern to which the date must be converted. For example, to display a date in 'MM/dd/yyyy' pattern the code would be:
<jsp:useBean id='now' class='java.util.Date'/>
<fmt:formatDate value='${now}'
pattern="MM/dd/yyyy"/>
That covers the most commonly used tags within the Core and Formatting libraries. In the next section, I will be developing an application that will display the details of an employee on the basis of selected employee id.
Next: JSTL in the Real World >>
More JavaScript Articles
More By A.P.Rajshekhar