java-school logo
Last Modified 2018.04.29

스프링 MVC에서 아파치 타일즈 사용하기

예제 다운로드
다운로드 한 파일의 압축을 풀고, pom.xml 파일이 있는 곳에서 mvn jetty:run을 실행한다.
http://localhost:8080을 방문하여 테스트한다.

의존성 추가

pom.xml
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-extras -->
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-extras</artifactId>
    <version>3.0.8</version>
</dependency>

JSPs

/WEB-INF/views/layouts/main-layout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE html>
<html lang="<tiles:insertAttribute name="lang" defaultValue="en" defaultValueType="string" />">
<head>
<meta charset="UTF-8" />
<meta name="Keywords" content="<tiles:insertAttribute name="keywords" />" />
<meta name="Description" content="<tiles:insertAttribute name="description" />" />
<title><tiles:insertAttribute name="title" /></title>
<link rel="stylesheet" href="/css/screen.css" type="text/css" />
<script type="text/javascript" src="/js/jquery-3.0.0.min.js"></script>
</head>
<body>

	<div id="wrap">

		<div id="header">
			<tiles:insertAttribute name="header" />
		</div>

		<div id="main-menu">
			<tiles:insertAttribute name="main-menu" />
		</div>

		<div id="container">
			<div id="content">
				<tiles:insertAttribute name="content" />
			</div>
		</div>

		<div id="sidebar">
			<tiles:insertAttribute name="sidebar" />
		</div>

		<div id="extra">
			<tiles:insertAttribute name="extra" />
		</div>

		<div id="footer">
			<tiles:insertAttribute name="footer" />
		</div>

	</div>

</body>
</html>

타일즈 정의 파일

/WEB-INF/tiles.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC  
        "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"  
        "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
	<definition name="app.default" template="/WEB-INF/views/layouts/main-layout.jsp">
		<put-attribute name="header" value="/WEB-INF/views/inc/header.jsp" />
		<put-attribute name="main-menu" value="/WEB-INF/views/inc/main-menu.jsp" />
		<put-attribute name="extra" value="/WEB-INF/views/inc/extra.jsp" />
		<put-attribute name="footer" value="/WEB-INF/views/inc/footer.jsp" />
	</definition>
	<!-- Homepage -->
	<definition name="index" extends="app.default">
		<put-attribute name="title" value="Homepage" />
		<put-attribute name="keywords" value="homepage" />
		<put-attribute name="description" value="Welcome to Java-School" />
		<put-attribute name="content" value="/WEB-INF/views/index.jsp" />
		<put-attribute name="sidebar" value="/WEB-INF/views/index-sub.jsp" />
	</definition>
</tiles-definitions>

한글을 위한 tiles_ko.xml를 생성한다.

/WEB-INF/tiles_ko.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC  
        "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"  
        "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
	<definition name="app.default" template="/WEB-INF/views/layouts/main-layout.jsp">
		<put-attribute name="lang" value="ko" type="string" />
		<put-attribute name="header" value="/WEB-INF/views/inc/header.jsp" />
		<put-attribute name="main-menu" value="/WEB-INF/views/inc/main-menu.jsp" />
		<put-attribute name="extra" value="/WEB-INF/views/inc/extra.jsp" />
		<put-attribute name="footer" value="/WEB-INF/views/inc/footer.jsp" />
	</definition>
	<!-- Homepage -->
	<definition name="index" extends="app.default">
		<put-attribute name="title" value="홈페이지" />
		<put-attribute name="keywords" value="홈페이지" />
		<put-attribute name="description" value="자바스쿨에 오신 걸 환영합니다." />
		<put-attribute name="content" value="/WEB-INF/views/index_ko.jsp" />
		<put-attribute name="sidebar" value="/WEB-INF/views/index-menu_ko.jsp" />
	</definition>
</tiles-definitions>

타일즈 뷰 리졸버 등록

spring-bbs-servlet.xml
<!-- ViewResolver -->
<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.xml</value>
		</list>
	</property>
</bean>

기존 뷰 리졸버는 제거한다.

테스트

/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<h1>Home</h1>
/index_ko.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<h1>홈</h1>
index-sub.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<h1>Home</h1>
index-sub_ko.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<h1>홈</h1>
관련 글