Spring MVC allows integration with many technologies. If you remember Sturts with Tiles, then a similar feature is provided by Spring MVC. There is minor variation in the way Spring MVC Tiles integration on Eclipse and Tomcat is implemented. Here are the details in this tutorial.In this article, we take previous example forward for Spring MVC Tiles integration. You are already aware of the advantages of usage of Tiles in an application. How you can define layout, and then configure to plug-in different strings and jsps. This technology allows us to reuse presentation tier components in an elegant way.
In the previous article, if you look at the controller, at the end of onSubmit() method, we gave the Jsp path to which the control is to be directed. This would be the first place where the change is required. Instead of Jsp name, now we will give an identifier, which represents the construct of multiple components as per Tiles requirement. Next, we need something which identifies that the forward/redirect given by the controller is actually pointing to a tiles composition to construct a page. In short, this will integrate Spring MVC with Tiles. Further, we require tiles definitions for all our views. Last is the change in existing jsps to pull out the common part in the layout jsp and other component jsps, and keep only the functional presentation in our old jsps. Let us see each of the changes one by one, and the rationale behind those changes.
Changes Required for Spring MVC Tiles Integraiton:
To enable tiles functionality in our application, we need following jars. The ‘commons’ jars are required by tiles.
In this jsp, we have two types of place holders, one is for string, and other is for jsps itself. In Tiles 2, there is insertAttribute element in tag library, that is used to insert a jsp, while strings are inserted using getAsString element, same as Tiles 1.
In this example, I am not putting anything in left.jsp, header.jsp and footer.jsp. All jsps are just to print whether those are left, header, or footer.
Main change is in the jsps containing functionality. We have to moved the common part out from here to the layout.jsp. This is how the helloworld.jsp will look like. Similar change is done in hellovisitor.jsp.
First thing we need to introduce in configuration is the assembling of the different jsp components. In this xml below, we have one base definition which assembles all components, and then we have page specific definitions which extend from this base definition. This is inheritance in object oriented terms. The definition name ‘helloworld’ will be used to represent the view.
Second major configuration change is in the SpringMVCTutorial-servlet.xml, here we need to integrate the spring container with tiles. If you compare this xml with the previous one, then following changes can be seen.
We are using url mapping handler, hence the helloworld.htm mapping to the controller bean is now defined. Thus the bean is independent of its usage now. (This has nothing to do with tiles integration, but it is cleaner way of using Spring MVC configuration.)
We are going to use url based view resolver with tiles to resolve the view name given by controller class, and map it to the tiles definition.
Next is tiles configure, this integrates our layout-tiles.xml with spring container. You have to give the layout definition in this configuration as a property. At applications start up all the definitions will be loaded in memory by the (application) server, and kept ready for use.
<?xml version=”1.0″ encoding=”UTF-8″?>
<bean id=”tilesConfigurer” class=”org.springframework.web.servlet.view.tiles2.TilesConfigurer”>
Java Code Change:
It is simple, now we are going to use different view name. Hence the last line of onSubmit() method will be
return new ModelAndView(“hellovisitor”, dataMap);
See it working:
Build the project and run on tomcat server. This is how the page will be presented when accessed.
Problems Faced in Spring MVC Tiles Integration:
Instead of jsp, only jsp names presented on the page displayed. I ended up in putting an hour to solve this, when I accessed the url after deploying on tomcat, instead of above output, it presented only the header.jsp, footer.jsp etc. arranged by layout, instead of actually executing the jsps. The problem was in layout definition. – I am having the jsps in document root. Hence in attribute definition I gave <put-attribute name=”body” value=”hellovisitor.jsp“/>, and not “/helloworld.jsp”. It caused the trouble.
Second was the submit button was doing nothing on press. After, looking at the ‘view source’ of the presented page, I found that the form:form tag is not executed by the tag library. I had moved all tag library definitions to the layout.jsp. When I moved those to the respective components and kept only tiles definition in the layout.jsp, it worked properly.
You can download the complete source code (excluding libraries) here.