템플릿 설정하기(타일즈)

  • 이 블로그의 글은 JAVA DB 엔지니어링 과정 수업을 위해 작성된 글입니다.

  • 이미지로 인해 글이 길어질 수 있습니다.

  1. 타일즈 의존성 추가 (pom.xml)

  2. View Resolver 등록 (servlet-context.xml)

  3. 타일즈 설정 추가

  4. 기본 레이아웃 설정

  5. 적용

  6. git 관리

타일즈 의존성 추가

  • pom.xml 파일 안 <proertiies> 태그 안에 스프링 프레임워크 버전 수정

1
<org.springframework-version>5.1.8.RELEASE</org.springframework-version>
cs

  • pom.xml 파일안 <proertiies> 태그 안에 타일즈 버전 추가

1
<org.apache.tiles-version>3.0.8</org.apache.tiles-version>
cs


  • pom.xml 파일 안 <dependencies> 태그 안에 의존성 추가

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    <!-- Tiles -->
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-servlet</artifactId>
        <version>${org.apache.tiles-version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-api</artifactId>
        <version>${org.apache.tiles-version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>${org.apache.tiles-version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>${org.apache.tiles-version}</version>
    </dependency> 
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-template</artifactId>
        <version>${org.apache.tiles-version}</version>
    </dependency>
    cs


  • 위 단계에서 다음과 같은 에러가 나올 시

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    Failure to transfer commons-beanutils:commons-beanutils:jar:1.8.0 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact commons-beanutils:commons-beanutils:jar:1.8.0 from/to central (https://repo.maven.apache.org/maven2): The operation was cancelled.
    org.eclipse.aether.transfer.ArtifactTransferException: Failure to transfer commons-beanutils:commons-beanutils:jar:1.8.0 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact commons-beanutils:commons-beanutils:jar:1.8.0 from/to central (https://repo.maven.apache.org/maven2): The operation was cancelled.
        at org.eclipse.aether.internal.impl.DefaultUpdateCheckManager.newException(DefaultUpdateCheckManager.java:226)
        at org.eclipse.aether.internal.impl.DefaultUpdateCheckManager.checkArtifact(DefaultUpdateCheckManager.java:192)
        at org.eclipse.aether.internal.impl.DefaultArtifactResolver.gatherDownloads(DefaultArtifactResolver.java:564)
        at org.eclipse.aether.internal.impl.DefaultArtifactResolver.performDownloads(DefaultArtifactResolver.java:482)
        at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:400)
        at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:225)
        at org.eclipse.aether.internal.impl.DefaultRepositorySystem.resolveDependencies(DefaultRepositorySystem.java:335)
        at org.apache.maven.project.DefaultProjectDependenciesResolver.resolve(DefaultProjectDependenciesResolver.java:202)
        at org.apache.maven.project.DefaultProjectBuilder.resolveDependencies(DefaultProjectBuilder.java:225)
        at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:198)
        at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:122)
        at org.eclipse.m2e.core.internal.embedder.MavenImpl.readMavenProject(MavenImpl.java:630)
        at org.eclipse.m2e.core.internal.project.registry.DefaultMavenDependencyResolver.resolveProjectDependencies(DefaultMavenDependencyResolver.java:63)
        at org.eclipse.m2e.core.internal.project.registry.ProjectRegistryManager.refreshPhase2(ProjectRegistryManager.java:551)
        at org.eclipse.m2e.core.internal.project.registry.ProjectRegistryManager.lambda$2(ProjectRegistryManager.java:515)
        at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:177)
        at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:151)
        at org.eclipse.m2e.core.internal.project.registry.ProjectRegistryManager.refresh(ProjectRegistryManager.java:513)
        at org.eclipse.m2e.core.internal.project.registry.ProjectRegistryManager.refresh(ProjectRegistryManager.java:365)
        at org.eclipse.m2e.core.internal.project.registry.ProjectRegistryManager.refresh(ProjectRegistryManager.java:312)
        at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod.getProjectFacade(MavenBuilder.java:155)
        at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod$1.call(MavenBuilder.java:90)
        at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:177)
        at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:151)
        at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:99)
        at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod.execute(MavenBuilder.java:87)
        at org.eclipse.m2e.core.internal.builder.MavenBuilder.build(MavenBuilder.java:201)
        at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:833)
        at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
        at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:220)
        at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:263)
        at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:316)
        at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
        at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:319)
        at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:371)
        at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:392)
        at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:154)
        at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:244)
        at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
    cs
  • pom.xml 파일 안 <dependencies> 태그 안에 다음 의존성 추가

    1
    2
    3
    4
    5
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.8.3</version>
    </dependency>

    cs


