Announcement Announcement Module
No announcement yet.
spring:bind with property name known only at runtime Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring:bind with property name known only at runtime

    Hello all,

    I need to use spring:bind with variable property name at runtime. like in JSTL, you can do this:

    <c:forEach items="${props}" var="d">
    <td><c:out value="${entry[d.propName]}"/></td>

    I would like to have the same functionalties in spring:bind since I do not know the property name until the runtime. So I would like to have this:

    <spring:bind path="command[d.propName]">

    looks like this is not supported.

    how do I workaround this? Any body did this before?

    Thanks a lot.

    Happy new year!


  • #2
    If you're using JSP 2.0, you can use JSTL EL expressions in the path attribute of the bind tag:
    <spring&#58;bind path="command&#91;$&#123;d.propName&#125;&#93;">
    Note that EL expressions are evaluated by the JSP engine, while the "property path" syntax is evaluated by the Spring BeanWrapper implementation.

    The beanwrapper currently only supports String values as keys in to a Map, so the code example may not work, depending on the class of command.


    • #3
      You cant with spring use your command object as a map, the command object need to be a bean, with properties. To solve your problems, you may want to create a property "properties" in your command bean of type map.

             class MyCommandClass &#123;
                    Map properties = new HashMap&#40;&#41;;
                    public Map getProperties&#40;&#41; &#123;
                        return properties; 
                    public void setProperties&#40;Map hash&#41; &#123;
                          properties = hash; 

      Then during runtime, you can fill this map, with properties name and values, you will be able to access it in spring with :

      <spring&#58;bind path="properties&#91;$&#123;d.propName&#125;&#93;">
      or if command, is the name of your command object
      <spring&#58;bind path=";$&#123;d.propName&#125;&#93;">


      • #4
        Thanks a lot, olivier and plethora,

        I am using JSp2.0 and my object is a list. so I tried this and it does work now in case somebody else has the same issue.

        <c:set var="fldname">command.<c:out value="${f.propName}"/></c:set>

        <spring:bind path="${fldname}">

        in JSTl, the [] and dot are the same thing in retrieving the property value. the [] gives the flexibility of dynamic property. Hope spring will support this later.