Dynamic Attribute Values and Types

You may recall from Chapter 15 that to set a custom action attribute to a value determined at request time, you must use a request-time attribute value (a JSP expression), unless the custom action tag handler is designed to accept EL expressions. We look at how to handle EL expressions in the next chapter, but here we take a look at how request-time attribute values work.

To let a custom action accept a request-time attribute value, all you have to do is declare this fact in the TLD:

<tag>
  <name>geekContestEntry</name>
  <tag-class>com.xmp.GeekContextEntry</tag-class>
  <description>
    Saves the submitted data in the Geek Contest database.
  </description>
  
  <attribute>
    <name>yearsSinceLastVacation</name>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
    <name>hoursWithoutSleep</name>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
    <name>employersInAMonth</name>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
  ...
</tag>

An <rtexprexprvalue> element with the value true enables this feature. You can then assign dynamic values to the attributes in a page like this:

<xmp:geekContestEntry
  yearsSinceLastVacation='<%= request.getParameter("noVacation") %>'
  hoursWithoutSleep='<%= request.getParameter("noSleep") %>'
  employersInAMonth='<%= request.getParameter("employers") %>'
  ...
/>

In the tag handler, each action attribute is implemented as a property setter method that takes one argument (the value), in accordance with the JavaBeans conventions. As ...

Get JavaServer Pages, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.