When building a Struts2 application recently, I needed to add arbitrary parameters to a URL when creating the menu from dynamic content. The parameters were stored in a map, so I used the my standard bit of code for iterating over a map:
<s:url var="url" action="%{link}" > <s:iterator value="parameters.keySet()" var="key"> <s:param name="%{key}" value="%{parameters.get(#key)}"/> </s:iterator> </s:url>
…and nothing happened. No parameters at all appeared in the URL.
Odd.
I got rid of the iterator and used a single parameter, just to check:
<s:url var="url" action="%{link}" > <s:param name="test-name" value="test-value"/> </s:url>
That worked fine. One quick debugging session later and I found the problem – the Struts2 org.apache.struts2.components.Param component parameterises its parent component. In this case, the parent component is an iterator and so it was absorbing the parameters and they were never getting as far as the URL.
I couldn’t find a way to do what I needed the core Struts2 components and tags and so I created my own.
IterableParam overrides Param’s findAncestor method to return the grandparent component in the case where the parent is an Iterator:
<s:url var="url" action="%{link}" > <s:iterator value="parameters.keySet()" var="key"> <ob:iterable-param name="%{key}" value="%{parameters.get(#key)}"/> </s:iterator> </s:url>
Result – works as required.
Despite the title of this blog entry, any Struts2 component that can be parameterised using the <s:param> tag can be parameterised using <ob:iterable-param>.
You can get the new tag and component from
- binary: objectify-struts2-tags-1.0.jar
- source: objectify-struts2-tags-1.0-src.zip
Hi,
I am kind of experiencing same problem. Please let me know do we need to create a tld file and create an entry for this customized tag.
If yes, please send me the details as above thing is not working for me.
Hi,
I am kind of experiencing same problem. Please let me know do we need to create a tld file and create an entry for this customized tag.
If yes, please send me the details as above thing is not working for me.
Hi,
If you can mail me the entire solution, it will be great. I also looked at following link for help :
http://stackoverflow.com/questions/872375/struts2-adding-parameters-from-a-map-to-a-url-tag
Please email me the complete solution at azaveri7@gmail.com
The TLD is defined in the jar, so you just need to reference it in your webapp. You can then use the ob:iterable-param tag just like you would any other tag.
I am getting errors like :
The method get(Class) in the type TagHandlerPool is not applicable for the arguments (Class)
The method setPageContext(PageContext) is undefined for the type IterableParam
The method setParent(Tag) is undefined for the type IterableParam
Please help me out on this
This is something I wrote 5 years ago, which is the last time I used Struts 2. Off the top of my head, I imagine you’re using a much later version of Struts 2 – your best bet is to take the code that I wrote (linked at the bottom of the blog post) and update it for your version.
Ok Steve. I will try and let you know if I resolve this.