On Mon, Oct 12, 2009 at 2:55 PM, excurser <[email protected]> wrote:
> On Mon, Oct 12, 2009 at 12:38 PM, excurser <[email protected]> wrote:
>> On Mon, Oct 12, 2009 at 10:42 AM, Antonio Petrelli
>> <[email protected]> wrote:
>>>
>>> 2009/10/12 Johannes Kuhs <[email protected]>
>>>
>>> > I was wondering if there is a way to inherit the value of an attribute and
>>> > to extend it with new content. Basically, I’m looking for something like
>>> > inherit=”true” for list-attributes but for normal attributes.
>>> >
>>> > Following is a quick example of what I would like to do:
>>> >
>>> > Initialization in /WEB-INF/tiles-defs.xml:
>>> >
>>> > <definition name=" test.definition" template="/layouts/classic.jsp">
>>> >    <put-attribute name="title" value="Initial title value" />
>>> > </definition>
>>> >
>>> > Inherit and extend in /test.jsp, note inherit=”true” in the <putAttribute>
>>> > tag (which does not work but I think it shows what I want to achieve):
>>> >
>>> > <tiles:insertDefinition name="test.definition">
>>> >  <tiles:putAttribute name="title" inherit=”true”>
>>> >      --This text gets appended to the initial value of the title attribute
>>> >  </tiles:putAttribute>
>>> > </tiles:insertDefinition>
>>> >
>>> > Now, when inserting the attribute with the following code:
>>> >
>>> > <tiles:insertAttribute name="title"/>
>>> >
>>> > The following title should be displayed:
>>> > Initial title value--This text gets appended to the initial value of the
>>> > title attribute
>>> >
>>>
>>>
>>> You can do it with a preparer. For example, add an "auxiliar" attribute in
>>> which you put the text to add, something like:
>>>
>>> <tiles:insertDefinition name="test.definition"
>>> preparer="my.package.MyPreparer">
>>>  <tiles:putAttribute name="title-aux">
>>>     --This text gets appended to the initial value of the title attribute
>>>  </tiles:putAttribute>
>>> </tiles:insertDefinition>
>>>
>>> And in your preparer:
>>>
>>> public void execute(TilesRequestContext tilesContext,
>>>            AttributeContext attributeContext) {
>>>        String auxValue =
>>> attributeContext.getAttribute("title-aux").getValue().toString();
>>>        String originalTitle =
>>> attributeContext.getAttribute("title").getValue().toString();
>>>        attributeContext.putAttribute("title", new Attribute(originalTitle +
>>> auxValue, null);
>>>    }
>>>
>>> HTH
>>> Antonio
>>
>> Thanks for your reply, Antonio! I guess the preparer solution would
>> work but I would have to create a new preparer for every attribute
>> that I want to inherit/extend. Is there a way to have a general
>> preparer that I pass the attribute in question and the value to append
>> to?
>>
>> Thanks,
>> John
>>
>
> Following up on this. I wrote a preparer class that I can reuse for
> various attributes. However, so far I can only extend attributes that
> have a string value associated with them. If an attribute links to a
> file (e.g. /header.jsp) the the preparer adds the aux value to the
> file name instead of the actual content in the file resulting in
> something like:
>
> /header.jsp--appended text here
>
> This of course results in an error because there is not file called
> "/header.jsp--appended text here". I guess the header attribute
> content needs to be evaluated before adding the aux value. Is this
> possible within the preparer class?
>
> Thanks!
>

Can nobody help me with this? I tried evaluating the attribute content
with the following Java code but unfortunately get an error.

public class AuxiliaryPreparer implements ViewPreparer  {

        @Override
        public void execute(TilesRequestContext tilesContext,
AttributeContext attributeContext) {
                String attributeName =
attributeContext.getAttribute("aux-attribute").getValue().toString();
                String attributeValue =
attributeContext.getAttribute(attributeName).getValue().toString();
                Attribute att = attributeContext.getAttribute(attributeName);
                
                // Code to evaluate attribute
                BasicTilesContainerFactory factory = new
BasicTilesContainerFactory();
                TilesContainer container =
factory.createContainer(tilesContext.getApplicationContext());
                Attribute test = (Attribute) container.evaluate(att, 
tilesContext);
                
                String auxValue =
attributeContext.getAttribute("aux-value").getValue().toString();
                attributeContext.putAttribute(attributeName, new
Attribute(attributeValue + auxValue));
        }
}

I get the following error:
java.io.FileNotFoundException: ServletContext resource
[/WEB-INF/tiles.xml] cannot be resolved to URL because it does not
exist

I don't have a file called tiles.xml but I don't know why it is even
looking for it.

Anyone has an idea how to solve this problem?

Reply via email to