Sun's Blog

Tiles 본문

ETC

Tiles

버스는그만 2024. 2. 4. 21:17

의존성 추가

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/jstl/jstl -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-extras</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>

Config 파일

@Configuration
public class TilesConfig {

        @Bean
        public TilesConfigurer tilesConfigurer() {
            TilesConfigurer tilesConfigurer = new TilesConfigurer();
            tilesConfigurer.setDefinitions("/WEB-INF/tiles.xml");
            tilesConfigurer.setCheckRefresh(true);
            tilesConfigurer.setPreparerFactoryClass(SimpleSpringPreparerFactory.class);
            return tilesConfigurer;
        }

        @Bean
        public TilesViewResolver tilesViewResolver() {
            TilesViewResolver viewResolver = new TilesViewResolver();
            viewResolver.setViewClass(TilesView.class);
            viewResolver.setOrder(1);

            return viewResolver;
        }
}

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="base" template="/WEB-INF/template/main.jsp">
        <put-attribute name="header" value="/WEB-INF/layout/header.jsp"/>
        <put-attribute name="body" value="/WEB-INF/layout/body.jsp"/>
        <put-attribute name="footer" value="/WEB-INF/layout/footer.jsp"/>
    </definition>

    <definition name="base/*" extends="base">
        <put-attribute name="body" value="/WEB-INF/layout/{1}.jsp"/>
    </definition>
</tiles-definitions>
  • definition: 화면에 보여줄 레이아웃 정의
    • name: 레이아웃명
    • template: 레이아웃 경로
    • extends: definition을 상속받고 재정의할 수 있다.
  • put-attribute: 해당 레이아웃에 들어갈 페이지
    • name: 페이지명
    • value: 페이지 경로
      • 와일드카드(*)를 사용할 수 있으며 {n}으로 n은 해당 와일드 카드 순서를 넣어주면 된다.

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>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<tiles:insertAttribute name="header" />
<tiles:insertAttribute name="body" />
<tiles:insertAttribute name="footer" />
</body>
</html>
  • <title:insertAttribute name="" />으로 해당 레이아웃에 원하는 페이지를 출력

컨트롤러

@Controller
public class HomeController {

    @GetMapping("/")
    public String goHome() {
        return "base";
    }

    @GetMapping("/base/{other}")
    public String goOther(@PathVariable String other) {
        return "base/" + other;
    }
}

결과

/localhost:8080

localhost:8080/base/other

 

구조

main
 ┣ java
 ┃ ┗ com
 ┃ ┃ ┗ example
 ┃ ┃ ┃ ┗ demo
 ┃ ┃ ┃ ┃ ┣ config
 ┃ ┃ ┃ ┃ ┃ ┗ TilesConfig.java
 ┃ ┃ ┃ ┃ ┣ home
 ┃ ┃ ┃ ┃ ┃ ┗ web
 ┃ ┃ ┃ ┃ ┃ ┃ ┗ HomeController.java
 ┃ ┃ ┃ ┃ ┗ DemoApplication.java
 ┣ resources
 ┃ ┣ static
 ┃ ┣ templates
 ┃ ┗ application.properties
 ┗ webapp
 ┃ ┗ WEB-INF
 ┃ ┃ ┣ layout
 ┃ ┃ ┃ ┣ body.jsp
 ┃ ┃ ┃ ┣ footer.jsp
 ┃ ┃ ┃ ┣ header.jsp
 ┃ ┃ ┃ ┗ other.jsp
 ┃ ┃ ┣ template
 ┃ ┃ ┃ ┗ main.jsp
 ┃ ┃ ┗ tiles.xml

이슈사항

  • Tiles는 Java 1.8까지만 지원하기 때문에 spring boot 같은 경우는 2.x 버전까지 지원해준다. 초기 프로젝트를 3.x 버전으로 만들었으며 tiles 관련 라이브러리가 없어서 당황했었음

'ETC' 카테고리의 다른 글

node-sass 사용해보기  (0) 2024.02.18
React 4일차: state  (1) 2024.02.04
리액트 3일차  (0) 2024.01.31
react 2일차  (0) 2024.01.29
React 1일차  (0) 2024.01.25