Take the 2-minute tour ×

In Stuts2 I am using Tiles plugin to create a layout for the website (menu, footer, header etc.) that is consistent on every page.

Now each tile is just a static HTML content.

Is it possible to make a Tile more dynamic by eg. calling a Footer action class every time the footer is to be rendered? For example: to fetch footer content from database.

If I was to do that inside every page's action class in my application this would make for a very unusable code...

So maybe it is possible from Tile perspective?

share|improve this question
Have you tried it? As a last resort you can always use the <s:action> tag. –  Dave Newton Jan 12 '13 at 12:53
The <s:action> tag looks like something that I could use. I would be happy to accept this as an answer if you post one. –  Eleeist Jan 12 '13 at 13:17
It seems 1 sec solution, next time you get that you need Ajax. –  Roman C Jan 12 '13 at 13:41
But it does not seem right to do 10 Ajax requests on page load if I need 10 different "dynamic modules" on my page... –  Eleeist Jan 12 '13 at 13:45
I don't understand what are you talking could you provide SSCCE? –  Roman C Jan 12 '13 at 14:54

2 Answers 2

up vote 2 down vote accepted

There is only one way to do what you ask with a tiles version less than 2.2.2 and that is with a "preparer".

This however is not an integration with struts2 but would mean the preparer it self will access the service layer to get the required content for the view and all that content would need to be set though tiles attributes.

With tiles versions 2.2.2 and higher: You can use OGNL expressions within tiles attributes, this can allow access to some struts2 interaction as well as static method access. With static method access you can call a method to return a string how ever you want. Creating such a string would be on par with writing a scriptlet.

To upgrade you need to either manually override some jars to get tiles 2.2.2, or to get version three you will need to implement your own result type: How to integrate Struts 2 with Tiles 3.

I don't actually recommend either of the above methods at this time, tiles 3 is recommended but not as an excuse to do something as bad as writing a scriptlet. It would probably be better to use the s:action tag in a tile, as mentioned by David or use an Ajax method as mentioned by Jaiwo99. The reason being that both these methods keep with struts2 while the ones I presented would be unusual and be harder to maintain. Personally I would lean towards the ajax methods.

Struts2 along with the struts2-json-plugin makes creating json services very simple. Tiles is a nice system for reducing boiler plate. If ajax is used heavily the two really can compliment each other. You can make a lot of reusable ajax components, just be sure to not hard code the urls of actions. Always use the s:url tag and assign that to JS variables.

share|improve this answer
I will accept this as an answer as you nicely explain pros and cons of each method. That explained many things and was very helpful. –  Eleeist Jan 12 '13 at 22:09

Try following code:


i'm assuming your footer is with id footer, everytime you open a page, your footer action class will be called and the data can be fetched dynamically.

share|improve this answer
Yeah, that's what I also thought about and it's certainly a valid option, however... are there any non-javascript solutions? –  Eleeist Jan 12 '13 at 11:54

Your Answer


