728x90
★ 새 프로젝트
- New > Spring Legacy Project > Spring MVC Project > "NonTilesTest" > "com.test.nontiles" > Finish
- New > Spring Legacy Project > Spring MVC Project > "TilesTest" > "com.test.tiles" > Finish
★ Tiles
- 레이아웃 프레임워크
- 페이지 레이아웃을 구성하고 관리하는 기술
- <@include file="">
■ 구현 기능
- 메인(index.do)
- 회원
- 회원정보(member/info.do)
- 활동내역(member/history.do)
- 즐겨찾기(member/favorite.do)
- 관리자
- 로그(admin/log.do)
- 환경설정(admin/setting.do)
■ 파일 생성
- "com.test.controller" > "MainController.java"
> "MemberController.java"
> "AdminController.java"
- views > "index.jsp"
- views > "member" > "info.jsp"
> "history.jsp"
> "favorite.jsp"
- views > "admin" > "log.jsp"
> "setting.jsp"
- views > "inc" > "main_menu.jsp"
> "member_menu.jsp"
> "admin_menu.jsp"
> "asset.jsp"
■ 의존성 추가 -> 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-jsp</artifactId>
<version>3.0.8</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-api</artifactId>
<version>3.0.8</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>3.0.8</version>
</dependency>
■ ServletContext.xml -> 추가
<beans:bean id="tielsViewResolver" 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/tiles.xml</beans:value>
</beans:list>
</beans:property>
</beans:bean>
■ layout을 깔고 -> 바뀌어야 할 부분들을 조각 페이지로 뺀다.
- admin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<tiles:insertAttribute name="asset"></tiles:insertAttribute>
<style>
</style>
</head>
<body>
<!-- -->
<header>
<h1>Tiles</h1>
<tiles:insertAttribute name="main_menu"></tiles:insertAttribute>
<tiles:insertAttribute name="admin_menu"></tiles:insertAttribute>
</header>
<tiles:insertAttribute name="content"></tiles:insertAttribute>
<script>
</script>
</body>
</html>
- member.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<tiles:insertAttribute name="asset"></tiles:insertAttribute>
<style>
</style>
</head>
<body>
<!-- -->
<header>
<h1>Tiles</h1>
<tiles:insertAttribute name="main_menu"></tiles:insertAttribute>
<tiles:insertAttribute name="member_menu"></tiles:insertAttribute>
</header>
<tiles:insertAttribute name="content"></tiles:insertAttribute>
<script>
</script>
</body>
</html>
- test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
</style>
</head>
<body>
<!-- -->
<h1>TEST</h1>
<tiles:insertAttribute name="menu"></tiles:insertAttribute>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
</script>
</body>
</html>
- tiles.xml -> definition을 통해 조각페이지 관리 -> 하지만 좋지 않은 방법
<?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개 == 화면 1장 == JSP 페이지 -->
<definition name="test" template="WEB-INF/views/layout/test.jsp">
<put-attribute name="menu" value="/WEB-INF/views/inc/main_menu.jsp"></put-attribute>
</definition>
<definition name="info" template="/WEB-INF/views/layout/member.jsp">
<put-attribute name="asset" value="/WEB-INF/views/inc/asset.jsp"></put-attribute>
<put-attribute name="main_menu" value="/WEB-INF/views/inc/main_menu.jsp"></put-attribute>
<put-attribute name="member_menu" value="/WEB-INF/views/inc/member_menu.jsp"></put-attribute>
<put-attribute name="content" value="/WEB-INF/views/member/info.jsp"></put-attribute>
</definition>
<definition name="history" template="/WEB-INF/views/layout/member.jsp">
<put-attribute name="asset" value="/WEB-INF/views/inc/asset.jsp"></put-attribute>
<put-attribute name="main_menu" value="/WEB-INF/views/inc/main_menu.jsp"></put-attribute>
<put-attribute name="member_menu" value="/WEB-INF/views/inc/member_menu.jsp"></put-attribute>
<put-attribute name="content" value="/WEB-INF/views/member/history.jsp"></put-attribute>
</definition>
<definition name="favorite" template="/WEB-INF/views/layout/member.jsp">
<put-attribute name="asset" value="/WEB-INF/views/inc/asset.jsp"></put-attribute>
<put-attribute name="main_menu" value="/WEB-INF/views/inc/main_menu.jsp"></put-attribute>
<put-attribute name="member_menu" value="/WEB-INF/views/inc/member_menu.jsp"></put-attribute>
<put-attribute name="content" value="/WEB-INF/views/member/favorite.jsp"></put-attribute>
</definition>
<definition name="log" template="/WEB-INF/views/layout/admin.jsp">
<put-attribute name="asset" value="/WEB-INF/views/inc/asset.jsp"></put-attribute>
<put-attribute name="main_menu" value="/WEB-INF/views/inc/main_menu.jsp"></put-attribute>
<put-attribute name="admin_menu" value="/WEB-INF/views/inc/admin_menu.jsp"></put-attribute>
<put-attribute name="content" value="/WEB-INF/views/admin/log.jsp"></put-attribute>
</definition>
<definition name="setting" template="/WEB-INF/views/layout/admin.jsp">
<put-attribute name="asset" value="/WEB-INF/views/inc/asset.jsp"></put-attribute>
<put-attribute name="main_menu" value="/WEB-INF/views/inc/main_menu.jsp"></put-attribute>
<put-attribute name="admin_menu" value="/WEB-INF/views/inc/admin_menu.jsp"></put-attribute>
<put-attribute name="content" value="/WEB-INF/views/admin/setting.jsp"></put-attribute>
</definition>
</tiles-definitions>
- tiles.xml -> 좋은 Solution
<definition name="member.*" template="/WEB-INF/views/layout/member.jsp">
<put-attribute name="asset" value="/WEB-INF/views/inc/asset.jsp"></put-attribute>
<put-attribute name="main_menu" value="/WEB-INF/views/inc/main_menu.jsp"></put-attribute>
<put-attribute name="member_menu" value="/WEB-INF/views/inc/member_menu.jsp"></put-attribute>
<put-attribute name="content" value="/WEB-INF/views/member/{1}.jsp"></put-attribute>
</definition>
<definition name="admin.*" template="/WEB-INF/views/layout/admin.jsp">
<put-attribute name="asset" value="/WEB-INF/views/inc/asset.jsp"></put-attribute>
<put-attribute name="main_menu" value="/WEB-INF/views/inc/main_menu.jsp"></put-attribute>
<put-attribute name="admin_menu" value="/WEB-INF/views/inc/admin_menu.jsp"></put-attribute>
<put-attribute name="content" value="/WEB-INF/views/admin/{1}.jsp"></put-attribute>
</definition>
- tiles.xml -> 더 좋은 Solution
<definition name="*.*" template="/WEB-INF/views/layout/layout.jsp">
<put-attribute name="asset" value="/WEB-INF/views/inc/asset.jsp"></put-attribute>
<put-attribute name="main_menu" value="/WEB-INF/views/inc/main_menu.jsp"></put-attribute>
<put-attribute name="sub_menu" value="/WEB-INF/views/inc/{1}_menu.jsp"></put-attribute>
<put-attribute name="content" value="/WEB-INF/views/member/{2}.jsp"></put-attribute>
</definition>
■ Controller
- AdminController.java
@Controller
@RequestMapping("/admin")
public class AdminController {
@GetMapping("/log.do")
public String log() {
return "admin.log";
}
@GetMapping("/setting.do")
public String setting() {
return "admin.setting";
}
}
- MainController.java
@Controller
public class MainController {
@GetMapping("/index.do")
public String index() {
//TilesViewResolver > 아래의 문자열 > <definition>의 name 속성 값으로 인식
return "index";
}
}
- MemberController.java
@Controller
@RequestMapping("/member")
public class MemberController {
@GetMapping("/info.do")
public String info() {
return "member.info";
}
@GetMapping("/history.do")
public String history() {
return "member.history";
}
@GetMapping("/favorite.do")
public String favorite() {
return "member.favorite";
}
}
728x90
'Spring' 카테고리의 다른 글
Spring STEP 9 - AOP (0) | 2023.06.19 |
---|---|
Spring STEP 8 - 파일 업로드 (0) | 2023.06.19 |
Spring STEP 6 - MyBatis 활용 게시판 만들기 (0) | 2023.06.16 |
Spring STEP 5 - MyBatis 응용 (1) | 2023.06.15 |
Spring STEP 4 - Spring MVC (0) | 2023.06.14 |