wood burning stoves 2.0*
The moose likes JSP and the fly likes Enumeration of values for an attribute for a custom tag? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Enumeration of values for an attribute for a custom tag?" Watch "Enumeration of values for an attribute for a custom tag?" New topic
Author

Enumeration of values for an attribute for a custom tag?

Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Hi,
I wonder if anyone can help. I am creating a small library of JSP tags, and am struggling with the TLD - in particular specifying a fixed set of possible values for a given attribute.

http://java.sun.com/products/jsp/tutorial/TagLibraries11.html#62167 tells me that the format for the attribute element is:


This is fine, and I have a TLD with this in it:


This works fine, but what I really want the TLD to describe is that the layoutCol1 attribute can have one of the values label or field or helpicon

I've read that that I can do this at runtime by having a class that extends TagExtraInfo and writing the isValid method. But what I'd really like to do is do it in the TLD - that after all should describe my tag and all of its attributes?

Any pointers gratefully received;

Cheers

Nathan


Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1628
    
  23

Hi Nathan,

There is an option of giving a "type" attribute in the attribute element.
check: SUN tutorial

However I don't know whether you can use an enum there...
Regards,
Frits
Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
Thanks Frits, but I dont think the type attribute is what I want (I think it is for when you allow expressions as values for your custom attribute you can say what object type the expression return should be)

There is an interesting line in the page I originally posted - in the section Attribute Validation, the first line reads:
The documentation for a tag library should describe valid values for tag attributes. When a JSP page is translated, a JSP container will enforce any constraints contained in the TLD element for each attribute.

Does anyone know what documentation they refer to? The way I am reading this is that there are some documentation tags which I should include in my TLD which (amongst other things) can describe the valid option values for an attribute?

Cheers

Nathan
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1628
    
  23

Hi Nathan,

Just an small example that uses an enum as an attribute:
My small implementation consists of the following files:
  • enum definition
  • TLD
  • Servlet
  • JSP
  • TagHandler

  • defining the enum

    the TLD entry only allowing an enum for the attribute layoutCol1:

    The Servlet code:

    The jsp:

    The TagHandler code:

    The queryString contains the parameter "anOption", and I am using the URL that maps to my servlet with the queryString "?anOption=label"
    The output on the page is:
    doStartTag LABELdoEndTag LABEL


    There is no way you can put a String or other object than an LayoutCol enum as an attribute to your Tag.
    Does this answer your question?

    Regards,
    Frits
     
     
    subject: Enumeration of values for an attribute for a custom tag?
     
    Similar Threads
    how to pass Parameter in custom tag
    Problem while Tag lib Up gradation
    weblogic error
    Problems making Struts/JSTL 1.2 site work with JSTL 2.0
    to output the custom tag's attribute..