Search code examples
javaspringjspjsp-tagsapache-tiles

Do logic on apache tile attributes


In my apache tiles config file, I have a section like this:

<definition name="admin/*/*" extends="adminLayout">
    <put-attribute name="key" cascade="true" value="{1}" />
</definition>

Then, in my JSP, I am would like to do some logic on the tile attribute. Something like:

<c:if test="${key == 'value'}">
    // do something
</c:if>

where key comes from the tile attribute.

How can I access this tile attribute inside the expression language?

I have tried

<c:set value="<tiles:insertAttribute name='key'/>" var="theKey"></c:set>

and

<c:if test="${<tiles:insertAttribute name='key'/> == 'value'}">

and both times the raw tiles xml is used as the comparing string - it doesn't get replaced by the attribute.


Solution

  • Use Tiles extras:

    <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
        xmlns:c="http://java.sun.com/jsp/jstl/core"
        xmlns:tiles="http://tiles.apache.org/tags-tiles"
        xmlns:tilesx="http://tiles.apache.org/tags-tiles-extras"
        version="2.0">
    
        <tilesx:useAttribute id="keyJspVariable" name="key" classname="java.lang.String" />
    
        <c:if test="${keyJspVariable== 'value'}">
           // do something
        </c:if>
    

    Documentation example: https://tiles.apache.org/framework/tutorial/advanced/list-attributes.html