2014년 10월 23일 목요일

SPRING+TILES 설정하고 사용하기

일단,
1. pom.xml에

<!-- tiles start -->
<!-- Tiles --> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-core</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-servlet</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-jsp</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-extras</artifactId> <version>2.2.2</version> </dependency>
<!-- tiles end -->

를 추가한다.


http://mvnrepository.com/에서 받아올 수 있음


2. servlet-contex.xml에 bean을 등록 한다.

<!-- tiles -->
<beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<beans:property name="definitions">
<beans:list>
<beans:value>/WEB-INF/tilese/tiles-layout.xml</beans:value>
</beans:list>
</beans:property>
<beans:property name="preparerFactoryClass" value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory"/>
</beans:bean>
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
<beans:property name="order" value="1" />
</beans:bean>
<!-- tiles -->



3. tiles-layou.xml 만들기
/WEB-INF/tiles/tiles-layout.xml 을 만들고,


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name="layout-tiles" template="/WEB-INF/views/tiles/main/mainTemplate.jsp">
<put-attribute name="content" value=""></put-attribute>
<put-attribute name="navi" value="/WEB-INF/views/tiles/main/navi.jsp" ></put-attribute>
<put-attribute name="top" value="/WEB-INF/views/tiles/main/top.jsp" ></put-attribute>
<put-attribute name="footer" value="/WEB-INF/views/tiles/main/footer.jsp" ></put-attribute>
</definition>

<definition name="*.tiles" extends="layout-tiles">
<put-attribute name="content" value="/WEB-INF/views/{1}.jsp"></put-attribute>
</definition>
<definition name="*/*.tiles" extends="layout-tiles">
<put-attribute name="content" value="/WEB-INF/views/{1}/{2}.jsp"></put-attribute>
</definition>
<definition name="*/*/*.tiles" extends="layout-tiles">
<put-attribute name="content" value="/WEB-INF/views/{1}/{2}/{3}.jsp"></put-attribute>
</definition>
</tiles-definitions>


 <!-- no tiles -->
 <definition name="no-layout" templateExpression="/WEB-INF/views/tiles/no/noTemplate.jsp">
        <put-attribute name="content" value="" />
 </definition>

 <definition name="/*.notiles" extends="no-layout">
        <put-attribute name="content" expression="/WEB-INF/views/{1}.jsp" />
 </definition>

</tiles-definitions>​




4. mainTemplate.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>
<meta charset="UTF-8">

</head>
<body>
 <div class="mainSection">
  <div class="top">
   <tiles:insertAttribute name="top" />
  </div>
  <div class="navi">
   <tiles:insertAttribute name="navi" />
  </div>
  <div class="content">
   <tiles:insertAttribute name="content" />
  </div>
  <div class="footer">
   <tiles:insertAttribute name="footer" />
  </div>
 </div>

</body>
</html>



이런식으로 쓰면 된다. 


댓글 없음:

댓글 쓰기