Spring MVC with Tiles framework sample application


Below post talks about the Spring MVC integration with the tiles framework.

You can build developer friendly and user friendly web applications using the tiles framework.

Basic Folder structure of the application:

Typical folder structure of the Spring MVC application has mentioned below:

Spring MVC Application folder structure
Spring MVC Application folder structure

 

Web.xml:

Below is the web.xml file which contains the spring configuration. DispatcherServlet is controller of the spring mvc application, all the requests which coming to the application will be passed through it. DispatcherServlet loads the spring-context.xml file during server start up and keeps the all the configurations in servlet context. Action path configured for this applications is *.html.

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xmlns=”http://java.sun.com/xml/ns/javaee&#8221;
xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221;
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221;
id=”WebApp_ID” version=”2.5″>
<display-name>Spring Hello World</display-name>
<welcome-file-list>
<welcome-file>/</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/config/spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

index.jsp:

index.jsp file configured in web.xml as a welcome file, so this file will be loaded when user hit the http://localhost:8080/SpringMVC/ url in the browser.

<jsp:forward page=”contacts.html”></jsp:forward>

HelloWorldController .java:

When ever index.jsp loaded into the browser, it will forward the request to contacts.html. This contacts request is mapped to contollContacts() method in HelloWorldController class. So, contollContacts() method will be executed and it will return the ‘contact’ string after completion of method execution.

package in.javatutorials.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
*
* @author Javatutorials
*
*/
@Controller
public class HelloWorldController {

/**
* Controls the ‘contacts’ and return to respective layout page
* @return
*/
@RequestMapping(value = “/contacts”, method = RequestMethod.GET)
public String contollContacts() {

String message = “Hi, You are in Controller !!!”;
System.out.println(message);

return “contact”;
}

}

 

spring-context.xml:

As the Tiles framework has been configured in the spring-context file as below with UrlBasedViewResolver, whenever contact will be returned by contollContacts() methods, request will be reached to tiles.xml and contact tile configuration will be loaded.

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:mvc=”http://www.springframework.org/schema/mvc&#8221;
xmlns:context=”http://www.springframework.org/schema/context&#8221;
xsi:schemaLocation=”
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd”&gt;

<context:component-scan base-package=”in.javatutorials.spring.controller” />
<mvc:annotation-driven />

<bean id=”viewResolver”
class=”org.springframework.web.servlet.view.UrlBasedViewResolver”>
<property name=”viewClass”>
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id=”tilesConfigurer”
class=”org.springframework.web.servlet.view.tiles2.TilesConfigurer”>
<property name=”definitions”>
<list>
<value>/config/tiles.xml</value>
</list>
</property>
</bean>

</beans>

 

tiles.xml:

Below will be the tiles configuration made for our sample application. contact tiles definition is extending to base.definition and base definition will be using layout.jsp as the layout file for our application.

<?xml version=”1.0″ encoding=”UTF-8″ ?>
<!DOCTYPE tiles-definitions PUBLIC
“-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN”
http://tiles.apache.org/dtds/tiles-config_2_0.dtd”&gt;

<tiles-definitions>
<definition name=”base.definition” template=”/jsp/layout.jsp”>
<put-attribute name=”title” value=”” />
<put-attribute name=”header” value=”/jsp/header.jsp” />
<put-attribute name=”menu” value=”/jsp/menu.jsp” />
<put-attribute name=”body” value=”” />
<put-attribute name=”footer” value=”/jsp/footer.jsp” />
</definition>

<definition name=”contact” extends=”base.definition”>
<put-attribute name=”title”
value=”Sample Spring MVC With Tiles Appliacation” />
<put-attribute name=”body” value=”/jsp/contact.jsp” />
</definition>

</tiles-definitions>

layout.jsp:

This layout.jsp is divided the complete application page as header, menu, body, footer. So, once the layout.jsp called, all the other jsps will be included as per the configuration mentioned in the tiles.xml file.

