Search code examples
springspring-roojsp-tags

tagx files: c:set tag, access to a property of an object


I'm using Spring Roo, and I have changed the list.tagx file to add a title at the top. This title is the value of a property of the first item. The problem is that I want to specify this property in a generic way, as an attribute of the list.tagx. Something like that:

<jsp:directive.attribute name="titleValue" type="java.lang.String"
        required="false" rtexprvalue="true"
        description="The value to be shown in the title" />
   ...
   <c:when test="${not empty items}">
      <c:if test="${not empty titleValue}">
         <c:set var="variable" value="${items[0].titleValue}" />
      </c:if>
   ...

There is a problem, because it tries to get the value of a property called 'titleValue' into the object items[0]. For instance, if I set the value of 'titleValue' as 'firstName', I want to get the value of items[0].firstName

Is it possible to do that?

Thanks in advance...


Solution

  • it is possible by using the spring:eval tag from the namespace xmlns:spring="http://www.springframework.org/tags". Try this:

    <jsp:directive.attribute name="titleValue" type="java.lang.String"
            required="false" rtexprvalue="true"
            description="The value to be shown in the title" />
       ...
       <c:when test="${not empty items}">
          <c:if test="${not empty titleValue}">
             <c:set var="variable">
                 <spring:eval expression="items[0].${titleValue}" />
             </c:set>
          </c:if>
       ...
    

    EDIT: fixed typo, sorry. It wasn't ${items[0]}.${..} but items[0].${..}