This week's book giveaway is in the Object Relational Mapping forum. We're giving away four copies of Pro JPA 2: Mastering the Java Persistence API and have Mike Keith and Merrick Schincariol on-line! See this thread for details.
In a separate topic, Satish had noticed that there was a topic (Dynamic Attributes) that didn't make it into the core text of HFS/J. I promised that I would write up a brief tutorial on this topic. And here it is...
Before I start, I want to say that I could not find a way to upload the code example that I will be describing. If you would like my source and WAR file, please send me a private message at b_basham@yahoo.com.
Dynamic attributes is a feature of custom tags that allow the JSP author to include any number of attributes on a tag. The canonical use of this feature is to allow a tag developer to mimic (but augment) standard HTML tags. My example follows this usage. I have developed a custom tag to generate a drop-down list (the <select> tag) that automatically generates the set of <option> tags from an application list object.
The webapp has three views. The Home page (index.html) has a link to a web form. The link is to the URL 'goToForm.do' which executes a servlet that sets up a List of java.awt.Color objects in the session-scope and forwards to the 'form.jsp' page. This is the page that uses the <formTags:select> custom tag which is the focus of this tutorial. The user must type in some text in the text field and select a color from the drop-down list and then submit the form. The form action is 'processForm.do' which executes another servlet that validates the user's input and either returns to the form (if the data fails validation) or forwards to the 'results.jsp' page which merely displays the data.
OK, let's get to the meat of this tutorial. Here is the code for the form.jsp page:
The first line declares the use of my custom tag library. The <formTags:select> tag is my custom tag which generates the HTML <select> tag and its <option> subtags. The set of option is declared by the 'optionsList' attribute which must take a java.util.List object. Here I am using the 'colorList' session-scoped attribute, which was setup by the goToForm.do servlet. Notice also that this custom tag has two HTML-specific attributes: 'name' and 'tabindex'. These attributes must be sent to the rendered page verbatim.
Here is what is rendered:
The next step of this tutorial is to discuss how the <formTags:select> custom tag is declared in the TLD. Here is the relevant chunk of the TLD:
Notice that I have only declared a single attribute 'optionsList'. The <dynamic-attributes>true</dynamic-attributes> element declares that this custom tag may also allow any other attributes that the user enters.
The last piece of the Dynamic Attributes puzzle is the tag handler itself. I choose to implement this as a "Simple" tag, but it could also be done as a "Classic" tag. Let me start with a code skeleton for this tag hanlder:
The <formTags:select> handler is the FormSelectTagHandler class. This class extends SimpleTagSupport and implements the DynamicAttributes. This interface introduces the setDynamicAttribute(uri, localName, value) method which allows the tag handle to store any number of attributes used by the JSP page.
The first method setOptionsList stores the 'optionsList' attribute which is unique to my custom tag (it is not an HTML attribute for the <select> tag). This method must exist because I have declared the 'optionsList' attribute in the TLD.
The second method setDynamicAttribute stores any attributes other than those declared in the TLD. In our form.jsp page, the 'name' and 'tabindex' attributes will be stored in the tag object using this method. The localName parameter is the name of the tag and the value is an Object (usually a String) that was passed in as the value of the attribute. The uri parameter is only used when the JSP is a "JSP Document" (an XML document) which may use multiple namespaces. This is rare (and not on the SCWCD exam) so I will not go into any detail on it; for most purposes you can safely ignore this parameter. The most common implementation of this method is to store the localName and value paratemers in a Map object to be used when the HTML is generated.
The last method doTag is responsible for generating the actual HTML content for this custom tag. There are three tasks for this method. First, it must generate the <select> start tag. This task must include all of the HTML-specific (dynamic) attributes stored in the Map describe above. Second, it must generate the set of <option> tags from the optionsList supplied by the JSP author. Third, it must generate the </select> end tag.
Here is the full code (minus package and import statements) for this tag handler:
OK, that's it. I hope that you enjoyed (and learned) from this tutorial. Sorry if it wasn't very head-firsty, but the limitations of HTML prevents a more graphical view of these concepts.
Thank you for the tutorial and thank you for writing a really good book. I am currently studyiong for my SCWCD, should probably give it in a few weeks.... I would really appreciate if yo could send me a copy of the source code and WArs for Dynamic Attribute examples. My address is sumita.padiyar@gmail.com.
I appreciate that you have discovered this tutorial, but please understand that this is an old thread (from September of 2005) and I don't often return to view the additional posts. Please forgive me if I don't respond to requests in this thread. However, you are always welcome to email me directly at b_basham@yahoo.com.
I have a doubt i checked the code that you put here. But in this code i can't find anything that uses the dynamic attributes. Because if i execute the code, then the result is same with or without dynamic attributes.I think the tag handler class using only static attribute optionList.
Please go through the annotations....usage is explained
The second method setDynamicAttribute stores any attributes other than those declared in the TLD. In our form.jsp page, the 'name' and 'tabindex' attributes will be stored in the tag object using this method. The localName parameter is the name of the tag and the value is an Object (usually a String) that was passed in as the value of the attribute. The uri parameter is only used when the JSP is a "JSP Document" (an XML document) which may use multiple namespaces. This is rare (and not on the SCWCD exam) so I will not go into any detail on it; for most purposes you can safely ignore this parameter. The most common implementation of this method is to store the localName and value paratemers in a Map object to be used when the HTML is generated.
Sayak i know the usage of dynamic attribute. But i told here is that the example is not good enough to show the perfect usage of dynamic attribute.If he put that dynamic attributes in the select option list that will make clear to everyone. Am i right?
SCJP 5.0<br />SCWCD 1.4<br />Preparing for <b>SCEA</b>.<br /><b>"I prefer an interesting vice to a virtue that bores."</b>
Originally posted by Sreeraj G H: Sayak i know the usage of dynamic attribute. But i told here is that the example is not good enough to show the perfect usage of dynamic attribute.If he put that dynamic attributes in the select option list that will make clear to everyone. Am i right?
Here is the chunk of code in the FormSelectTagHandler that inserts the dynamic attribues into the open tag:
Is this statement OK. I think if we have both static and dynamic attributes for a tag, the static attributes need to come first. And here I think name is a dynamic attribute.
And one more question, Can we have EL to set the values for dynamic attributes??
One more thing, is there some version compatibilty regarding the use of runtime expressions for dynamic atrributes.??? Or the runtime expressions have been always applicable to dynamic strributes. Please confirm that.
As I have read it somewhere "If you try to use EL to set dynamic attributes, you will get an error". Not able to recollect the reference. Whenever come across to that again, will let you know.
One more thing, is there some version compatibilty regarding the use of runtime expressions for dynamic atrributes.??? Or the runtime expressions have been always applicable to dynamic strributes. Please confirm that.
I dno't know why I was asking for your container version, and I don't know why you're asking that either. As far as I know, dynamic attributes were introduced in JSP2, so there should not be any problem as long as you're using a container supporting JSP2. Dynamic attributes expect runtime expressions from the beginning.
And somewhere I read "If you try to use EL to set dynamic attributes, you will get an error" I am sure it's written there. As dynamic attributes don't have <rtexprvalue> option.
I read through your thread with Christophe and it was not clear to me that one of your questions was answered. You had a question about the code snippet:
which returned:
This result makes sense to me because the 'min' attribute is set first before the 'num' attribute. Therefore, the this.num instance variable is zero and the Math.min of 0 and 6 (from ${var2}) is still 0.
The order that tag attributes are set is based upon the order that the attributes appear in the tag itself.
Nice to have something fom you. I got your point. Since we don't have any value for the num by the time val variable is set for min, we get 0 for num, which is the default value for num.
Thanks a lot Bryan Hope we will keep listening from you in future as well.
About the reference of where does it says that dynamic attributes doesn't accept EL, you can see it on the "SCWCD exam study kit, Second Edition" book from manning. The exact quote says using EL on dynamic attributes shows an error, although it works when you try that.
Hello everyone, I went through the tutorial on dynamic attributes. My understanding is that it allows The JSP author to enter attributes while writing a JSP without defining them in a TLD. In your tutorial "name" and "tabindex" were the dynamic attributes which are not defined in the TLD.It means I can add another dynamic attribute "attri" if I want to.
Am I correct. Please correct me if I am mistaken.
Thanks Bryan for the tutorial.
If you ask me anything I don't know, I'm not going to answer.<br />--Yogi Berra
You are correct. You can add any tag attribute and it will be handled by the setDynamicAttribute() method. Note that no validation is possible that you have typed the attribute name correctly. For example, if you want to add the 'onchange' JavaScript attribute but you mistype it as 'onchnge' then this attribute is passed on to the generated HTML <select> tag. When the browser renders the <select> tag it will not understand the 'onchnge' attribute and will just ignore it.
If you need to validate the attribute names, then you will have to add code to perform this validation in the setDynamicAttribute() method.