Quadriga as well as several other of the Java/Spring web applications developed in the Digital Innovation Group use Apache Tiles for managing the layout of the application. This tutorial will you walk through a very basic example of how to implement Apache Tiles with Spring.
...
Code Block | ||||
---|---|---|---|---|
| ||||
<dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-template</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-core</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-api</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-servlet</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-jsp</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> |
Next, open the rootservlet-context.xml file in your WEB-INF > spring folder. From there, remove the InternalResourceViewResolver bean declaration and add the following code:
Code Block | ||||
---|---|---|---|---|
| ||||
<beans:bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" > <beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/> </beans:bean> <beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"> <beans:property name="definitions"> <beans:list> <beans:value>/WEB-INF/tiles-def.xml</beans:value> </beans:list> </beans:property> </beans:bean> |
Your rootservlet-context.xml file should now look like this:
Code Block | ||||
---|---|---|---|---|
| ||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="edu.asu.diging.tutorial.spring" /> <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" > <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/> </bean> <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"> <property name="definitions"> <list> <value>/WEB-INF/tiles-def.xml</value> </list> </property> </bean> </beans> |
You declared two beans in this piece of XML. First, you specify that you want to use a UrlBasedViewResolver to resolve your views. As a property you give the resolver a TilesView. The second bean, the TilesConfigurer, "simply configures a TilesContainer using a set of files containing definitions, to be accessed by TilesView
instances. This is a Spring-based alternative (for usage in Spring configuration) to the Tiles-provided ServletContextListener
(e.g. CompleteAutoloadTilesListener
for usage in web.xml
.)" (Spring TilesConfigurer Javadoc)
Inside the "definitions" property, you specify where Tiles can find the definitions telling it what templates to render. In our example, the definitions are in a file called "tiles-def.xml" inside the WEB-INF folder.
Tiles Definitions
Ok, now let's create the tiles-def.xml file we just told Tiles about. Create a new file inside your WEB-INF folder, and paste the following code in it:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"> <tiles-definitions> <!-- Base definition --> <definition name="base.definition" template="/WEB-INF/tiles/layouts/skeleton.jsp"> <put-attribute name="title" value="" /> <put-attribute name="currentPage" value="home" /> </definition> <!-- Home pages --> <definition name="home" extends="base.definition"> <put-attribute name="title" value="Quadriga - Login" /> <put-attribute name="content" value="/WEB-INF/views/home.jsp" /> <put-attribute name="footer" value="/WEB-INF/views/footer.jsp" /> </definition> </tiles-definitions> |
For now, let's leave it with that. We'll come back to it later with more explanations.
The Skeleton
So far, we have talked about how to use Apache Tiles, however, we haven't said anything about why we would use it. Apache is a templating framework with the goal to make it easier to write and maintain webpages across an application. Remember, how in the first tutorial we wrote jsp pages like this:
...