For the standard tag library to be able to do all the wonderful things it claims to do, the tags will need to take parameters that are more complicated than such simple instructions as “yes” and “no.” In fact, the parameters to the standard tag library comprise a full language, although one that is significantly simpler than Java itself and much better suited for building pages.
This language is built into the very core of JSPs in the latest version of the JSP specification. This means that programmers creating new tags may use this language for their own purposes; this will also be illustrated in Chapter 13.
Expressions in this language are surrounded by braces and preceded by a dollar sign. The simplest kinds of expressions in the language are constants, such as strings or numbers:
${23} ${98.6} ${'hello'}
These expressions don’t mean anything on their own, but when used as the value of a parameter, they are evaluated by the expression language before they are sent to the tag. Because numbers and strings evaluate to themselves, this means that the following two expressions mean the same thing:
<awl:maybeShow show="${'yes'}"> <awl:maybeShow show="yes">
Note that within an expressions, literals are surrounded by single quotes and that the whole expression is surrounded by double quotes.
Errors to Watch For
If an expression is written incorrectly, such as leaving off a closing quote or a brace, a JSP page error will report something like
An error occurred while parsing custom action
Now for the fun part: The scripting language can also refer to beans and properties of beans. Listing 3.1 used a bean to display some static properties, including the seventh prime number. Suppose that bean were loaded into a page with this tag:
<jsp:useBean id="bean1" class="com.awl.jspbook.ch03.Bean1"/>
In that case, then the scripting language would refer to the seventh prime number property as
${bean1.seventhPrimeNumber}
Note the pattern: first, the name of the bean as defined in the jsp:useBean tag, then a dot, then the name of the property. This is not exactly equivalent to the jsp:getProperty tag, as dropping this script fragment into a page will not display the value. In fact, it will not do anything at all. However, this would serve perfectly as a way to send the seventh prime number to a custom tag. Admittedly, there would probably never be any need to do such a thing, but often it will be necessary to send a value from a form to a tag. We now have the means to do this: Send the form inputs into a bean with the jsp:setProperty tag and then send the value from the bean to a tag with a scripted parameter.
Errors to Watch For
If an attempt is made to access a property that does not exist, a page error that looks like the following will be generated:
Unable to find a value for "property" in object of class "beanClass"
Listing 4.3 shows a simple form that lets the user choose whether to show, hide, or reverse a block of text.
Listing 4.3 A form that will be used by a tag
<html> <body> <form action="show_result.jsp" method="post"> Shall I display the tag body? <select name="shouldShow"> <option>yes <option>no <option>reverse </select><br> <input type="Submit" name="Go" value="Go"> </form> </body> </html>
The page that will use this form is shown in Listing 4.4. It combines many of the things that have been discussed so far: a bean, the awl:maybeShow tag, and a scripted parameter.
Listing 4.4 Using a bean and a tag together
<%@ taglib prefix="awl" uri="http://jspbook.awl.com/samples" %> <jsp:useBean id="form" class="com.awl.jspbook.ch04.FormBean"/> <jsp:setProperty name="form" property="*"/> <awl:maybeShow show="${form.shouldShow}"> The time is: <awl:date format="hh:mm:ss MM/dd/yy"/> </awl:maybeShow>
The first portion of this example should be old hat by now: First, a tag library is loaded, and then a bean is obtained and fed the form values. The second part uses the tag almost exactly as in Listing 4.2. The only difference is that the show parameter is not a fixed value but comes from the bean via a script. Using a bean, a custom tag, and the scripting language, we can now dynamically control a whole block of text!