스프링에서 Tiles를 적용하기 위해 pom.xml에 다음을 추가한다.
<properties>
<org.apache.tiles-version>2.2.2</org.apache.tiles-version>
<org.slf4j-version>1.7.5</org.slf4j-version>
</properties>
<dependencies>
...
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>${org.apache.tiles-version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-api</artifactId>
<version>${org.apache.tiles-version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>${org.apache.tiles-version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>${org.apache.tiles-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependencies>
spring-servlet.xml에 다음을 추가한다.
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</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>
샘플 컨트롤러 (Controller)를 다음과 같이 작성한다.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@Autowired
private HelloSpring helloSpring;
@RequestMapping("/hello")
public String handleRequest(@RequestParam("name") String name, Model model)
throws Exception {
String message = this.helloSpring.sayHello(name);
model.addAttribute("message", message);
return "hello";
}
}
/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="hello" extends="base.definition">
<put-attribute name="title" value="Hello" />
<put-attribute name="body" value="/WEB-INF/jsp/hello.jsp" />
</definition>
</tiles-definitions>
/WEB-INF/jsp/layout.jsp을 다음과 같이 작성한다.
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<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>
/WEB-INF/jsp/header.jsp을 다음과 같이 작성한다.
<h1>Header</h1>
/WEB-INF/jsp/footer.jsp을 다음과 같이 작성한다.
<p>Copyright © izeye.com</p>
/WEB-INF/jsp/menu.jsp을 다음과 같이 작성한다.
<p>Menu</p>
/WEB-INF/jsp/hello.jsp을 다음과 같이 작성한다.
${message}
레이아웃 (Layout)이 적용된 결과를 확인할 수 있다.
References:
http://viralpatel.net/blogs/spring-3-mvc-tiles-plugin-tutorial-example-eclipse/
http://tiles.apache.org/2.2/framework/getting_started.html
http://tiles.apache.org/2.2/framework/tutorial/index.html
'Spring' 카테고리의 다른 글
[ SpringBoot ] 2개 이상의 Datasource 운용하기 (0) | 2018.10.15 |
---|---|
[Maven] Maven 테스트. (0) | 2014.10.09 |
[ Spring ] Web Service - Restful 2 (0) | 2013.11.26 |
[ Spring ] Web Service - Restful 1 (0) | 2013.11.26 |
[Spring] Spring에서 import 시 "classpath:"와 "classpath*:" 의 차이점 (0) | 2012.08.29 |