You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tiles.apache.org by Pauli Savolainen <sa...@gmail.com> on 2008/02/28 15:42:19 UTC

Struts 2 Tiles 2 Runtime composition

Hi,

I am attempting to change an existing tiles definition during an execution
of a Struts action. Example tiles definition:

<definition name="layout" template="/layout.jsp">
  <put-attribute name="head" value="/head.jsp" />
  <put-attribute name="body" value="/login.jsp" />
</definition>

In the action I want to choose a correct body value based on some
application specific rules. For example if I call:  /user/1, the User
action's show method is executed and the body attribute's value would turn
into /user/show.jsp. This would prevent me from writing definitions for each
possible case.

Tile tutorial says I can get my hands on an attribute context (The class
TilesAttributeContext does not exist in my tiles plugin build). In the
action I do:

TilesAttributeContext attributeContext = container.startContext(request,
response);
attributeContext.setAttribute("body", "/user/show.jsp");
container.render("layout", request, response);
container.endContext(request, response);

I bet this is not the correct way to do it, because it seems that the tiles
definition overrides the value I insert here. Even if I do this in an
interceptor after the action result, the value is not changed.

I suppose there is no option in tiles to define definition with wildcards.
At least I did not find this option. I could point my action results
dynamically to a definition with wildcards.

<definition name="layout" template="/layout.jsp">
  <put-attribute name="head" value="/head.jsp" />
  <put-attribute name="body" value="/login.jsp" />
</definition>

<definition name="layout.*.*" extends="layout">
  <put-attribute name="body" value="/{1}/{2}.jsp" />
</definition>

So my question is how would I achieve what am I try to do? Am I on the right
tracks and just missing something, or is this even possible?

I am using 2.1.1-SNAPSHOT builds struts-core and struts tiles-plugin.

Thank you

Pauli

Re: Struts 2 Tiles 2 Runtime composition

Posted by Antonio <an...@gmail.com>.
2008/11/12 stanlick <st...@gmail.com>:
>
> Antonio --
>
> I'm adding this comment here for complete navigability when others might
> have the same misunderstanding.  So is the Tiles/2 example code:
>
> <tiles:insertDefinition name="test.definitionOne.messageThisIsAMessage" />
>
> where the dependencies are set/created?

Sorry the misunderstanding was mine :-D
Anyway the Struts 2 code you posted was OT in this case.

Antonio

Re: Struts 2 Tiles 2 Runtime composition

Posted by stanlick <st...@gmail.com>.
Antonio -- 

I'm adding this comment here for complete navigability when others might
have the same misunderstanding.  So is the Tiles/2 example code:

<tiles:insertDefinition name="test.definitionOne.messageThisIsAMessage" />

where the dependencies are set/created?

Peace,
Scott



Antonio Petrelli-3 wrote:
> 
> 2008/11/11 stanlick <st...@gmail.com>:
>> It appears that I am in the same boat you were awhile back!  Given your
>> wildcard tiles definition layout.*.* , does the Struts 2 action mapping
>>
>>                <action name="layout.*.*">
>>                        <result>layout.{1}.{2}</result>
>>                </action>
>>
>> not substitute correctly?
> 
> Same problem here, probably the Struts2/Tiles2 plugin does not
> set/create all the dependencies.
> 
> Antonio
> 
> 

-- 
View this message in context: http://www.nabble.com/Struts-2-Tiles-2-Runtime-composition-tp15737832p20460034.html
Sent from the tiles users mailing list archive at Nabble.com.


Re: Struts 2 Tiles 2 Runtime composition

Posted by Antonio <an...@gmail.com>.
2008/11/11 stanlick <st...@gmail.com>:
> It appears that I am in the same boat you were awhile back!  Given your
> wildcard tiles definition layout.*.* , does the Struts 2 action mapping
>
>                <action name="layout.*.*">
>                        <result>layout.{1}.{2}</result>
>                </action>
>
> not substitute correctly?

Same problem here, probably the Struts2/Tiles2 plugin does not
set/create all the dependencies.

Antonio

Re: Struts 2 Tiles 2 Runtime composition

Posted by stanlick <st...@gmail.com>.
Hey Pauli -- 

