Eclipse

[Eclipse] - Tiles 설정

aieo 2024. 6. 7. 18:00
반응형

큰 프로젝트나 유지보수 관리 등을 위해서 Tiles 설정으로 해서 사용하는게 좋다고 함.

 

프로젝트 생성은 eGovFramework로 생성 했고 기본적인건 다 설정 하고 main index.jsp로 테스트 먼저 함

 

1. dispathcer-servlet.xml 설정

<!-- JsonView 설정 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" id="viewResolver" p:order="0"/>
<bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
	<property name="contentType" value="application/json;charset=UTF-8"> </property>
</bean>

 

중간 부분에 위에 소스 추가 하고 아래 이미지 처럼 하단 소스 주석이랑 수정 해줌

추가로 package를 egovframework를 사용 안하고 본인이 원하는 package에 패키지명을 설정한다면

<context:component-scan base-package="packageName"> 으로 수정 해주기 "packageName"에 원하는 패키지명 설정하고 맞춰주면 됨

 

2. pom.xml 의존성 추가 해주기

- Spring Boot 사용시 Tomcat-embed-jasper, jstl 의존성 추가

- jstl: Spring Boot에서 JSTL을 사용 할 수 있게 도와주는 패키지임

 

<!-- JSP -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>

<!-- tiles -->
<dependency>
	<groupId>org.apache.tiles</groupId>
	<artifactId>tiles-core</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-servlet</artifactId>
	<version>3.0.8</version>
</dependency>

 

3. Tiles 관련 Bean 등록

 - Tiles 관련 Bean을 등록하여, Tiles 를 통해 ViewResolver 가 동작하도록 설정

 - xml 을 통해 java code로 Bean 등록

   - 패키지 경로 추가

 - TilesConfig.java 에다가 아래 소스입력

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

@Configuration
public class TilesConfig {

	@Bean
	public TilesConfigurer tilesConfigurer() {
		final TilesConfigurer configurer = new TilesConfigurer();
		configurer.setDefinitions(new String[] {"/WEB-INF/tiles/tiles.xml"});
		configurer.setCheckRefresh(true);
		return configurer;
	}
	
	@Bean
	public TilesViewResolver tilesViewResolver() {
		final TilesViewResolver resolver = new TilesViewResolver();
		resolver.setViewClass(TilesView.class);
		resolver.setOrder(1);
		return resolver;
	}
}

 

4. Tiles 관련 설정

 - src/main/webapp/WEB-INF 폴더에 tiles 폴더 생성 후 tiles 관련 설정 파일인 tiles.xml을 생성한다.

 - tiles.xml : tiles를 통해 보여줄 view 위치를 설정할 파일

 - default.jsp : 요청된 화면만을 보여줄 jsp

 - jayout folder : 요청된 화명 외에 기본화면 (header, footer..)를 표시할 jsp

 

먼저 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="tiles-default" template="/WEB-INF/tiles/default.jsp">
        <put-attribute name="header" value="/WEB-INF/tiles/layouts/header.jsp"/>
        <put-attribute name="footer" value="/WEB-INF/tiles/layouts/footer.jsp"/>
    </definition>
    
    <definition name="*.main" extends="tiles-default">
        <put-attribute name="body" value="/WEB-INF/jsp/test/{1}.jsp" />
    </definition>
</tiles-definitions>

 

이제 tiles.xml 설정한거에 jsp 맞춰서 생성 후 안에 내용 추가 해주기

- default.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>test</title>
    
<style>
    .header {
        background-color : red;
        height : 150px;
    }
    
    .contents {
        background-color : green;
        height : 500px;
    }
    
    .footer {
        background-color : blue;
        height : 100px;
    }
</style>
</head>
<body>
	<tiles:insertAttribute name="header" />
	<tiles:insertAttribute name="body" />
    
</body>
</html>

<!--  footer.jsp -->
<div class="footer">Footer</div>

 

- header.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<div class="header">Header</div>

 

- footer.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<div class="footer">Footer</div>

 

 

경로 설정 하는거 잘 맞추고 오타 없으면 index.jsp가 controller 잘 타고 return 설정한 .main 으로 잘 감

나는 main으로 했지만 해당 프로젝트에 맞춰서 진행 하시면 됩니다.

반응형