<%@ taglib uri=”http://tiles.apache.org/tags-tiles&#8221; prefix=”tiles”%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
http://www.w3.org/TR/html4/loose.dtd”&gt;
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title><tiles:insertAttribute name=”title” ignore=”true” /></title>
</head>
<body>
<table border=”1″ cellpadding=”2″ cellspacing=”2″ align=”center”>
<tr>
<td height=”30″ colspan=”2″><tiles:insertAttribute name=”header” />
</td>
</tr>
<tr>
<td height=”250″><tiles:insertAttribute name=”menu” /></td>
<td width=”350″><tiles:insertAttribute name=”body” /></td>
</tr>
<tr>
<td height=”30″ colspan=”2″><tiles:insertAttribute name=”footer” />
</td>
</tr>
</table>
</body>
</html>

 

header.jsp:

<h1>Header</h1>

 

menu.jsp:

<p>Menu</p>

 

footer.jsp:

<h1>Footer page</h1>

 

contact.jsp:

<%@taglib uri=”http://www.springframework.org/tags/form&#8221; prefix=”form”%>
<html>
<head>
<title>Sample Spring MVC With Tiles Appliacation</title>
</head>
<body>
<h2>Sample Spring MVC With Tiles Appliacation</h2>
<%
System.out.println(“Contact jsp”);
%>

</body>
</html>

 

Final Output of the Application:

When ever user hits the below url in the browser, application will be loaded as mentioned in the below screenshot.

http://localhost:8080/SpringMVC/

Sample Spring MVC With Tiles framework application
Sample Spring MVC With Tiles framework application

 

To download complete application , please click on the link below : Click here

Other Useful links:

Basic Spring MVC Application

6 thoughts on “Spring MVC with Tiles framework sample application

  1. Vicky August 20, 2013 / 2:30 am

    Got stuck with Tiles.
    How dispatchServlet will know that a particular view should be resolved with a particular resolver? eg. I am using two resolvers InternalResourceViewResolver or TilesViewResolver?
    Now, I returned some view name that was supposed to resolved with TilesViewResolver but it got resolved with InternalResourceViewResolver and it tried to find that jsp. Whereas, actual jsp name should come after consulting TilesViewResolver! What’s wrong here?
    I am using plain JSPs also that doesn’t have anything to do with Tiles. Should I use only plain JSPs or Tiles only?

    Please explain. Let me know if you have any query.

    Thanks.

    NOTE:
    Getting the below exception. Here Dispatcher servlet is calling InterviewViewResolver instead of TilesViewResolver? May be because of this attribute’ title’ is not visible there.

    org.apache.tiles.template.NoSuchAttributeException: Attribute ‘title’ not found.
    org.apache.tiles.template.DefaultAttributeResolver.computeAttribute(DefaultAttributeResolver.java:50)
    org.apache.tiles.template.GetAsStringModel.resolveAttribute(GetAsStringModel.java:152)
    org.apache.tiles.template.GetAsStringModel.execute(GetAsStringModel.java:110)
    org.apache.tiles.jsp.taglib.GetAsStringTag.doTag(GetAsStringTag.java:261)
    org.apache.jsp.WEB_002dINF.jsp.showTilePage_jsp._jspx_meth_tiles_005fgetAsString_005f0(showTilePage_jsp.java:117)
    org.apache.jsp.WEB_002dINF.jsp.showTilePage_jsp._jspService(showTilePage_jsp.java:62)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

  2. Mallik August 21, 2013 / 3:21 pm

    When u are using two resolvers it is better to use different attributes fr both the resolvers to avoidcnfusion.

    I coulnt understand why u need use above mentioned two view resolvers using both we can achieve similer kind of output.

    Can u let me knw, wht is the exact reason to cnfigure abve resolvers in a single application ?

  3. Vicky August 21, 2013 / 4:04 pm

    Thanks. I solved it by specifying ORDER attribute on resolvers.
    Since I am newbie, so I created sample application with these resolvers.But I didn’t get one point above. How you can get similar kind of result using both resolvers? Both resolvers are different, isn’t it?

    • Mallik August 24, 2013 / 12:16 pm

      Great… Thanx….

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.