It appears that I am in the same boat you were awhile back!  Given your
wildcard tiles definition layout.*.* , does the Struts 2 action mapping 

		<action name="layout.*.*">
			<result>layout.{1}.{2}</result>
		</action>

not substitute correctly?

Peace,
Scott



Pauli Savolainen wrote:
> 
> Hi,
> 
> I am attempting to change an existing tiles definition during an execution
> of a Struts action. Example tiles definition:
> 
> <definition name="layout" template="/layout.jsp">
>   <put-attribute name="head" value="/head.jsp" />
>   <put-attribute name="body" value="/login.jsp" />
> </definition>
> 
> In the action I want to choose a correct body value based on some
> application specific rules. For example if I call:  /user/1, the User
> action's show method is executed and the body attribute's value would turn
> into /user/show.jsp. This would prevent me from writing definitions for
> each
> possible case.
> 
> Tile tutorial says I can get my hands on an attribute context (The class
> TilesAttributeContext does not exist in my tiles plugin build). In the
> action I do:
> 
> TilesAttributeContext attributeContext = container.startContext(request,
> response);
> attributeContext.setAttribute("body", "/user/show.jsp");
> container.render("layout", request, response);
> container.endContext(request, response);
> 
> I bet this is not the correct way to do it, because it seems that the
> tiles
> definition overrides the value I insert here. Even if I do this in an
> interceptor after the action result, the value is not changed.
> 
> I suppose there is no option in tiles to define definition with wildcards.
> At least I did not find this option. I could point my action results
> dynamically to a definition with wildcards.
> 
> <definition name="layout" template="/layout.jsp">
>   <put-attribute name="head" value="/head.jsp" />
>   <put-attribute name="body" value="/login.jsp" />
> </definition>
> 
> <definition name="layout.*.*" extends="layout">
>   <put-attribute name="body" value="/{1}/{2}.jsp" />
> </definition>
> 
> So my question is how would I achieve what am I try to do? Am I on the
> right
> tracks and just missing something, or is this even possible?
> 
> I am using 2.1.1-SNAPSHOT builds struts-core and struts tiles-plugin.
> 
> Thank you
> 
> Pauli
> 
> 

-- 
View this message in context: http://www.nabble.com/Struts-2-Tiles-2-Runtime-composition-tp15737832p20447743.html
Sent from the tiles users mailing list archive at Nabble.com.


Re: Struts 2 Tiles 2 Runtime composition

Posted by VSG <sr...@gmail.com>.
Hi Pauli,
 
 I am working on the same requirement. I have done some thing like this
  <definition name="preparer" preparer="TestViewPreparer" >
       <put-attribute name="title" value="Tiles tutorial homepage" />
       <put-attribute name="header" value="/layouts/header.jsp" />	 
        <put-attribute name="body" value=="???{nextPage}???" />
        <put-attribute name="footer" value="/layouts/header.jsp" />
     </definition> 

In testViewPreparer
 
 HttpServletRequest serReqObj =
(HttpServletRequest)tilesContext.getRequest();
 ValueStack valuStack = ServletActionContext.getValueStack(serReqObj);
 String nextPage = valuStack.findString("nextPage");  
 attributeContext.putAttribute("body",new Attribute(nextPage));

In jsp

<tiles:insertDefinition name="preparer.definition" />

here I am facing two problems

 1. value stack is giving null value.
 2.i hard code the nextPage value some how but after that the exception:-

 java.lang.NullPointerException
	at
org.apache.struts2.tiles.StrutsTilesRequestContext.dispatch(StrutsTilesRequestContext.java:82)
	at
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:417)
	at
org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:368)
	at
org.apache.tiles.jsp.taglib.InsertDefinitionTag.render(InsertDefinitionTag.java:63)
	at
org.apache.tiles.jsp.taglib.RenderTagSupport.execute(RenderTagSupport.java:171)
	at
org.apache.tiles.jsp.taglib.RoleSecurityTagSupport.doEndTag(RoleSecurityTagSupport.java:75)
	at
org.apache.tiles.jsp.taglib.ContainerTagSupport.doEndTag(ContainerTagSupport.java:80)
	at
org.apache.jsp.second_jsp._jspx_meth_tiles_005finsertDefinition_005f0(second_jsp.java:86)
	at org.apache.jsp.second_jsp._jspService(second_jsp.java:59)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

 
 know my question is it the correct way of doing the things.
 can u help me out how u finshed the things.

 VSG.

 
 
 

       
 


