Programing/Springboot

Spring boot 2.x apache tiles 적용 방법

리커니 2021. 8. 17.
반응형

Spring boot 2.x apache tiles 적용 방법

Springboot 2.x 에서 apache tiles를 적용하는 방법을 알아보도록 하겠습니다.

apache tiles 는 페이지의 레이아웃을 보다 편하고 보기좋은 코드를 만드는데 도움을 줍니다.

 

예를들어 header와 footer가 모든 페이지에 들어간다고 했을때,

apache tiles를 사용하지 않으면 각각의 페이지마다 include를 사용하여 추가해야 했습니다.

 

<%@ include file="/resource/layout/header.jsp" %>
.
. (페이지 코드)
. 
<%@ include file="/resource/layout/footer.jsp" %>

 

하지만 tiles를 사용하게 되면 각 페이지에 include를 하지 않아도 됩니다.

 

그럼 spring boot 2.x 에 적용하는 방법을 알아보겠습니다.

의존성 주입 도구는 gradle 입니다. maven이나 기타 다른 의존성 도구는 아래의 링크를 확인하세요.

 

Link : https://mvnrepository.com/artifact/org.apache.tiles/tiles-jsp/3.0.5

코드를 복사하여 build.gradle dependencies 에 추가해 줍니다.

 

implementation group: 'org.apache.tiles', name: 'tiles-jsp', version: '3.0.5'

 

Refresh Gradle Project를 하여 의존성을 주입합니다.

이제 tiles 설정을 위해 TilesConfig 클래스를 생성합니다.

TileConfig class 에서는 tiles.xml 설정파일 등록과 viewResolver에 등록작업을 합니다. 

화면 표출에 필요한 view를 찾아 리턴하게 하는 설정입니다.

 

[TilesConfig.java]

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesView;

@Configuration
public class TilesConfig {

	@Bean
	public UrlBasedViewResolver tilesViewResolver() {
		UrlBasedViewResolver tilesViewResolver = new UrlBasedViewResolver();
		tilesViewResolver.setViewClass(TilesView.class);
		return tilesViewResolver;
	}

	@Bean
	public TilesConfigurer tilesConfigurer() {
		TilesConfigurer tilesConfigurer = new TilesConfigurer();
		String[] defs = {"classpath*:/tiles/tiles.xml" };
		tilesConfigurer.setDefinitions(defs);
		return tilesConfigurer;
	}
}

 

이제 설정 파일에 필요한 tiles.xml 파일을 생성합니다.

 

[tiles.xml]

<?xml version="1.0" encoding="UTF-8" ?>
<!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>
   <definition name="popup-layout" template="/resource/layout/popup.jsp">
       <put-attribute name="body" value="" />
   </definition>
   <definition name="/popup/*" extends="popup-layout">
       <put-attribute name="body" value="/WEB-INF/jsp/popup/{1}.jsp" />
   </definition>
   
   <definition name="main-layout" template="/resource/layout/main.jsp">
       <put-attribute name="header" value="/resource/layout/header.jsp" />
       <put-attribute name="body" value="" />
       <put-attribute name="footer" value="/resource/layout/footer.jsp" />
   </definition>
   <definition name="/*" extends="main-layout">
       <put-attribute name="body" value="/WEB-INF/jsp/{1}.jsp" />
   </definition>
   <definition name="/*/*" extends="main-layout">
       <put-attribute name="body" value="/WEB-INF/jsp/{1}/{2}.jsp" />
   </definition>
</tiles-definitions>

main-layout은 header, body, footer 로 구분된 레이아웃 설정입니다.

header와 footer 는 고정으로 모든페이지에 추가되게되고, body로 설정한 부분만 변경되어 페이지가 출력됩니다.

 

팝업의 경우 body만 출력하면 되는 구조라 위와같이 추가해 주었습니다. popup-layout을 main-layout 아래 추가하게되면 main-layout이 우선적으로 적용되니 주의하세요!

 

이제 layout의 기본이 되는 main.jsp를 생성합니다.

 

[main.jsp]

<%@ 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 lang="ko">
  <head>
    <title>tiles test</title>
    <!--<meta name="viewport" content="width=device-width, initial-scale=1.0">-->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
  </head>
   <body>
      <tiles:insertAttribute name="header"/>
      <tiles:insertAttribute name="body"/>
      <tiles:insertAttribute name="footer"/>
  </body>
</html>

 

이제 설정은 끝났습니다.

각각의 경로에 맞게 jsp을 추가해주시면 됩니다.

반응형

댓글

💲 추천 글