CREATING A TEMPLATE WITH APACHE TILES
The first thing you need to do is open your Spring Tool Suite and copy the test project and paste it. The name you need to give the project is AlphaShop. Then go to the pom.xml file, change the ArtifactId to alphashop, replace all occurrences named Test1 in alphashop. We also need to set a dependency to Apache Tiles as shown in the figure.
CONFIGURATION OF APACHE TILES
Adding the dependency saves the project, the libraries will be updated. Now we are going to create a folder under the WEB-INF folder, layouts with two other folders inside, definitions and templates. In the definitions folder we create an XML file, tiles.xml. I report the code, represents the Apache Tiles configuration where we are going to specify a common template for all JSP pages and the footer. We also specify the title and content of the page represented by the welcome.jsp file.
Let’s now go ahead and create the two JSP files in the template folder. See base.jsp. We inserted thanks to tiles three dynamic attributes defined in the XML configuration file, the title, content and footer represented by two JSP pages. It is important to remark that this file, base.jsp will form the skeleton, the scaffolding of our website. So the tiles work like this. You create the configuration in an xml file, then in a base page we will have as many dynamic elements as specified in the xml file. The footer is very simple and you can look at it by downloading the project.
TILE CONFIGURATION IN THE CONFIGURATION JAVA CLASS
Now we need to tell Spring MVC that there are now tiles; so, we need to specify that our View Resolver is also the Apache Tiles. We create a new configuration class TilesConfig.java. I report the code.
We specified the @Configuration notation because it must be loaded in the startup phase, plus it is used to tell the IOC that this is a configuration class. Such a class is very simple, in fact the path to the configuration XML file is set. Now we need to edit the WebApplicationContextConfig.java file. As you will recall in Spring MVC there must be a View Resolver that transforms the raw data into a client-compatible page. So far, we have specified a standard View Resolver that goes and looks for JSP pages in /WEB-INF/view/. Now we need to adapt that configuration file to the tiles. We need to tell it not to just search JSP pages but to use tiles as well.
MODIFICATION OF THE CONTROLLER CLASS
The index name that we assigned in the tiles configuration XML file is very important because as we will see it will be used by the controller. It requires the creation of a welcome.jsp file, which we are now going to create. Having done that let’s go to modify the controller, I report the changes. The basic RequestMapping is the crossbar, and when this occurs you have to use the getWelcome2 method. If there is index in the Query String then we use getWelcome.
DOWNLOAD ARTICLE CODE
Below is the video for importing into your project workspace.
Leave A Comment