I also had this issue and found a ViewPreparer did the trick. Code follows (not completely tested, use at your own risk):
Code:
package com.somecompany.tiles.view.preparer;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.Attribute;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.PreparerException;
import org.apache.tiles.preparer.ViewPreparer;
/**
* This ViewPreparer makes the last bit of the URI available to help the menu know which version of the button to display. For example,
* a request for /spring/index means that the home page was requested and, thus, the selected version of the home page button
* should be displayed. I tried using pageContext.request.requestURI in the jsp, but that only gives you the Tiles template URI. Research
* indicates that this has something to do with Servlet 2.4 and not Tiles.
*/
public class MenuViewPreparer implements ViewPreparer {
protected final Log logger = LogFactory.getLog(getClass());
public void execute(TilesRequestContext tilesContext, AttributeContext attributeContext)
throws PreparerException {
HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
String uri = request.getRequestURI();
logger.debug("RequestURI is: " + request.getRequestURI());
// get rid of any jsessionid
int i = uri.indexOf(';');
if (i > 0)
uri = uri.substring(0, i);
// find the last slash and extract the text after that (e.g., extract "blah" from "/spring/blah")
i = uri.lastIndexOf("/");
if (i >= 0) {
uri = uri.substring(i); // will include the slash
if (uri.length() > 1)
uri = uri.substring(1); // get rid of the slash
else
throw new PreparerException("URI ended with forward slash. Some string must exist after the last slash.");
}
else
throw new PreparerException("Could not find a forward slash in the URI.");
logger.debug("Processed URI: " + uri);
Attribute attribute = new Attribute();
attribute.setType(Attribute.AttributeType.STRING); // set the type to string or else Tiles will try to insert the view
attribute.setBody(uri);
attributeContext.putAttribute("pageName", attribute);
}
}
In layouts.xml:
Code:
<definition name="standardLayout" template="/WEB-INF/layouts/standard.jsp" preparer="com.somecompany.tiles.view.preparer.MenuViewPreparer" />
In standard.jsp:
Code:
<tiles:insertAttribute name="pageName" />