-
tiles 구조
tiles >
comm >
nav.jsp
footer.jsp
header.jsp
layout >
template.jsp
servlet-context.xml 추가
: tiles.xml 경로 설정
<!-- Tiles 뷰 리졸버 --> <beans:bean id="tielsViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" /> <beans:property name="order" value="1" /> </beans:bean> <!-- Tiles 설정 파일 --> <beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"> <beans:property name="definitions"> <beans:list> <beans:value>/WEB-INF/tiles/tiles.xml</beans:value> </beans:list> </beans:property> </beans:bean>
pom.xml 추가
: tiles maven
<!-- Tiles --> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-core</artifactId> <version>3.0.8</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-jsp</artifactId> <version>3.0.8</version> </dependency>
tiles.xml 작성
: tiles 공통부분 작성
<!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> <!-- 화면의 기초가 되는 base --> <definition name="base" template="/WEB-INF/tiles/layout/template.jsp"> <put-attribute name="nav" value="/WEB-INF/tiles/comm/nav.jsp" /> <put-attribute name="header" value="/WEB-INF/tiles/comm/header.jsp" /> <put-attribute name="footer" value="/WEB-INF/tiles/comm/footer.jsp" /> </definition> <!-- base를 상속받은 base/index --> <definition name="base/home" extends="base"> <put-attribute name="body" value="/WEB-INF/views/home.jsp" /> </definition> <!-- 동적 매핑. 만약 /user/login의 경로라면 /WEB-INF/views/user/login.jsp 뷰를 가져온다. --> <definition name="*" extends="base"> <put-attribute name="body" value="/WEB-INF/views/{1}.jsp" /> </definition> <definition name="*/*" extends="base"> <put-attribute name="body" value="/WEB-INF/views/{1}/{2}.jsp" /> </definition> <definition name="*/*/*" extends="base"> <put-attribute name="body" value="/WEB-INF/views/{1}/{2}/{3}.jsp" /> </definition> </tiles-definitions>
예제
header.jsp
<div style="background-color : gray; width:100%"> <h1>Hello ! I am Header!</h1> </div>
footer.jsp
<div style="background-color : gray; width:100%"> <h1>Hello ! I am Footer!</h1> </div>
nav.jsp
<div style="width:100%"> <h1>NAV!</h1> </div>
template.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %> <!DOCTYPE html> <html> <head> </head> <body> <tiles:insertAttribute name="header" /> <tiles:insertAttribute name="nav" /> <div id="container"> <tiles:insertAttribute name="body" /> </div> <tiles:insertAttribute name="footer" /> </body> </html>
home.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="false" %> <html> <head> <title>Home</title> </head> <body> <h1> Hello world! </h1> <P> The time on the server is ${serverTime}. </P> </body> </html>
home.jsp 실행
'기초 > SPRING' 카테고리의 다른 글
[Spring] Mysql 계층적 쿼리구조 (0) 2020.09.23 [Spring] Maria DB 연결+SQL LOG (0) 2020.09.09 Spring - Interceptor 적용 (0) 2020.08.28 Spring - Servelt 설정변경 (0) 2020.08.28 부트스트랩 적용 및 resources 지정 (0) 2020.08.28