2. Using the Tutorial Examples 3. Getting Started with Web Applications 5. JavaServer Pages Technology 7. JavaServer Pages Standard Tag Library Encapsulating Reusable Content Using Tag Files Declaring Tag Attributes in Tag Files Declaring Tag Variables in Tag Files Evaluating Fragments Passed to Tag Files Simple and Fragment Attribute and Variable Example Top-Level Tag Library Descriptor Elements Declaring Tag Attributes for Tag Handlers Declaring Tag Variables for Tag Handlers Programming Simple Tag Handlers Including Tag Handlers in Web Applications How Is a Simple Tag Handler Invoked? Tag Handlers for Tags with Attributes Defining Attributes in a Tag Handler Setting Deferred Value Attributes and Deferred Method Attributes Tag Handlers for Tags with Bodies Tag Handler Does Not Manipulate the Body Tag Handlers for Tags That Define Variables 10. JavaServer Faces Technology 11. Using JavaServer Faces Technology in JSP Pages 12. Developing with JavaServer Faces Technology 13. Creating Custom UI Components 14. Configuring JavaServer Faces Applications 15. Internationalizing and Localizing Web Applications 16. Building Web Services with JAX-WS 17. Binding between XML Schema and Java Classes 19. SOAP with Attachments API for Java 21. Getting Started with Enterprise Beans 23. A Message-Driven Bean Example 24. Introduction to the Java Persistence API 25. Persistence in the Web Tier 26. Persistence in the EJB Tier 27. The Java Persistence Query Language 28. Introduction to Security in the Java EE Platform 29. Securing Java EE Applications 31. The Java Message Service API 32. Java EE Examples Using the JMS API 36. The Coffee Break Application | Types of TagsSimple tags are invoked using XML syntax. They have a start tagand an end tag, and possibly a body: <tt:tag>body</tt:tag> A custom tag with no body is expressed as follows: <tt:tag /> or <tt:tag></tt:tag> Tags with AttributesA simple tag can have attributes. Attributes customize the behavior of a customtag just as parameters customize the behavior of a method. There are threetypes of attributes:
Simple AttributesSimple attributes are evaluated by the container before being passed to the taghandler. Simple attributes are listed in the start tag and have the syntaxattr="value". You can set a simple attribute value from aString constant, or anexpression language (EL) expression, or by using ajsp:attribute element (seejsp:attribute Element). Theconversion process between the constants and expressions and attribute types follows the rules describedfor JavaBeans component properties inSetting JavaBeans Component Properties. The Duke’s Bookstore pagetut-install/javaeetutorial5/examples/web/bookstore3/web/bookcatalog.jsp calls thecatalog tag, which has twoattributes. The first attribute, a reference to a book database object, is setby an EL expression. The second attribute, which sets the color of therows in a table that represents the bookstore catalog, is set with aString constant. <sc:catalog bookDB ="${bookDB}" color="#cccccc">Fragment AttributesAJSP fragment is a portion of JSP code passed to a taghandler that can be invoked as many times as needed. You can thinkof a fragment as a template that is used by a tag handlerto produce customized content. Thus, unlike a simple attribute which is evaluated bythe container, a fragment attribute is evaluated by a tag handler during taginvocation. To declare a fragment attribute, you use thefragment attribute of theattributedirective (seeDeclaring Tag Attributes in Tag Files) or use thefragment subelement of theattribute TLD element (seeDeclaring Tag Attributes for Tag Handlers). Youdefine the value of a fragment attribute by using ajsp:attribute element. Whenused to specify a fragment attribute, the body of thejsp:attribute elementcan contain only static text and standard and custom tags; itcannot containscripting elements (seeChapter 9, Scripting in JSP Pages). JSP fragments can be parameterized by means of expression language (EL) variables inthe JSP code that composes the fragment. The EL variables are set bythe tag handler, thus allowing the handler to customize the fragment each timeit is invoked (seeDeclaring Tag Variables in Tag Files, andDeclaring Tag Variables for Tag Handlers). Thecatalog tag discussed earlier accepts two fragments:normalPrice, which is displayedfor a product that’s full price, andonSale, which is displayed for aproduct that’s on sale. <sc:catalog bookDB ="${bookDB}" color="#cccccc"> <jsp:attribute name="normalPrice"> <fmt:formatNumber value="${price}" type="currency"/> </jsp:attribute> <jsp:attribute name="onSale"> <strike><fmt:formatNumber value="${price}" type="currency"/></strike><br/> <font color="red"><fmt:formatNumber value="${salePrice}" type="currency"/></font> </jsp:attribute></sc:catalog>The tag executes thenormalPrice fragment, using the values for theprice ELvariable, if the product is full price. If the product is on sale,the tag executes theonSale fragment using theprice andsalePrice variables. Dynamic AttributesAdynamic attribute is an attribute that is not specified in the definition ofthe tag. Dynamic attributes are used primarily by tags whose attributes are treatedin a uniform manner but whose names are not necessarily known at developmenttime. For example, this tag accepts an arbitrary number of attributes whose values arecolors and outputs a bulleted list of the attributes colored according to thevalues: <colored:colored color1="red" color2="yellow" color3="blue"/> You can also set the value of dynamic attributes using an ELexpression or using thejsp:attribute element. Deferred ValueAdeferred value attribute is one that accepts deferred value expressions, which are described inValue Expressions. Deferred MethodAdeferred method attribute is one that accepts deferred method expressions, which are described inMethod Expressions. Dynamic Attribute or Deferred ExpressionThis kind of attribute can accept a String literal, a scriptlet expression, oran EL expression, including deferred expressions. jsp:attribute ElementThejsp:attribute element allows you to define the value of a tag attributein thebody of an XML element instead of in the value ofan XML attribute. For example, the Duke’s Bookstore template pagescreendefinitions.jsp usesjsp:attribute to usethe output offmt:message to set the value of thevalue attributeoftt:parameter: ...<tt:screen> <tt:parameter name="title" direct="true"> <jsp:attribute name="value" > <fmt:message key="TitleBookCatalog"/> </jsp:attribute> </tt:parameter> <tt:parameter name="banner" value="/template/banner.jsp" direct="false"/> <tt:parameter name="body" value="/bookcatalog.jsp" direct="false"/></tt:screen>... jsp:attribute accepts aname attribute and atrim attribute. Thename attribute identifies whichtag attribute is being specified. The optionaltrim attribute determines whether or notwhite space appearing at the beginning and end of the element body shouldbe discarded. By default, the leading and trailing white space is discarded. Thewhite space is trimmed when the JSP page is translated. If a bodycontains a custom tag that produces leading or trailing white space, that whitespace is preserved regardless of the value of thetrim attribute. An empty body is equivalent to specifying"" as the value of theattribute. The body ofjsp:attribute is restricted according to the type of attribute beingspecified:
Tags with BodiesA simple tag can contain custom and core tags, HTML text, and tag-dependentbody content between the start tag and the end tag. In the following example, the Duke’s Bookstore application pagetut-install/javaeetutorial5/examples/web/bookstore3/web/bookshowcart.jsp uses theJSTLc:if tag to print the body if the request contains a parameternamedClear: <c:if test="${param.Clear}"> <font color="#ff0000" size="+2"><strong> You just cleared your shopping cart! </strong><br> <br></font></c:if>jsp:body ElementYou can also explicitly specify the body of a simple tag by usingthejsp:body element. If one or more attributes are specified with thejsp:attributeelement, thenjsp:body is the only way to specify the body ofthe tag. If one or morejsp:attribute elements appear in the bodyof a tag invocation but you don’t include ajsp:body element, the taghas an empty body. Tags That Define VariablesA simple tag can define an EL variable that can be used withinthe calling page. In the following example, theiterator tag sets the valueof the EL variabledepartmentName as it iterates through a collection of department names. <tlt:iterator var="departmentName" type="java.lang.String" group="${myorg.departmentNames}"> <tr> <td><a href="list.jsp?deptName=${departmentName}"> ${departmentName}</a></td> </tr></tlt:iterator>Communication between TagsCustom tags communicate with each other through shared objects. There are two typesof shared objects: public and private. In the following example, thec:set tag creates a public EL variable calledaVariable, which is then reused byanotherTag. <c:set var="aVariable" value="aValue" /><tt:anotherTag attr1="${aVariable}" />Nested tags can share private objects. In the next example, an object createdbyouterTag is available toinnerTag. The inner tag retrieves its parent tagand then retrieves an object from the parent. Because the object is notnamed, the potential for naming conflicts is reduced. <tt:outerTag> <tt:innerTag /></tt:outerTag> The Duke’s Bookstore pagetut-install/javaeetutorial5/examples/web/bookstore3/web/template/template.jsp uses a set of cooperating tags thatshare public and private objects to define the screens of the application. Thesetags are described inA Template Tag Library. Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |