struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David H. DeWolf" <ddew...@apache.org>
Subject [tiles2] Re: A little help please
Date Wed, 03 Jan 2007 01:12:26 GMT


Stone, Sam wrote:
> I'm in a JSP that's NOT invoked by tiles. I'm looking for a certain
> attribute and, if found, I want to do a <tiles:insertDefinition
> template="<%=my_attribute%>"/>.

That's not currently possible and it seems odd. Why do you want to use 
an attribute from a totally unrelated tile to store the name of a template?

> 
> Here is my code that works with the Sep2006 snapshot:
> <%
> String sTilesDefName = (String) request.getAttribute("tile_name");
> TilesContext tilesContext = 
>   new ServletTilesContext(session.getServletContext(), request,
> response);
> ComponentDefinition componentDefinition = 
>   TilesUtil.getDefinition(sTilesDefName, tilesContext);
> String sSpecialPage = 
>   (String) componentDefinition.getAttribute("SpecialPage");
> boolean hasCodePage = sSpecialPage!= null && sSpecialPage.length() > 0;
> %>
> <c_rt:choose>
>   <c_rt:when test='<%=hasCodePage%>'>
>     <tiles:insert template="<%=sSpecialPage%>" ignore="true"/>
>   </c_rt:when>
>   <c_rt:otherwise>
>     <tiles:insert name="<%=sTilesDefName%>" ignore="true"/>
>   </c_rt:otherwise>
> </c_rt:choose>

The code from september is inconsequential at this point.  That was 
merely a snapshot and things have changed dramatically since then.  The 
point of the refactoring was to encapsulate all of the tiles processing 
logic into a container so that it could be more easily integrated and 
extended by/with other frameworks.

I'm sure there's another way to accomplish what you're trying to 
accomplish though.  So explain it a little more for me.

1) Where does the special page currently get set - in your tiles xml or 
programatically?

2) Is there are reason why you don't invoke a single definition which 
contains this logic (and then have it perform the view logic needed?)

3) Have you considered using a ViewPreparer with your definition to 
mutate the component context prior to rendering?

I'd probably go with #3 as it's cleaner:

<definition name="tileName"
             preparer="com.me.MyViewPreparer" . . .>
    <put name="special" value="special.jsp" type="template" />
</definition>

public class MyViewPreparer implements ViewPreparer {

     public void execute(TilesRequestContext tilesContext,
                         ComponentContext componentContext)
     throws Exception {
         if(componentContext.getAttribute("special") != null)) {
             // override the default
             componentContext.putAttribute(. . .)
         }
     }
}

Hope that helps,


David

> 
> 
> This is all working fine. I just can't quite figure out how to do this
> with the new Tiles 2 codebase.
> 
> Sam
> 
>  
> -----Original Message-----
> From: David H. DeWolf [mailto:ddewolf@gmail.com] On Behalf Of David H.
> DeWolf
> Sent: Tuesday, January 02, 2007 7:24 PM
> To: Struts Developers List
> Subject: Re: A little help please
> 
> The component context is not used to retrieve attributes for a given 
> definition - it is used to define and retrieve information about the 
> currently executing environment.
> 
> If you need to MODIFY a definition you can try to utilize the 
> MutableTilesContainer interface.
> 
> Perhaps what you're asking for is not supported. . .what is the use 
> case?  What specifically are you trying to accomplish by accessing the 
> definition's attribute?
> 
> 
> David
> 
> Stone, Sam wrote:
>> Still having trouble grabbing an attribute from a tiles definition. I
>> was doing this OK with Sep 06 code like so:
>>
>> String sTilesDefName = (String) request.getAttribute("tile_name");
>> TilesContext tilesContext = new
>>   ServletTilesContext(session.getServletContext(), request, response);
>>
>> ComponentDefinition componentDefinition =
>>   TilesUtil.getDefinition(sTilesDefName, tilesContext);
>>
>> String sMyAtribute = (String)
>>  componentDefinition.getAttribute("myAttribute");
>>
>> String sTilesDefName = (String)   
>>   request.getAttribute(RovingNavServlet.ATTR_ACTIVEPAGE);
>>
>>
>> David DeWolfe suggested I try the following:
>>
>>> TilesContainer container = TilesAccess.getContainer(context);
>>> ComponentContext ctx = container.getComponentContext(req, res);
>>> ComponentAttribute attr = ctx.getAttribute(name);
>> I did this but I find that attr is always null. Containter and ctx
>> appear to be valid objects.
>>
>> So I modified my code a bit including checking to see if the container
>> was really recognizing my definition (isValidDefinition). It is. If I
>> give the name of any existing definition then it returns true. If I
> give
>> it a name not in my tiles-defs then it returns false.
>>
>> Ctx.getAttributeNames is coming back with no attributes (i.e. my while
>> (it.hasNext()) loop never executes). So I'm still stuck.
>>
>> Following is my current code:
>>
>> try
>> {
>> TilesContainer container = 
>>   TilesAccess.getContainer(session.getServletContext());
>>
>> String sTilesDefName = (String) request.getAttribute("tile_name");
>> if (container.isValidDefinition(request, response, sTilesDefName))
>> 	System.out.println("valid definition");
>> else
>> 	System.out.println("invalid definition");
>>
>> ComponentContext ctx = container.getComponentContext(pageContext);
>> System.out.println("->ctx=" + ctx);
>>
>> Iterator it = ctx.getAttributeNames();
>> int i=0;
>> while (it.hasNext())
>> 	System.out.println("->" + i++);
>> }
>> catch (Exception e)
>> {
>> 	System.out.println(e);
>> }
>>
>>  
>> Sam
>> ------------------------------------------------
>>
>> -----Original Message-----
>> From: Stone, Sam [mailto:sstone@constantcontact.com] 
>> Sent: Tuesday, January 02, 2007 1:28 PM
>> To: Struts Developers List
>> Subject: RE: How to access componentDefinition?
>>
>> In your example:
>>
>> TilesContainer container = TilesAccess.getContainer(context);
>> ComponentContext ctx = container.getComponentContext(req, res);
>> ComponentAttribute attr = ctx.getAttribute(name);
>>
>> ctx comes back with no attributes (therefore attr results in null).
>>
>> I think I want to call getContainer with pageContext. How do I get the
>> pageContext?
>>
>> To recap what I WAS doing with the old API - I was inspecting a tiles
>> definition and searching for a specific attribute. I'm doing this
> BEFORE
>> dispatching to the tiles servlet.
>>
>> Sam
>>
>> -----Original Message-----
>> From: David H. DeWolf [mailto:ddewolf@gmail.com] On Behalf Of David H.
>> DeWolf
>> Sent: Tuesday, January 02, 2007 10:36 AM
>> To: Struts Developers List
>> Subject: Re: How to access componentDefinition?
>>
>> potentially . . .it depends on your environment.  My guess is that
> from 
>> your example the answer is yes.
>>
>> David
>>
>> Stone, Sam wrote:
>>> TilesContainer container = TilesAccess.getContainer(context);
>>>
>>> Is "context" the servletContext?
>>>
>>> -----Original Message-----
>>> From: David H. DeWolf [mailto:ddewolf@gmail.com] On Behalf Of David
> H.
>>> DeWolf
>>> Sent: Tuesday, January 02, 2007 10:30 AM
>>> To: Struts Developers List
>>> Subject: Re: How to access componentDefinition?
>>>
>>> Hi Sam:
>>>
>>> Everything is now done through the container.
>>>
>>> Try something like this:
>>>
>>> TilesContainer container = TilesAccess.getContainer(context);
>>> ComponentContext ctx = container.getComponentContext(req, res);
>>> ComponentAttribute attr = ctx.getAttribute(name);
>>>
>>>
>>> David
>>>
>>> Stone, Sam wrote:
>>>> Using the API from Sep 2006 I was able to access externally an
>>> attribute
>>>> from a tiles definition as follows:
>>>>
>>>> String sTilesDefName = (String) request.getAttribute("tile_name");
>>>> TilesContext tilesContext = new
>>>> ServletTilesContext(session.getServletContext(), request, response);
>>>>
>>>> ComponentDefinition componentDefinition =
>>>> TilesUtil.getDefinition(sTilesDefName, tilesContext);
>>>>
>>>> String sMyAtribute = (String)
>>>> componentDefinition.getAttribute("myAttribute");
>>>>
>>>> I can't quite figure out how to do this using the current Tiles 2
>> API.
>>>> Sam
>>>>
>>>>
>>>>
>>>>
> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
>>>> For additional commands, e-mail: dev-help@struts.apache.org
>>>>
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: dev-help@struts.apache.org
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: dev-help@struts.apache.org
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
>> For additional commands, e-mail: dev-help@struts.apache.org
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
>> For additional commands, e-mail: dev-help@struts.apache.org
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
>> For additional commands, e-mail: dev-help@struts.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
> For additional commands, e-mail: dev-help@struts.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
> For additional commands, e-mail: dev-help@struts.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Mime
View raw message