Pauli Savolainen wrote:
> 
> Hi,
> 
> I am attempting to change an existing tiles definition during an execution
> of a Struts action. Example tiles definition:
> 
> <definition name="layout" template="/layout.jsp">
>   <put-attribute name="head" value="/head.jsp" />
>   <put-attribute name="body" value="/login.jsp" />
> </definition>
> 
> In the action I want to choose a correct body value based on some
> application specific rules. For example if I call:  /user/1, the User
> action's show method is executed and the body attribute's value would turn
> into /user/show.jsp. This would prevent me from writing definitions for
> each
> possible case.
> 
> Tile tutorial says I can get my hands on an attribute context (The class
> TilesAttributeContext does not exist in my tiles plugin build). In the
> action I do:
> 
> TilesAttributeContext attributeContext = container.startContext(request,
> response);
> attributeContext.setAttribute("body", "/user/show.jsp");
> container.render("layout", request, response);
> container.endContext(request, response);
> 
> I bet this is not the correct way to do it, because it seems that the
> tiles
> definition overrides the value I insert here. Even if I do this in an
> interceptor after the action result, the value is not changed.
> 
> I suppose there is no option in tiles to define definition with wildcards.
> At least I did not find this option. I could point my action results
> dynamically to a definition with wildcards.
> 
> <definition name="layout" template="/layout.jsp">
>   <put-attribute name="head" value="/head.jsp" />
>   <put-attribute name="body" value="/login.jsp" />
> </definition>
> 
> <definition name="layout.*.*" extends="layout">
>   <put-attribute name="body" value="/{1}/{2}.jsp" />
> </definition>
> 
> So my question is how would I achieve what am I try to do? Am I on the
> right
> tracks and just missing something, or is this even possible?
> 
> I am using 2.1.1-SNAPSHOT builds struts-core and struts tiles-plugin.
> 
> Thank you
> 
> Pauli
> 
> 

-- 
View this message in context: http://www.nabble.com/Struts-2-Tiles-2-Runtime-composition-tp15737832p20949380.html
Sent from the tiles users mailing list archive at Nabble.com.


Re: Struts 2 Tiles 2 Runtime composition

Posted by Pauli Savolainen <sa...@gmail.com>.
2008/2/28, Antonio Petrelli <an...@gmail.com>:
> 2008/2/28, Pauli Savolainen <sa...@gmail.com>:
>
> >  In the action I want to choose a correct body value based on some
>  >  application specific rules. For example if I call:  /user/1, the User
>  >  action's show method is executed and the body attribute's value would turn
>  >  into /user/show.jsp. This would prevent me from writing definitions for each
>  >  possible case.
>
>
> You could use a ViewPreparer:
>  http://tiles.apache.org/tutorial/advanced/preparer.html
>  You can use the AttributeContext object passed as a parameter in the
>  "execute" method and override the "body" attribute.
>  To know the calling URL, look at the TilesRequestContext:
>  http://tiles.apache.org/2.0/framework/apidocs/org/apache/tiles/context/TilesRequestContext.html
>  The "getRequest" method returns the original HttpServletRequest.
>
>  HTH
>
> Antonio
>

Hi,

Thank you very much. I got it working with a little tweaking. I
inspect the url in the view preparer and include a body content
according to it. Works nice.

Pauli

Re: Struts 2 Tiles 2 Runtime composition

Posted by Antonio Petrelli <an...@gmail.com>.
2008/2/28, Pauli Savolainen <sa...@gmail.com>:
>  In the action I want to choose a correct body value based on some
>  application specific rules. For example if I call:  /user/1, the User
>  action's show method is executed and the body attribute's value would turn
>  into /user/show.jsp. This would prevent me from writing definitions for each
>  possible case.

You could use a ViewPreparer:
http://tiles.apache.org/tutorial/advanced/preparer.html
You can use the AttributeContext object passed as a parameter in the
"execute" method and override the "body" attribute.
To know the calling URL, look at the TilesRequestContext:
http://tiles.apache.org/2.0/framework/apidocs/org/apache/tiles/context/TilesRequestContext.html
The "getRequest" method returns the original HttpServletRequest.

HTH
Antonio