Last Modified 2019.04.29

Apache Tiles

Source : https://github.com/kimjonghoon/tilesOnSpringMVC

Add apache tiles dependency to pom.xml

pom.xml
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-extras -->
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-extras</artifactId>
    <version>3.0.8</version>
</dependency>

Create Layout JSP

/WEB-INF/views/layouts/main-layout.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 uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>

<tiles:importAttribute name="links" />
<tiles:importAttribute name="scripts" />
<tiles:importAttribute name="alias" />

<!DOCTYPE html>
<html lang="<spring:message code="lang" />">
<head>
<meta charset="UTF-8" />
<title><spring:message code="${alias }.title" /></title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="Keywords" content="<spring:message code="${alias }.keys" />" />
<meta name="Description" content="<spring:message code="${alias }.desc" />" />
<link rel="stylesheet" href="/resources/css/<spring:message code="lang" />.css" />
<c:forEach var="link" items="${links }">
	<link rel="stylesheet" href="<c:url value="${link }" />" type="text/css" />
</c:forEach>
<c:forEach var="script" items="${scripts }">
	<script src="<c:url value="${script }" />"></script>
</c:forEach>
</head>
<body>
<div id="wrap">

	<div id="header">
		<tiles:insertAttribute name="header" />
	</div>
	
	<div id="main-menu" lang="en">
		<tiles:insertAttribute name="main-menu" />
	</div>
	
	<div id="container">
		<div id="content">
			<tiles:insertAttribute name="content" />
		</div>
	</div>
	
	<div id="sidebar" lang="en">
		<tiles:insertAttribute name="sidebar" />
	</div>
	
	<div id="extra">
		<tiles:insertAttribute name="extra" />
	</div>
	
	<div id="footer">
		<tiles:insertAttribute name="footer" />
	</div>

</div>
</body>
</html>

Create tiles definition file

/WEB-INF/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="app.default" template="/WEB-INF/views/layouts/main-layout.jsp">
        <put-attribute name="header" value="/WEB-INF/views/inc/header.jsp" />
        <put-attribute name="main-menu" value="/WEB-INF/views/inc/main-menu.jsp" />
        <put-attribute name="extra" value="/WEB-INF/views/inc/extra.jsp" />
        <put-attribute name="footer" value="/WEB-INF/views/inc/footer.jsp" />
        <put-list-attribute name="links">
			<add-attribute value="/resources/css/screen.css" />
			<add-attribute value="/resources/css/print.css" />
        </put-list-attribute>
        <put-list-attribute name="scripts">
        	<add-attribute value="/resources/js/jquery-3.2.1.min.js" />
			<add-attribute value="/resources/js/commons.js" />
        </put-list-attribute>
    </definition>
    <definition name="403" extends="app.default">
        <put-attribute name="content" value="/WEB-INF/views/403.jsp" />
        <put-attribute name="sidebar" value="/WEB-INF/views/error-sub.jsp" />
        <put-attribute name="alias" value="403" />
    </definition>
    <definition name="users/*" extends="app.default">
        <put-attribute name="content" value="/WEB-INF/views/users/{1}.jsp" />
        <put-attribute name="sidebar" value="/WEB-INF/views/users/user-sub.jsp" />
        <put-attribute name="alias" value="user.{1}" />
    </definition>
    <!-- BBS -->
    <definition name="bbs/*" extends="app.default">
        <put-attribute name="content" value="/WEB-INF/views/bbs/{1}.jsp" />
        <put-attribute name="sidebar" value="/WEB-INF/views/bbs/bbs-sub.jsp" />
        <put-attribute name="alias" value="bbs.{1}" />
    </definition>
    <!-- admin -->
    <definition name="admin/*" extends="app.default">
        <put-attribute name="content" value="/WEB-INF/views/admin/{1}.jsp" />
        <put-attribute name="sidebar" value="/WEB-INF/views/admin/admin-sub.jsp" />
        <put-attribute name="alias" value="admin.{1}" />
    </definition>    
    <definition name="index" extends="app.default">
        <put-attribute name="content" value="/WEB-INF/views/articles/java/JDK-Install_en.jsp" />
        <put-attribute name="sidebar" value="/WEB-INF/views/index-sub.jsp" />
        <put-attribute name="alias" value="java.JDK-Install" />
        <put-list-attribute name="links" inherit="true">
            <add-attribute value="/resources/css/windows.css" />
            <add-attribute value="/resources/css/prettify.css" />
        </put-list-attribute>
        <put-list-attribute name="scripts" inherit="true">
            <add-attribute value="/resources/js/prettify.js" />
            <add-attribute value="/resources/js/javaschool-prettify.js" />
        </put-list-attribute>
    </definition>
    <definition name="*/*" extends="app.default">
        <put-attribute name="content" value="/WEB-INF/views/articles/{1}/{2}_en.jsp" />
        <put-attribute name="sidebar" value="/WEB-INF/views/{1}/{1}-sub.jsp" />
        <put-attribute name="alias" value="{1}.{2}" />
    </definition>
</tiles-definitions>

Create tiles definition file for korean

/WEB-INF/tiles_ko.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="index" extends="app.default">
        <put-attribute name="content" value="/WEB-INF/views/articles/java/JDK-Install.jsp" />
        <put-attribute name="sidebar" value="/WEB-INF/views/index-sub.jsp" />
        <put-attribute name="alias" value="java.JDK-Install" />
        <put-list-attribute name="links" inherit="true">
            <add-attribute value="/resources/css/windows.css" />
            <add-attribute value="/resources/css/prettify.css" />
        </put-list-attribute>
        <put-list-attribute name="scripts" inherit="true">
            <add-attribute value="/resources/js/prettify.js" />
            <add-attribute value="/resources/js/javaschool-prettify.js" />
        </put-list-attribute>
    </definition>
    <definition name="*/*" extends="app.default">
        <put-attribute name="content" value="/WEB-INF/views/articles/{1}/{2}.jsp" />
        <put-attribute name="sidebar" value="/WEB-INF/views/{1}/{1}-sub.jsp" />
        <put-attribute name="alias" value="{1}.{2}" />
    </definition>
</tiles-definitions>

Tiles View Resolver

Remove the existing view resolver and tiles view resolver to Spring Configuration file.

<!-- ViewResolver -->
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
	<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
</bean>

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
	<property name="definitions">
		<list>
			<value>/WEB-INF/tiles.xml</value>
		</list>
	</property>
</bean>

Test

/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<h1>Home</h1>
/index_ko.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<h1>홈</h1>
/index-sub.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<h1>Home</h1>
/index-sub_ko.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<h1>홈</h1>