MY mENU


Showing posts with label JSTL. Show all posts
Showing posts with label JSTL. Show all posts

Tuesday, 29 May 2012

Formatting date with fmt:formatDate Action


Date and time are important matters in internationalized web applications. Different country has it own way to present date and time in different format. JSP has an action called to allow you to format date and time based on a specificlocale. Here is a table of action attributes. As you can see only value attribute isrequired for action.

Date and time are important matters in internationalized web applications. Different country has it own way to present date and time in different format.  JSP has an action called  to allow you to format date and time based on a specificlocale. Here is a table of action attributes. As you can see only value attribute is required for action.
Attributes
NameMandatoryMeaning
valueyesDate and/or time value to be formatted.
typenoAccept date or/and time to be used to format
dateStylenoSpecify predefined formatted style for date only if date value is used for formatting.
timeStylenoSpecify predefined formatted style for time only if time value is used for formatting.
patternnoSpecify standard customized pattern for formating date and/or time
timeZonenoIf the time value is used to format, this attribute specify time zone for that time value.
varnoThis attribute is for exporting scoped  variable which stores the formatted date and/or time as a string
scopenospecify the scope of var.

Let's take a look at an example of using  action to format date in different locale.
First we use the useBean tag to initialize a scoped variable called now and store current date value into this variable. Then we set the locale to different locales: US and FR and print out the current date in now variable by using  action.

Formatting Date and/or Time

By default format and print only date. If you want to print time or bothdate and time you can use the type attribute. The type attribute accepts date, time and both to allow you to print such a this information in a specific locale. Let's take a look at an example:
Formatting Date Time with predefined styles and custom patterns
You cannot only format and print date & time with predefined styles provided by JSTL but also with a custom pattern. The timeStyle and dateStyle attributes of specify the predefined styles you want to format. Patterns attribute enables you to define your own style. In this case, you need the date & time code table bellow to find exactly what you want to format.
CodeMeaning
GThe era (A.D., B.C., and so on)
yThe year (yy for two-digit years, yyyy for four digits)
MThe month (MM for numeric month, MMM or longer for month names orabbreviations)
wThe week of the year (ww for two digits)
WThe week of the month
DThe day of the year (DDD for three digits)
dThe day of the month (dd for two digits)
FThe numeric day of the week
EThe text day of the week (EEEE or longer for full name)
aAMa.m./PMp.m. indicator
HHour of the day (0–23)
kHour of the day (1–24)
KHour in a.m./p.m. (0–11)
hHour in a.m./p.m. (1–12)
mMinutes in hour
sSeconds in minute
SMilliseconds in second
zFull time zone name
ZRFC 822 time zone (for example, 0500)
Here is an example of using predefined styles and custom pattern to format and print date & time.

Monday, 28 May 2012

Formatting Number with fmt:formatNumber Action


JSTL provides you the to format number in different forms. If you remember, with the action you can print any number such as integer or floating-point number. But with you have more controls how number is formatted and printed. The most simple basic usage of the action is as follows:

The action accepts any number in the value attribute. It does more than the action that it formats the number based on the locale which specify by the web browser setting in the client side or set by the web application in the server side. Let's take a look at an example of using :
In the above example, first we define a variable and set its value to 200.51. Then we  use action to override the locale of the browser. We set the locale to en_US so when we use the action  to format the number therefore it displays as 200.51 which is USA format. Next we set the locale to fr_FR and print the number again, now it displays as 200,51 which is France format.
Let's take a look at attributes of the in more details:
NameMeaning
valueNumeric value to be formatted.
typeDetermine the value is formatted as number, currency, or percentage.
patternSpecify a custom formatting pattern for the output.
currencyCodeOnly apply for the currency formatting which accepts ISO 4217currency code.
currencySymbolOnly apply for the currency formatting, accepts currency symbol
groupingUsedUsed to define grouping separator for formated number
maxIntegerDigitsSpecify the maximum number of digits in the integer portion of the formatted number in the output.
minIntegerDigitsSpecify the minimum number of digits in the integer portion of the formatted number in the output.
maxFractionDigitsSpecify the maximum number of digits in the fractional portion of the formatted number in the output
minFractionDigitsSpecify the minimum number of digits in the fractional portion of the formatted number in the output.
varThe variable name of the exported scoped variable
scopescope of the var variable

 Printing the percentage

