O'Reilly logo

JavaServer Pages, Second Edition by Hans Bergsten

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required