Results 1 to 4 of 4

Thread: Tiles forward and RequestURI

  1. #1
    Join Date
    Oct 2005
    Location
    Arlington, VA
    Posts
    14

    Default Tiles forward and RequestURI

    I have an application using JSF (MyFaces), Tiles and Acegi.

    I have two pages, and a post to the first page can result in a forward to the second page.

    The problem is that, for the forward, the HttpServletRequest's RequestURI is set to the "layout.jsp" page (which is my Tiles master layout for all my pages).

    So I can't put any URL security on any page that is the result of JSF navigation, because they all end up as forwards, and they all end up with the URL "layout.jsp".
    (I know I can use <redirect/> in all my navigation rules, but I'd rather not if possible).

    Just wondering if anyone else using Tiles and Acegi has run into this issue. Has anyone done any customizations where they can specify ViewIDs instead of URLs in the definition source?

    Thanks,
    Christopher Pierce

  2. #2

    Default

    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" />

  3. #3

    Default

    Or you could just put the info on the request and access that in your jsp...

    Code:
    request.setAttribute("PAGE_NAME", uri);

  4. #4

    Default

    This seems to work:
    Code:
    ${requestScope['javax.servlet.forward.request_uri']}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •