2018년 4월 8일 일요일

Spring tiles3 설정

1. POM.xml 에 설정 추가

<dependency>
 <groupId>org.apache.tiles</groupId>
 <artifactId>tiles-core</artifactId>
 <version>3.0.8</version>
</dependency>
<dependency>
 <groupId>org.apache.tiles</groupId>
 <artifactId>tiles-servlet</artifactId>
 <version>3.0.8</version>
</dependency>
<dependency>
 <groupId>org.apache.tiles</groupId>
 <artifactId>tiles-jsp</artifactId>
 <version>3.0.8</version>
</dependency>
<dependency>
 <groupId>org.apache.tiles</groupId>
 <artifactId>tiles-el</artifactId>
 <version>3.0.8</version>
</dependency>

2./WEB-INF/layout 경로에 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="layout-tiles" template="/WEB-INF/layout/classic.jsp">
        <put-attribute name="header" value="/WEB-INF/layout/header.jsp" />
        <put-attribute name="content" value="" />
        <put-attribute name="footer" value="/WEB-INF/layout/footer.jsp" />
    </definition>

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

</tiles-definitions>

3. tiles.xml에 value에 설정한 jsp파일들을 만들고 definition에서 설정한 'template' 경로에 다음과 같이 파일을 만든다.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE html>
<html>
<head>
    <tiles:insertAttribute name="header" />
</head>
<body>
<div id="wrapper">
    <tiles:insertAttribute name="content" />
    <tiles:insertAttribute name="footer" />
</div>
</body>
</html>

4. WebMvcConfigurerAdapter를 상속 받은 클래스에 소스를 추가한다.

    @Bean
    public TilesConfigurer tilesConfigurer(){
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions(new String[] {"/WEB-INF/**/tiles.xml"});
        tilesConfigurer.setCheckRefresh(true);
        return tilesConfigurer;
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        TilesViewResolver viewResolver = new TilesViewResolver();
        registry.viewResolver(viewResolver);
        viewResolver.setOrder(1);
    }
Share:

0 개의 댓글:

댓글 쓰기