2020년 5월 26일 화요일

[Spring] To set tiles

What is Tiles?

 Tiles is a library that organizes screens on a single web page by attaching each area, such as attaching tiles. Areas on a web page are divided into size headers, content, left, Nav, and footers, and if you want to use them in common on multiple webpages, you can use them conveniently by specifying them as tiles tags.

pom.xml

dependency
<!-- Tiles -->
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-core</artifactId>
    <version>3.0.7</version>
</dependency>
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-jsp</artifactId>
    <version>3.0.7</version>
</dependency>

servlet-context.xml

View Resolver Settings
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
    in the /WEB-INF/views directory -->
<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
    <beans:property name="order" value="2" />
</beans:bean>

<!-- Tiles 뷰 리졸버 -->
<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>
<!-- Tiles 설정 파일 -->
<beans:bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <beans:property name="definitions">
        <beans:list>
            <beans:value>/WEB-INF/tiles/tiles.xml</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>
 In the existing blank setting, define the order attribute as 2, and paste the settings related to the tiles. where the order means the priority to be executed and in the value tag, type the path to the xml file to which you want to tile.

tiles.xml

 Create the /WEB-INF/tiles folder, as you entered in the path above, and create the tiles.xml file in it.

예시
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC 
    "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" 
    "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
    <definition name="default-layout" template="/WEB-INF/tiles/layouts/default-layout.jsp">
        <put-attribute name="head" value="/WEB-INF/tiles/components/head.jsp" />
        <put-attribute name="footer" value="/WEB-INF/tiles/components/footer.jsp" />
    </definition>
    
    <definition name="*/*" extends="default-layout">
        <put-attribute name="content" value="/WEB-INF/views/{1}/{2}.jsp" />
    </definition>
</tiles-definitions>
Type the path to the default-layout.jsp file to use the tiles tag as the default. Then enter the name of the tag to be used as a put-attribute tag, and the path to the jsp file. The content part is jsp that will be displayed on the screen by the controller.Because it means a file, write '*/*' in the name property and 'value' in the path using the replacement as shown above.

Jsp file

header.jsp
<!DOCTYPE html>
<html>
    <head>
        <!-- head내용 -->
        <meta charset="UTF-8">
        <title>이름</title>
        <link rel="stylesheet" href="<c:url value='/resources/css/reset.css'></c:url>">
        <link rel="stylesheet" href="<c:url value='/resources/css/style.css'></c:url>">
    </head>
    <body>

    </body>
</html>
footer.jsp
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
    <!-- footer 내용 -->
    <footer id="footer">
    ....
    </footer>
    </body>
</html>
 main.jsp
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <!-- content 내용 -->
        <div id="content">
        ...
        </div>
    </body>
</html>
 이 부분들은 tiles:insertAttribute 태그가 될 부분이기 때문에 head 부분만 내용을 작성합니다. footer.jsp, main.jsp 파일도 작성합니다. contents 내용을 담고 있는 main.jsp는 폴더를 하나 만들어서 그 폴더 안에 만들도록 합니다.

Default Layout file

default-layout.jsp
<%@ page pageEncoding="utf-8" session="false"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>

<html>
    <head>
        <tiles:insertAttribute name="head" />
    </head>
    <body>
        <div class="container">
            <tiles:insertAttribute name="content" />
            <tiles:insertAttribute name="footer" />
        </div>
    </body>
</html>
 The default layout page is where you use the tile tag, where the jsp contents associated with the tile tag are linked to a single web page.

Controller file

HomeController.java
@Controller
public class HomeController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
        
        return "main/main";
    }
}
  The '*/*' format was satisfied in the tile settings, so the main.jsp file will appear linked to the part pput-insertAttribute name=content//.

댓글 없음:

댓글 쓰기