Let's play with the printing percentage value.
In order to print the percentage format, you assign percent to the type attribute of the action.
Printing different currency locale  To print currency, you assign currency to the type attribute and set the locale before outputting the formatted number.
Printing number with a custom pattern Sometimes, you'll need to print number with a specific custom pattern especially number inscientific notation. In these cases, you can define your own pattern and specify it in thepattern attribute of the action. Let's take a look at an example of printing a big number with a scientific pattern:
If you run the above code you will see the number is formatted as the pattern which is46.5637E9 .
There are a lot more cases you'll need to format number and output it on screen. You can refer to the attribute table above to find what you need and exercise with it to get more familiar with different kind number formatting.

Sunday, 27 May 2012

Working with URL-Related Actions


In web application developmentweb developers usually have to deal with URL manipulation such as importing URL, redirect to another URL...JSTL provides several useful URL-relatedactions to simplify URL manipulation.

The Action

The action is used to define a parameter. It is used inside the body of other actions such as import>. and  actions. the syntax of the action is simple as follows:
You specify the parameter name in the name attribute and its value in the value attribute.

The Action

The action enables you to retrieve the content of an URL and then you canprocess it within JSP page. The syntax of the action is as follows:
The only attribute url is mandatory. The URL could be in absolute or relative form. If it is in relative form, the resource you refer must be inside the web application. Inside the body of the action, you can also has parameter which specify by the action. Let's take a look at an example of using the action:
In the example, we use the action to retrieve the content of an XML file resided in the web application. Then we output the content of the XML file into the textarea. Later on if you learn how to process XML, you can parse this XML content to extract data inside it.

The  Action

 The  simply to redirect the current page to another page or URL. The syntax of the is as follows:
You can put any URL in the url attribute of the action. Let's take a look at an example of using the action:
In the above example, you choose a search engine and click Go button, the page will beredirect to the corresponding URL.
JSTL c:redirect Action Demo

The Action

The  action enables you to format an URL correctly. Inside the action, you can put multiple to construct URL. Here is an example of using the action: 
In the above example, we construct a link:  http://localhost/JSTLDemo/curl.jsp?mode=demo by using c:url action. We use the to define the query string which is mode with the value demo.

Thursday, 29 March 2012

JSTL Types

JSP standard tags library can be divided into four tag libraries which are:
  1. Core tags
  2. Internationalization (i18l) and formatting tags
  3. Relational database access tags
  4. XML processing tags
The goals of those tag library above are:
  • Simplify the task of writing JSP page by providing friendly XML base tags
  • Provide reusable logic from page's presentation
  • Make the JSP page easier to read and maintain
Core tags:
As its name imply, core tags provide the core functionality actions to JSP to make the most common actions easier to achieve in a more effective way. Core tags specify several actions such as displaying content based on condition, manipulating collections and URL managing. By using the core tags you'll never has to write a such small piece of scriptlet. (But you still need to know scriptlet to maintain legacy web applications and convert them to JSTL later on if you have to).

Internationalization(I18L) and formatting tags:
Those tags specify a series actions to make the web application multilingual. Those actions including managing resource bundle, locales and base names.

Relational database access tags:
Accessing database is a most major task of web applications. JSTL provides a list of standard tags to help you to manipulate data such as select, insert, update and delete from the relational databases.

XML processing tags:
XML becomes a standard of enterprise web application for exchanging data. ManipulateXML effectively therefore is very important for most web applications and of course JSTL also provides a list of tags for processing from XML parsing to XML transformation.

Introducing to JSP Standard Tag Library - JSTL

JSP is designed for the presentation layer in the web applications but it needs to contain the logic or code inside the page to control the way it presents the visual elements. Since JSP was borned, scriptlet has been used intensively therefore the JSP pages become messy and difficult to maintain. The HTML mixed with JSP scriptlet and opening and closing brace make JSP page even hard to extend.

In June 2002, JavaServer Pages standard library (JSTL) specification 1.0 was first released. JSTL provided a new ways for JSP author to work with different elements with standard friendly tags. The current version of the JSTL is 1.2 which was started in 2004 so we will use JSTL 1.2 for all the tutorials.