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

Advertisements
About

I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Tagged with: , , , , , , , , , ,
Posted in Spring
6 comments on “Spring MVC with Tiles framework sample application
  1. […] Spring MVC with Tiles framework sample application […]

  2. […] Spring MVC with Tiles framework sample application […]

  3. Vicky says:

    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)

  4. Mallik says:

    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 ?

  5. Vicky says:

    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?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 677 other followers

Calendar
July 2013
M T W T F S S
« Jun   Aug »
1234567
891011121314
15161718192021
22232425262728
293031  
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 237,369 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100
All tags
AngularJS Apache Cassandra Arrays in Java Cassandra Cassandra Database Cassandra examples client stubs creating a java web service creating a web service using java difference between interface and abstract class Differences between SOAP and RESTfull webservices Display div as Overlay using JQuery download the spring basic application File directory Generating webservice client stubs Hibernate hibernate exceptions How to display over using JQuery how to generated web service client stubs in STS How to implement Spring basic application how to write singleton design pattern in java interface in java Java Java Arrays Java Example Programs Java examples Java interview questions java programs java project Java sample Programs Java Script JavaScript JavaServer Faces Java Sorting alogorithms Java Tutorials javatutorials Java Web services JDBC open shift openshift Overlay implementation using JQuery Overlay in JQuery POI API POI Examples public class public static void main Redhat OpenShift representational state transfer Restfull webservices sample programs in java Service-oriented architecture Singleton design pattern in java SOAP SOAP Webservices software Spring Framework Spring Interview Questions Spring MVC Spring MVC Example program Spring MVC sample Program Spring Sample Programs Spring Tutorials Struts technology Top Java Interview questions w3c dom Web service web service client Web Service Description Language webservices Web services writing singleton design pattern in Java WSDL Xml xml parsers