3

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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Eleeist
  • 6,891
  • 10
  • 50
  • 77
  • 1
    Have you tried it? As a last resort you can always use the `` tag. – Dave Newton Jan 12 '13 at 12:53
  • The `` 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](http://sscce.org)? – Roman C Jan 12 '13 at 14:54
  • Sending an Ajax request to get Header, Ajax request to get Footer, Ajax request to get Menu... It will make a lot of simultaneous Ajax requests in total and don't believe this is the most efficient way to load a page. – Eleeist Jan 12 '13 at 15:01

2 Answers2

2

Try following code:

$('#footer').load('your/action/with/namespace');

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.

Jaiwo99
  • 9,687
  • 3
  • 36
  • 53
  • 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
2

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.

Community
  • 1
  • 1
Quaternion
  • 10,380
  • 6
  • 51
  • 102
  • 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