Spring 3 MVC: Tiles Plugin Tutorial with Example in Eclipse

 

Welcome to Part 4 for Spring 3.0 MVC Series. In previous article we saw how to create a form using Spring 3 MVC and display it in JSP. Also we learn about annotation @ModelAttribute.

In this part we will discuss about Tiles Framework and its Integration with Spring 3.0 MVC. We will add Tiles support to our HelloWorld Spring application that we created in previous parts. I strongly recommend you to go through previous articles and download the source code of our sample application.

Introduction to Tiles 2

Nowadays, website are generally divided into pieces of reusable template that are being rendered among different web pages. For example a site containing header, footer, menu etc. This items remains same through out the website and give it a common look and feel. It is very difficult to hard code this in each and every webpage and if later a change is needed than all the pages needs to be modified. Hence we use templatization mechanism. We create a common Header, Footer, Menu page and include this in each page.

Tiles Plugin allow both templating and componentization. In fact, both mechanisms are similar: you
define parts of page (a “Tile”) that you assemble to build another part or a full page. A part can
take parameters, allowing dynamic content, and can be seen as a method in JAVA language. Tiles is a templating system used to maintain a consistent look and feel across all the web pages of a web application. It increase the reusability of template and reduce code duplication.

A common layout of website is defined in a central configuration file and this layout can be extended across all the webpages of the web application.

Our Application Layout

Our goal is to add Header, Footer and Menu to our Spring 3 HelloWorld application. Following will be the layout of the same.

tiles-framework-layout

Required JAR files

In order to add Tiles support to our Spring3 application, we will need few jar files. Following is the list of JARs in our example. Add these JARs in WEB-INF/lib folder.
spring-3-tiles-framework-jar
The highlighted jar files in above list are the new jars to be added in project for Tiles integration.

Configuring Tiles framework in Spring MVC

To configure Tiles, an entry for bean TilesConfigure has to be made in spring-servlet.xml. Open the spring-servlet.xml from WEB-INF folder and add following code between <beans> </beans> tag.

File: /WebContent/WEB-INF/spring-servlet.xml

<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>/WEB-INF/tiles.xml</value>
			</list>
		</property>
	</bean>

An input configuration file /WEB-INF/tiles.xml is passed as argument in above bean definition. This file contains the Tiles definition for our web application.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

Create a file tiles.xml in WEB-INF folder and copy following code into it.
tiles-xml-spring-mvc

File: WebContent/WEB-INF/tiles.xml

<?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">
<tiles-definitions>
	<definition name="base.definition"
		template="/WEB-INF/jsp/layout.jsp">
		<put-attribute name="title" value="" />
		<put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
		<put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" />
		<put-attribute name="body" value="" />
		<put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />
	</definition>

	<definition name="contact" extends="base.definition">
		<put-attribute name="title" value="Contact Manager" />
		<put-attribute name="body" value="/WEB-INF/jsp/contact.jsp" />
	</definition>

</tiles-definitions>

Here in tiles.xml we have define a template base.definition. This layout contains attributes such as Header, Title, Body, Menu and Footer. The layout is then extended and new definitions for Contact page. We have override the default layout and changed the content for Body and Title.

Creating View – The JSPs

spring-tiles-jsp-files

We will define the template for our webapplication in a JSP file called layout.jsp. This template will contain different segments of web page (Header, Footer, Menu etc). Create four new JSP files layout.jsp, header.jsp, menu.jsp and footer.jsp and copy following content in each of them.

File: WebContent/WEB-INF/jsp/layout.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<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>

File: WebContent/WEB-INF/jsp/header.jsp

<h1>Header</h1>

File: WebContent/WEB-INF/jsp/menu.jsp

<p>Menu</p>

File: WebContent/WEB-INF/jsp/footer.jsp

<p>Copyright &copy; ViralPatel.net</p>

That’s All Folks

Compile and Execute the application in Eclipse and see that the header, menu and footer are properly applied.
spring-tiles-demo-screen-contact-manager

Download Source Code

Click here to download Source Code Spring3MVC-part3.zip

Moving On

Today we saw how we can configure Tiles framework with Spring 3 MVC application. We used org.springframework.web.servlet.view.tiles2.TilesConfigurer class in bean definition to define the tiles configuration file. In next part we will discuss about Internationalization/Localization and adding its support in Spring 3 MVC. I hope you liked this article. Feel free to post your queries and comments in comment section.

 
본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
26 [bootstrap] spring 프로젝트에 bootstrap 적용 file 졸리운_곰 2017.01.29 164
25 eGov 표준프레임워크(전자정부프레임워크) 공통 컴포넌트 사용법 : Common Component file 졸리운_곰 2017.01.29 1030
24 Passing Javascript object and object list to Spring controller 졸리운_곰 2017.01.23 92
23 뷰에 모델(Model) 전달 file 졸리운_곰 2017.01.22 128
22 CentOS에서 Apache Tomcat 설치하기 졸리운_곰 2017.01.18 490
21 Spring MVC Tiles Plugin with Example file 졸리운_곰 2017.01.15 1384
20 Spring MVC Tiles 3 Integration Tutorial file 졸리운_곰 2017.01.15 173
» Spring 3 MVC: Tiles Plugin Tutorial with Example in Eclipse file 졸리운_곰 2017.01.15 194
18 MyBatis 에서 한 insert 태그로 여러 Insert문 수행 졸리운_곰 2016.12.09 112
17 Web App Architecture - the Spring MVC - AngularJs stack file 졸리운_곰 2016.11.20 208
16 Introduction to Angular 2 with Spring MVC file 졸리운_곰 2016.11.20 676
15 Migrating a Spring Web MVC application from JSP to AngularJS file 졸리운_곰 2016.11.20 114
14 스프링(Spring) MVC 프레임워크(Model View Controller Framework) file 졸리운_곰 2016.11.16 151
13 JSP 정리 졸리운_곰 2016.09.11 332
12 JSP 요약 정리 file 졸리운_곰 2016.09.11 1294
11 전자정부 eGov 프레임워크 모바일 실행환경 Upgrade 가이드 file 졸리운_곰 2016.08.02 327
10 전자정부 eGov 프레임워크 개발프레임워크 개발환경 졸리운_곰 2016.08.02 474
9 표준프레임워크 실행환경 3.5 졸리운_곰 2016.08.02 172
8 전자정부 표준프레임워크 3.5 기반 개발 시작하기(Getting Started) file 졸리운_곰 2016.08.02 291
7 스프링(Spring) MVC 프레임워크(Model View Controller Framework) file 졸리운_곰 2016.07.31 103
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED