javaspringjsptiles-3

Dynamic attributes with apache tiles 3


I'm currently working on a Spring MVC project and started using apache tiles for rendering JSPs. Today, I'm dealing with the issue of inserting an attribute into a jsp page, with the value returned from my controller.

This is my base definition in tiles.xml:

<definition name="base.definition"
    template="/WEB-INF/views/layout.jsp">
    <put-attribute name="title" value="" />
    <put-attribute name="header" value="/WEB-INF/views/header.jsp" />
    <put-attribute name="menu" value="/WEB-INF/views/menuV2.jsp" />
    <put-attribute name="content" value="" />
    <put-attribute name="panelTable" value="" />
    <put-attribute name="footer" value="/WEB-INF/views/footer.jsp" />
</definition>

Then I extend base definition like this:

<definition name="viewpublication" extends="base.definition">
    <put-attribute name="title" value="publicationview.title"></put-attribute>
    <put-attribute name="content" value="/WEB-INF/views/publication/viewpublication.jsp"></put-attribute>
    <put-attribute name="panelTable" value="/WEB-INF/views/publication/leftpanel.jsp"></put-attribute>
    <put-attribute name="editLink" expression="${ model.editLink }"></put-attribute>
    <put-list-attribute name="scripts">
    </put-list-attribute>
    <put-list-attribute name="styles">

    </put-list-attribute>
</definition>

As you can see, I'm trying to insert an attribute inside another attribute (panelTable), and it is returned by my controller.

model.setAmIOwner(getUserId() == model.getPublicationOwnerId());
model.setEditLink(request.getContextPath() + "/publication/edit/" + pubService.getPublicationId() + "/" + sessionData.getAgentId());
return new ModelAndView("viewpublication", "publication", model);

This is the exception thrown:

org.apache.tiles.template.NoSuchAttributeException: Error importing attributes. Attribute 'editLink' is null

This is leftpanel.jsp

<tilesx:useAttribute name="editLink" id="editLink" classname="java.lang.String" />
<li class="list-group-item" id="firstRow"><span class="edit"><a href="${ editLink }"><fmt:message key="publicationview.panelEditPublication"></fmt:message></a></span></li>

What am I doing wrong? The things I've tried so far are from other answers, but error remains. Thanks in advance.


Solution

  • You need to cascade the attribute (cascade="true").

    Attributes defined into a definition can be cascaded to be available to all nested definitions and templates. For example the sample definition detailed above can be rewritten this way:

    Reading more about (cascaded) attributes here.