BBJSP Code Tags (Deprecated)

The BBJSP system is deprecated. For new development, use BBxServlet.


 

Description

A tag library provides the ability for developers to write BBj code to process tags found in BBJSP pages. These tags are not based upon BBJSP marup rather they are pure BBj classes that are executed when called upon in a page.

A tag library allows a developer to group together tags with related functionality. A tag library uses a tag library descriptor (TLD) file that describes the tag extensions and relates them to their classes. BBJSP uses the TLD to get information about the extensions. TLD files have the file extension .tld and are written in XML notation.

Identify the TLD in your BBJSP pages

To use a tag library within a page or a custom tag you should first import the taglib into the page with the taglib tag as follows:

<%@ taglib file='/WEB-CFG/tld/mytags.tld' prefix='mytag' %>

In doing so, you can now access the defined tag classes in your page as follows:

<prefix:tagname attribute='value' ... />

Where prefix was specified in the taglib tag and tagname is the name of the tag defined in the TLD file. Multiple attribtes='value' pairs should be added depending on the TLD declaration.

Optional attributes are defined in the calling page with their values. The values will be sent to the tag for processing. These attribute values can be and valid BBJSP data, including BBjVector of records or beans.

Here is a more real example of how a tag may be used:

<%
  declare BBjVector products!
  categoryId$=#getRequest().getParameter("category_id")
  products! = ProductLoader.loadProductsInCategory(categoryId$)
  layout$="list"
%><c:iterate data='<%= products! >' class='ProductBean' id='product!' >&lts:productTag product='product!' layout='layout$'/></c:iterate> 

Tag Library Descriptors

<taglib>  <tag name='code'>    <source>mytags/CodeTag.bbj</source>    <class>CodeTag</class>    <attribute>      <name>myAttributes</name>      <required>true</required>      <type>BBjString</type>    </attribute>    <attribute>      <name>dataVector</name>      <required>true</required>      <type>BBjVector</type>    </attribute>  </tag></taglib> 

Once defined, the BBJSP engine automatically generates the BBj code required to interact with the tag class at run-time.

Custom Tag Example

Let's say we want to show a number with formatting with commas and spaces. This can be very useful for user when the number is really long. So we want some custom tags like below:

<mytags:formatNumber number="100050.574" format="#,###.00"/> 

Create the BBj class file

Based on the number and format passed, it should write the formatted number in the page, for above example it should print 100,050.57

USE ::core/Utils.bbj::Utils
USE ::core/Tags.bbj::TagUtil
USE ::core/Tags.bbj::TagSupport

class public NumberFormatterTag extends TagSupport

  field public BBjString format
  field public BBjString number
  field public BBjspPageContext PageContext!

  method public int doStartTag()
    return TagSupport.getEVAL_BODY()
  methodend

  method public int doBodyTag()
    double amount = Double.parseDouble(number)
    DecimalFormat formatter = new DecimalFormat(format)
    String formattedNumber = formatter.format(amount)
    #PageContext().write(formattedNumber)
error:
    return TagSupport.getSKIP_BODY()
  methodend

  method public void doAfterBody()
  methodend

  method public void doEndTag()
  methodend
classend 

Create the Tag descriptor (TLD) entry

Once the tag handler class is ready, we need to define a TLD file inside the WEB-CFG directory so that the page generator will know how to load it.

<tag name='formatNumber' >  <source>NumberFormatterTag.bbj</source>  <class>NumberFormatterTag</class>  <content>empty</content>  <attribute>    <name>format</name>    <required>true</required>  </attribute>  <attribute>    <name>number</name>    <required>true</required>  </attribute></tag> 

Use the tag in a page

The tag can now be used by inserting it into your page as follows:

<mytags:formatNumber number="${customerAccount['Balance']}" format="$#,###.00"/>

Remarks

None.

See Also

BBJSP

Core Tag Library

BBJSP Tag Libraries

Simple Tags