Page layout 을 content 와 분리
-> 독립적인 page layout 정의하고, 구성 요소들을 동적으로 삽입
<jsp:include> 에 비해 유연성, 유지보수 편의성 증가
TilesConfigurer 이용하여 Tiles 설정 파일 지정
TilesViewResolver 사용하여 view resolving
<br>
servlet-with-tiles.xml
<!-- ========================= TILES VIEW DEFINITIONS ========================= -->
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<!-- Tiles definition file : 논리적인 view이름과 동일한 이름을 갖는 view 선택 -->
<list>
<value>/WEB-INF/jsp/tiles/template/tilesdef.xml</value>
</list>
</property>
</bean>
<!-- ========================= TILES VIEW RESOLVER ========================= -->
<bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.tiles3.TilesViewResolver">
<property name="order" value="1"/>
<!-- 우선 순위 1 -->
</bean>
<!-- 우선 순위2 : tiles view가 존재하지 않을 경우 JSP view 이용 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="order" value="2" />
<property name="contentType" value="text/html; charset=UTF-8"/>
</bean>
<br>
tiles/template/layout.jsp
<tiles:insertAttribute /> :외부 content 삽입 위치
<tiles:getAsString /> : 외부 text 삽입 위치
<%page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>
<!-- Tiles custom tag library 선언 -->
<html>
<head>
<title><tiles:getAsString name="title" /></title>
<!-- "title"이라는 이름 가진 외부 String 추가 -->
<!--<title>JPetStore Demo</title>-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
...
</head>
<body>
<!-- 영역의 레이아웃 정의 -->
<tiles:insertAttribute name="header" ignore="true"/>
<tiles:insertAttribute name="body" ignore="true"/>
<tiles:insertAttribute name="banner" ignore="true"/>
<tiles:insertAttribute name="footer" ignore="true"/>
</body>
</html>
<br>
tiles/template/tilesdef.xml
컨트롤러에서 메소드의 리턴값을 <tiles-definitions> 태그 안의 definition name 을 리턴값으로 함
<tiles-definitions>
<definition name="base_layout" template="/WEB-INF/jsp/tiles/template/layout.jsp">
<put-attribute name="title" value="JPetStore Demo" type="string"/>
<put-attribute name="header"
value="/WEB-INF/jsp/tiles/Top.jsp" />
<put-attribute name="footer"
value="/WEB-INF/jsp/tiles/Bottom.jsp" />
<put-attribute name="banner" value="" />
</definition>
<definition name="layout_with_banner" extends="base_layout">
<put-attribute name="banner"
value="/WEB-INF/jsp/tiles/Banner.jsp" />
</definition>
<definition name="index" extends="layout_with_banner">
<put-attribute name="body"
value="/WEB-INF/jsp/tiles/index.jsp" />
</definition>
<definition name="tiles/*" extends="base_layout">
<put-attribute name="body"
value="/WEB-INF/jsp/tiles/{1}.jsp" />
</definition>
</tiles-definitions>
<br>
Tiles definitions
<definition>tag 이용하여 layout과 view 정의
<put-attribute> 이용하여 view의 content를 생성할 JSP file 지정
Wildcard 이용
<definition name="tiles/*" extends="base_layout">
<put-attribute name="body"
value="/WEB-INF/jsp/tiles/{1}.jsp" />
</definition>
tiles/Category -> /WEB-INF/jsp/tiles/Category.jsp
'Computer Science > Spring' 카테고리의 다른 글
#9 HTTP Session 사용 (0) | 2018.06.16 |
---|---|
#8 Form 입력 값 검증 (0) | 2018.06.16 |
#7 Model Data (0) | 2018.05.26 |
#6 Command 객체 이용한 Form 전송 처리 (2) | 2018.05.25 |
#5 Spring MVC 구조 (0) | 2018.05.25 |