Declaratively injecting a bean in Tiles

374 Views Asked by At

I'm having a running system with integrated Tiles 2.1 and Spring MVC (with the help of Spring Roo) and want to set the menu item by a very simple bean, which returns a list of items:

public class TestMenu {
 public ArrayList<String> getEntries() {
    ArrayList<String> returner = new ArrayList<String>();

    returner.add("MenuItem 1");
    returner.add("MenuItem 2");

    return returner;
 }
}

My tiles configuration looks like that:

 <definition name="empty" template="/WEB-INF/layouts/empty.jspx">
  <put-attribute name="footer" value="/WEB-INF/views/empty/footer.jspx" />
  <put-attribute name="menu" value="/WEB-INF/views/empty/menu.jspx" />
  <put-attribute name="menuEntries">
   <bean classtype="com.reservation.ui.TestMenu" />
  </put-attribute>
 </definition>

And in my menu.jspx I intend to use the menuItems like following:

<tiles:useAttribute id="list" name="menuEntries" classname="com.reservation.ui.TestMenu" />
<c:forEach var="item" items="${list.Entries}">
  <div class="item">
    <div class="left">&amp;nbsp;</div>
    <div class="middle">${item}</div>
    <div class="right">&amp;nbsp;</div>
  </div>
</c:forEach>

I haven't found any documentation or example, which does something similiar but I think that this should be a common use case.

Does somebody know an applicable solution?

1

There are 1 best solutions below

0
On BEST ANSWER

OK, it goes like that:

The TestMenu class implements the ViewPreparer which implements the execute method and puts the requested MenuData as ListAttribute in Tiles.

public class TestMenu implements ViewPreparer {
 public List<String> getMenuItems() {
    ArrayList<String> returner = new ArrayList<String>();

    returner.add("MenuItem 1");
    returner.add("MenuItem 2");

    return returner;
 }

 public void execute(TilesRequestContext tilesContext, AttributeContext attributeContext) throws PreparerException {
    String selection = attributeContext.getAttribute("selection").getValue().toString();
    ListAttribute listAttribute = new ListAttribute(this.getMenuItems());

    attributeContext.putAttribute("menuItems", listAttribute, true);
 }
}

Configuration and jspx-file stays the same.