2

我正在尝试在 Struts2 标记中使用 HTML 的自定义数据属性,这是我的示例代码

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

例如,我期待这样的事情

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

相反,我得到了这个

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

是否可以在其中的选项的 struts 选择标签中描述数据属性。

4

2 回答 2

5

覆盖<s:select>标签模板。或者只使用 HTML 标签<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>
于 2012-11-19T17:06:23.853 回答
2

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.

于 2012-11-19T15:21:40.297 回答