[Spring] Header, Footer 넣기 (Apache Tiles)

Apache Tiles를 이용하여 Header와 Footer를 설정해본다.

build.gradle

dependencies에 tiles 추가.

Build.gradle
compile('org.apache.tiles:tiles-jsp:3.0.4')

File 생성

tiles.xml

/WEB-INF/ 아래 tiles.xml 생성

https://tiles.apache.org/framework/tutorial/basic/pages.html 참조

tiles.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
<!-- template -->
<definition name="layout.default" template="/WEB-INF/views/jsp/default.jsp">
<put-attribute name="header" value="/WEB-INF/views/jsp/header.jsp" />
<put-attribute name="footer" value="/WEB-INF/views/jsp/footer.jsp" />
</definition>
<!-- pages -->
<!-- 여기서 name의 index, login은 Controller에서 RequestMapping 할 때 return해주는 page 이름과 맞춰준다. -->
<definition name="index" extends="layout.default">
<put-attribute name="body" value="/WEB-INF/views/jsp/index.jsp" />
</definition>
<definition name="login" extends="layout.default">
<put-attribute name="body" value="/WEB-INF/views/jsp/login.jsp" />
</definition>
</tiles-definitions>

/WEB-INF/tiles/ 아래 header.jsp, footer.jsp 파일 생성

header.jsp
1
2
3
<div id="header" style="height:200px;background-color:#607d8b;color:#ffffff;">
Here is header
</div>
footer.jsp
1
2
3
<div id="footer" style="height:200px;background-color:#607d8b;color:#ffffff;">
Here is footer
</div>

default.jsp 파일은 기본적인 layout이라고 보면 될 것 같다. 전체 layout의 변경은 이 파일에서 이루어진다.

header, footer 형식 말고도 여러가지 layout들을 결합하여 사용할 수 있다.

default.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ 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>
<link rel="stylesheet" href="/css/style.css" />
</head>
<body>
<tiles:insertAttribute name="header" />
<tiles:insertAttribute name="body" />
<tiles:insertAttribute name="footer" />
</body>
</html>

Config 파일 생성

TilesConfig class 생성.

TilesConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.lazyrodi.binbang.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesView;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
@Configuration
public class TilesConfig
{
@Bean
public TilesConfigurer tilesConfigurer() {
final TilesConfigurer configurer = new TilesConfigurer();
configurer.setDefinitions(new String[] {"WEB/INF/tiles/tiles.xml"});
configurer.setCheckRefresh(true);
return configurer;
}
@Bean
public TilesViewResolver tilesViewResolver() {
final TilesViewResolver resolver = new TilesViewResolver();
resolver.setViewClass(TilesView.class);
return resolver;
}
}

참조

Share