Call for New Batch / Any Query / Demo Classes : ☎ 08871209400 , 7987028543 , Zone-2,M.P.Nagar,Bhopal

Custom Tags with Attributes

A simple tag can have attributes. Attributes customize the behavior of a custom tag just as parameters customize the behavior of a method. There are three types of attributes:

● Simple attributes
● Fragment attributes
● Dynamic attributes

‣ Simple Attributes

Simple attributes are evaluated by the container before being passed to the tag handler. Simple attributes are listed in the start tag and have the syntax: attr="value". You can set a simple attribute value from a String constant, or an expression language (EL) expression, or by using a jsp:attribute element. The conversion process between the constants and expressions and attribute types follows the rules described for JavaBeans component properties in Setting JavaBeans Component Properties.
The first attribute, a reference to a book database object, is set by an EL expression. The second attribute, which sets the color of the rows in a table that represents the bookstore catalog, is set with a String constant.

<sc:catalog bookDB ="${bookDB}" color="#cccccc">

‣ Fragment Attributes

A JSP fragment is a portion of JSP code passed to a tag handler that can be invoked as many times as needed. You can think of a fragment as a template that is used by a tag handler to produce customized content. Thus, unlike a simple attribute which is evaluated by the container, a fragment attribute is evaluated by a tag handler during tag invocation.

To declare a fragment attribute, you use the fragment attribute of the attribute directive or use the fragment subelement of the attribute TLD element. You define the value of a fragment attribute by using a jsp:attribute element. When used to specify a fragme nt attribute, the body of the jsp:attribute element can contain only static text and standard and custom tags; it cannot contain scripting elements.
JSP fragments can be parameterized by means of expression language (EL) variables in the JSP code that composes the fragment.

The catalog tag discussed earlier accepts two fragments: normalPrice, which is displayed for a product that’s full price, and onSale, which is displayed for a product that’s on sale.


<sc:catalog bookDB ="${bookDB}" color="#cccccc">
    <jsp:attribute name="normalPrice">
        
 </jsp:attribute>
    <jsp:attribute name="onSale">
        <strike><fmt:formatNumber value="${price}"
            type="currency"/></strike>
<font color="red"><fmt:formatNumber value="${salePrice}" type="currency"/></font> </jsp:attribute> </sc:catalog>

The tag executes the normalPrice fragment, using the values for the price EL variable, if the product is full price. If the product is on sale, the tag executes the onSale fragment using the price and salePrice variables.

‣ Dynamic Attributes

A dynamic attribute is an attribute that is not specified in the definition of the tag. Dynamic attributes are used primarily by tags whose attributes are treated in a uniform manner but whose names are not necessarily known at development time.
For example, this tag accepts an arbitrary number of attributes whose values are colors and outputs a bulleted list of the attributes colored according to the values:

<colored:colored color1="red" color2="yellow" color3="blue"/>

Deferred Value

A deferred value attribute is one that accepts deferred value expressions, which are described in Value Expressions.

Deferred Method

A deferred method attribute is one that accepts deferred method expressions, which are described in Method Expressions.

‣ Dynamic Attribute or Deferred Expression

This kind of attribute can accept a String literal, a scriptlet expression, or an EL expression, including deferred expressions.

jsp:attribute Element

The jsp:attribute element allows you to define the value of a tag attribute in the body of an XML element instead of in the value of an XML attribute.

For example, the Duke’s Bookstore template page screendefinitions.jsp uses jsp:attribute to use the output of fmt:message to set the value of the value attribute of tt:parameter:

...
<tt:screen id="/bookcatalog">
    <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 a name attribute and a trim attribute. The name attribute identifies which tag attribute is being specified. The optional trim attribute determines whether or not white space appearing at the beginning and end of the element body should be discarded.

An empty body is equivalent to specifying "" as the value of the attribute.

• The body of jsp:attribute is restricted according to the type of attribute being specified:
• For simple attributes that accept an EL expression, the body can be any JSP content.
• For simple attributes that do not accept an EL expression, the body can contain only static text.
• For fragment attributes, the body must not contain any scripting elements.

References : oracle « Privious Post » « Next Post » share on :        :  

Love to hear your Views / Guidance / Recommendations on this Post…

More Programming Topics

Key Features of the Java LanguageJava is simpler than most other languages that are used to create server applications because of its consistent enforcement of the object model.Introduction To JavaThe following sections explain how Java technology is both a programming language and a platform and provide an overview of what this technology can do for youChaining Constructor using SuperIf a subclass constructor invokes a constructor of its superclass either explicitly or implicitly there will be a whole chain of constructors called all the way back to the constructor of Object.Multilevel Inheritance in JavaMultilevel Inheritance is said to have taken place when a child class inherits from a parent class and then child class becomes the parent class for another class.Interface Inheritance JavaAn Interface declaration consists of modifiers the keyword interface the interface name a comma separated list of parent interfaces and the interface body.Inheritance in JavaA class that is derived from another class is called a subclass. The class from which the subclass is derived is called a superclass. This process of inheriting properties from another class is called Inheritance.White Spaces in JavaWhite Space is a character in Java. A character satisfies to be a White Space if and only if it is a Unicode space character.Serialization and Deserialization JavaTo serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object.Implementations in Collection FrameworkImplementations are the data objects used to store collections, which implement the interfaces described in the Interfaces section.

Explore the Technology World

An introduction to YouTube's new designThe Challenge Facing YouTube’s Design and Interaction Team When They Launched the Redesign Two Years Ago Was How to Tie Together a Host of Products with Very Different Audiences and UsesSpace Station Crew Member Discusses His First Days in Space with Minnesota StudentsAboard the International Space Station, Expedition 53 Flight Engineer Mark Vande Hei of NASA fielded questions about life and work on the orbital outpost during an in-flight educational event Sept. 18 with students from the Pine River-Backus schools in Pine River, MinnesotaSpace Station Crew Members Discuss Life in Space with the MediaAboard the International Space Station, Expedition 53 Flight Engineers Mark Vande Hei and Joe Acaba of NASA and Paolo Nespoli of the European Space Agency discussed life and work aboard the orbital complex in a pair of in-flight interviewsBeyond Mobile Server Games with Visual Studio for MacYou know that Visual Studio for Mac does Xamarin, but do You know what Else? We'll take a look at using Visual Studio for Mac for Mobile, Web, Game, Server less and IoT Development, and Connecting them with .NET Standard 2.0An introduction to YouTube's new designhe Challenge Facing YouTube’s Design and Interaction Team When They Launched the Redesign Two Years Ago Was How to Tie Together a Host of Products with Very Different Audiences and UsesAndroid Oreo Safer Than EverProtecting you from harm’s way. Android Oreo is more Secure than Ever.With Google Play Protect Built in, You’ll have Peace of Mind in the Palm of Your Hands

Call / Visit for New Batch

Services

Why Join Us

  • ➯ Free Demo Classes
  • ➯ No Registration Fee
  • ➯ Interview Questions
  • ➯ Study Materials
  • ➯ Softwares
  • ➯ Aptitude & Reasoning
  • ➯ Placement Assitance

Address

  • MyJava Training Institute ,
  • Plot No.243 , shop No 14 ,
  • Secnd Floor , Zone-II ,
  • M.P.Nagar , Bhopal,
  • Madhya Pradesh ( 462011 )
  • ☎  08871209400 , 7987028543
  • 🌐 www.myjava.in
FacebookGoogle +TwitterYouTube

MyJava - java Training Institute