JSP Standard Tag Libraries, Part 1
Pages: 1, 2
Using the XML processing tags provided in JSTL, you can manipulate XML from your JSP pages. This includes document parsing using
<x:parse> and using the
<x:expr> tag to specify XPath to select content, as well as performing XSLT transformations from within your JSP pages by using the
<x:transform> tag. You can also use
<x:foreach> to iterate over XML nodes.
You can easily access relational databases using SQL actions. By using various tags available (including
<sql:query>), you can do transaction-based database access. It is questionable, as a Java architect, why you would want to provide tags that mix presentation and data tier access. In theory, projects should be based on clean MVC architectures; however, page authors often face a different reality out there in the real world, and the need simply cannot be ignored. It is not encouraged to access the data tier in JSP pages, but if a page author must go that route (for whatever reason), doing so with a set of standard tags seems to be the lesser evil. I strongly feel that these SQL tags should only be used in prototyping or very simple applications, if at all. You really should be separating your presentation and logic tiers, especially a data tier.
One of the key features of JSTL is that it supports an expression language. An expression language leverages the fact that JSP-scoped attributes are the privileged way to communicate information from the business logic to the JSP pages. Why is this important? Well, think about how we have to do this now. If you are using JavaBeans or their properties with a custom tag, you must either have request-time expressions or make use of other functionality, such as the powerful tags that are provided with frameworks like Jakarta Struts. By using expression language in the tag, it's possible to provide values for an attribute of a tag and manipulate it in simple ways, without having to use scriptlets or request-time expression values. This sounds like something that might (or should) just be in the JSP specification, and maybe at some point it will be. But for now, it's part of the JSTL.
In order to be able to support both the scripting (
rtexprvalues) and the expression language (
elexprvalues) worlds, JSTL has the concept of twin tag libraries: one that supports the expression language, and one that supports request-time expression values. It's assumed that most developers will use the EL-based tag libraries; however, it's still possible for scripting page authors to use JSTL with
rtexprvalues. Using the
rtexprvalues will provide the benefit of type safety and performance via the request-time based tag libraries (their URI simply has the
-rt suffix appended).
For example, if we assume that we have an object bound in our page called
cd, we can use a expression to get the value of the artist property.
<c:if test="$cd.artist == 'Madonna'"> ... </c:if>
Notice that the expression starts with a
$ symbol. This is because something is needed to indicate that the string should be evaluated (and is not a static string). Most of the expression languages available in JSTL make use of a special symbol (usually the
$ character) to indicate this.
It's important to note that the whole idea of expression languages is being carefully watched and evaluated by the expert group. Because of this, you should think of it as more experimental than the rest of the JSTL. The community feedback will most likely determine whether it stays in the final version of the JSTL.
It is also possible that you can use JSTL as a base for developing your own custom tags. Some abstract classes are provided that can assist with rapid development of tags and promote integration of your own custom tags with JSTLs tag set.
For instance, you can build your own custom tags that make use of the JSTL's EL mechanism. By extending
javax.servlet.jsp.jstl.core.ConditionalTagSupport, you can write a conditional tag by simply implementing a single method that returns a boolean value corresponding with your tag's desired conditional behavior. Similarly,
javax.servlet.jsp.jstl.core.IteratorTagSupport lets you easily implement iteration tags. The handlers for the
<forTokens> tags extend this class and thus implement the
javax.servlet.jsp.jstl.core.IteratorTag interface, which provides a well-defined mechanism for iteration tags to communicate with custom subtags you may write.
There are a number of samples provided with the
standard-examples application that might prove helpful. Not only are samples of the tags provided, but there are also samples that induce errors, so that you can get familiar with what some of the errors mean. This application is part of the early access download. You can just drop the
standards-example.war file into the
webapps directory of Tomcat and access it in your browser at http://127.0.0.1:8080/standard-examples/.
What to Watch For
With the early access release of JSTL and the reference implementation provided on the Jakarta site, there is good reason to take a few minutes and explore what's going to be available. It remains to be seen how the expression language support will play out in the community. While some might welcome it, I can see how other people might feel that it is just introducing another way to have code still involved on your JSPs. The spec was scheduled to go into public review on March 12, 2002. The more developers who have a chance to play with the JSTL before the spec is final, the better it will suit the development community. So download JSTL, give it a spin, and go have yourself a good old custom tag time.
In the next article in this JSTL series, we'll look at the practical use of some of the tags. Stay tuned.
Read more JSP and Servlets columns.
Return to ONJava.com.
How to refer java variable inside tag
2008-11-14 05:09:10 arulman [View]
- Trackback from http://blog.chinoy.com/archive/0001/01/01/188.aspx
2004-10-22 20:37:44 [View]
JSP Tag Libraries
2004-06-10 06:47:04 fj_rodriguez [View]
importing rtexpression core taglib
2002-11-20 10:32:32 anonymous2 [View]
this article about JSTL is nice
2002-10-01 10:37:49 anonymous2 [View]