Sunday, March 13, 2011

Spring MVC and Tiles 2

I still remember how cool it was to use Tiles with Struts, back in Mexico and a couple of times here, but since I've migrated my efforts on Spring MVC;
At the very beginning I couldn't implement Tiles successfully (I just tried 2 times :P - 1 really important and another one just for fun)
But when Tiles became an independent project: Apache Tiles 2 things changed a little bit, and now that everything could be delegated to Spring, let's give it a shot!
I didn't have to change too many things, because let's start from an existing App using Spring MVC, Spring Core, Hibernate, Apache CXF, DisplayTag, jCaptcha, etc.

This is my Application layout (yes! I <3 Maven 2!)
 

This is one of my Controllers (something really common)
On the return statement I'm just displaying the file: /jsp/start/landing_page.jsp
This is my previous webApplicationContext.xml (look at the InternalResourceViewResolver - it maps the JSP's to the return statements on my Controllers)

Let's start by integrating Tiles


Add new dependencies in your pom.xml
Update your webApplicationContext.xml (don't forget to take out the existing InternalResourceViewResolver - we're going to replace it with a Tiles View Resolver) by adding these entries
Put more attention to this portion (the rest is a nice to have)
Then according to this tilesConfigurer, I have to add a new file on each JSP folder (views.xml) and my layouts, like this
 

layouts.xml
default.jsp
footer.jsp. Look at the links, because this is the key to make this APP Multilanguage (besides the entries on webApplicationContext.xml and obviously the *.properties files)
 

views.xml on /jsp/start/ folder
This portion makes the magic (
without touching any single line of code!!! )
Where else have you seen "start/landing_page" before? If you don't remember, in my controller (return statement: return "start/landing_page";//show jsp)

And now you're ready to rock!

No comments:

Post a Comment