Giter Site home page Giter Site logo

struts2-iterable-params's Introduction

From an old, old blog post on my blog

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.

struts2-iterable-params's People

Contributors

schaloner avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.