View Resolver 등록 (servlet-context.xml)

  • servlet-context.xml에 다음 코드를 추가

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <beans:bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
        <beans:property name="order" value="1" /><!-- 순서를 우선으로 지정 -->
    </beans:bean>
         
    <beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <beans:property name="definitions">
            <beans:list>
                <beans:value>/WEB-INF/spring/tiles-def.xml</beans:value>
            </beans:list>
        </beans:property>
    </beans:bean>

    cs


타일즈 설정 추가

  • WEB-INF > spring 폴더에 tiles-def.xml 파일 생성 후 다음 코드 붙여넣기

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    <?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>
        <!-- (1) 레이아웃을 적용하지않는화면-->    
        <definition name=".login" template="/WEB-INF/views/layout/loginLayout.jsp">
            <put-attribute name="title" value="" />
            <put-attribute name="header" value="/WEB-INF/views/common/header.jsp" />
            <put-attribute name="menu" value="" />        
            <put-attribute name="footer" value="" />
        </definition>
     
        <!-- (2) 레이아웃을 적용하는화면-->
        <definition name=".root" template="/WEB-INF/views/layout/baseLayout.jsp">
            <put-attribute name="title" value="" />
            <put-attribute name="header" value="/WEB-INF/views/common/header.jsp" />
            <put-attribute name="menu" value="/WEB-INF/views/common/menu.jsp" />        
            <put-attribute name="footer" value="/WEB-INF/views/common/footer.jsp" />
        </definition>
        
        <!-- (1) -->    
        <definition name="/login/*" extends=".login">
          <put-attribute name="body" value="/WEB-INF/views/login/{1}.jsp" />
        </definition>
     
        <!-- (2) -->
       <definition name="/*/*" extends=".root">      
          <put-attribute name="body" value="/WEB-INF/views/{1}/{2}.jsp" />
        </definition>
    </tiles-definitions>

    cs



기본 레이아웃 설정

  • 위에서 설정한 경로에 jsp 파일 생성

  • baseLayout.jsp

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
     
    <html>
    <head>
    <title>스프링</title>
    <jsp:include page="/WEB-INF/views/include/head.jsp"></jsp:include>
    </head>
    <body>
        <tiles:insertAttribute name="header"/>
        <div class="container">        
            <tiles:insertAttribute name="body" />
        </div>                                                  
        <tiles:insertAttribute name="footer" />
    </body>
    </html>
    cs
  • 폴더 구조

적용

  • 컨트롤러를 다음과 같이 수정

    1
    2
    3
    4
    5
    6
    @RequestMapping(value= {"/main/home","/home.do"})
    public ModelAndView openTilesView(ModelAndView mv) throws Exception{
        mv.setViewName("/main/home");
        mv.addObject("setHeader""타일즈");
        return mv;
    }

    cs


git 관리

  • GitKraken 실행

  • 커밋 후 푸쉬


자세한 각 jsp 파일 내용 확인




출처: https://epthffh.tistory.com/entry/스프링-타일즈-Spring-Tile-설정해보기 [물고기 개발자의 블로그]

+ Recent posts