Snippets as Struts taglibrary attribute

Author: Kasper B. Graversen, 14/05/07
Keywords: Struts, Struts Attributes, Scriptlets, Struts Dynamic attributes.
Abstract: Struts does not allow you to mix text and scriptlets in most attributes. This guide shows you how to make "dynamic attributes".
subscribe to my RSS feed


Bookmark and Share


Snippets as Struts taglibrary attribute

Often when you are combining presentation of collections of data with javascript, you may wish to take actions on the individual line in that collection. While in the below code Struts allows you to set the styleId attribute with a mix of text and scriptlet, it does not for the onchange attribute. Whatever is inside the onchange attribute (and many other) are simply not evaluated by the JSP engine and hence you'll have useless HTML such as <input type="text" onchange="<%=row%>" ... />. Such HTML could be produced by code such as below:

Solution


<logic:iterate indexId="row" ...
<html:text styleId='<%="quantity_" + row%>' onchange="updateRow(<%=row%>)" indexed="true" ...   />   // ILLEGAL CODE!!
...

Fortunately, there is a hack around it which is to have only a scriptlet as the value for the onchange attribute.

<logic:iterate indexId="row" ...
<% String hack = "updateRow('"+row+"');"; %>
<html:text styleId='<%="quantity_" + row%>' onchange="<%=hack%>" ...  />
...



Comments

If you have any comments to this article, please drop me a mail at firstclassthoughts at gmail dot com please indicate if I can't publish whole or parts of your comment on the site.


If you like this site consider subscribing to my RSS feed or how about subscribing by Email.


Help spread the word

Share this post on your favorite social bookmarking sites:
If you enjoyed this article, found it thought provoking, educative or otherwise good, please link to this page from your page or social bookmarking page. If you have any texts you think are worth publishing on First Class Thoughts, don't hesitate to send me a mail! Quality always welcome.


Bookmark and Share


The most recent contributions
28/07/09 Magic in mathematics II Fun with the number cyclic numbers, and specifically with 142857 as it is the smallest of such numbers.
13/07/09 My top 8 time-saving Firefox shortcuts This article presents my favorite top 8 time-saving shortcuts in Firefox 3.0 and Firefox 3.5. Get to know these and you'll be saving a lot of time. They have been ordered by "the element of most surprise"
20/05/09 Board Game Jungle speed / Arriba Review of the cool game "Jungle Speed" aka. "Arriba".
16/05/09 Danish Twin words "Twin words" are words that not only have multiple meanings, they must be composed next to each other in meaningful sentences. This article explores the concept of twin words.
Nothing of interest? Try browsing the entire article archive...