Question

I'm trying to use custom data attributes of HTML in Struts2 tags here is my sample code

<s:select list="myList" listKey="myListVal"  listValue="myListDesc"  data-inputs="myListInput" ></s:select>

i was expecting something like this for example

<select >
     <option value="myListVal1" data-inputs="myListInput1">myListDesc1</option>
     <option value="myListVal2" data-inputs="myListInput2">myListDesc2</option>
     <option value="myListVal3" data-inputs="myListInput3">myListDesc3</option>
</select>

instead I'm getting this

<select data-inputs="myListInput" >
     <option value="myListVal1" >myListDesc1</option>
     <option value="myListVal2" >myListDesc2</option>
     <option value="myListVal3" >myListDesc3</option>
</select>

Is it possible to describe data-attribute in struts select tags for Options inside it.

Was it helpful?

Solution

Override the <s:select> tag template. Or just use HTML tags with <s:iterator>

<select name="list">
   <s:iterator value="myList" status="stat">
      <option value="<s:property value="myListVal"/>" data-inputs="myListInput<s:property value="#stat.index"/>"><s:property value="myListDesc"/></option>
   </s:iterator>
</select>

OTHER TIPS

You can't inject custom attributes into a Struts2 UI Tag directly.

According to Dave Newton's comment, you can with Struts2 >= 2.1.x

But still it's not possible to apply them to the option elements instead of the select, so I'll leave the answer in case you need to extend the original select tag to define a custom behaviour (like apply certain attributes to the options).


You can extend the <s:select> Struts2 tag to allow it to manage new kind of attributes...: http://bodez.wordpress.com/2009/03/13/customising-struts2-jsp-tags/ ,

or create your own tag directly, but in your case would be overkill: http://joshuajava.wordpress.com/2008/12/27/creating-custom-components-with-struts-2/).

Last but not least, you could even add your custom attributes once the page is rendered, using something like jQuery (demo: http://jsfiddle.net/CLNDs/ ); they will be accessible, but not visible in source.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top