0

I have apache Tiles layout page having Header, Menu, Body. In this layout Reload the whole Layout is refreshing. I want header,menu to be static, and only body part should get refresh.

(1)write this tiles configuration into spring xml file.
    <bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver"/>

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

(2)layouts.xml file 
<?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="DefaultTemplate" template="/WEB-INF/views/template/SiteTemplate.jsp">
        <put-attribute name="title"     value="Home" />
        <put-attribute name="header"    value="/WEB-INF/views/template/header.jsp" />
        <put-attribute name="menu"      value="/WEB-INF/views/template/menu.jsp" />
        <put-attribute name="body"      value="" />
        <put-attribute name="footer"    value="/WEB-INF/views/template/footer.jsp" />
    </definition>

</tiles-definitions>

(3)views.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="DefaultTemplate">
        <put-attribute name="body"  value="/WEB-INF/views/index.jsp" />
    </definition>

    <definition name="personList" extends="DefaultTemplate">
        <put-attribute name="body"  value="/WEB-INF/views/personList.jsp" />
    </definition>

</tiles-definitions>

(4)SiteTemplate.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Spring MVC - Tiles Integration tutorial</title>
        <link rel="stylesheet" href="resources/css/screen.css" type="text/css" media="screen, projection"></link>
        <link rel="stylesheet" href="resources/css/print.css" type="text/css" media="print"></link>
        <!--[if IE]>
        <link rel="stylesheet" href="resources/css/ie.css" type="text/css" media="screen, projection">
        <![endif]-->
    <style>
    body{ margin-top:20px; margin-bottom:20px; background-color:#DFDFDF;}
    </style>
    </head>
    <body>
        <div class="container" style="border: #C1C1C1 solid 1px; border-radius:10px;">
            <!-- Header -->
            <tiles:insertAttribute name="header" />
            <!-- Menu Page -->
            <div class="span-5  border" style="height:400px;background-color:#FCFCFC;">
                <tiles:insertAttribute name="menu" />
            </div>
            <!-- Body Page -->
            <div class="span-19 last">
                <tiles:insertAttribute name="body" />
            </div>
            <!-- Footer Page -->
            <tiles:insertAttribute name="footer" />
        </div>
    </body>
    </html>

(5)menu.jsp

    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <ul style="list-style:none;line-height:28px;">

        <li><spring:url value="/index" var="homeUrl" htmlEscape="true" />
            <a href="${homeUrl}">Home</a>
        </li>

        <li><spring:url value="/viewPeson" var="personListUrl" htmlEscape="true" />
            <a href="${personListUrl}">Person List</a>
        </li>

    </ul>

(6)same as write footer.jsp and header.jsp for your requirement.

(7)code for controlller

    @RequestMapping(value="index")
        public String index() {
            return "index";
        }

Is there any way to prevent the refresh of the Header, menu and update only Body content on the menu click which can be implemented using Apache Tiles 3 ?

2 Answers2

0

In the tiles.xml, you can create multiple layout definitions. In cases where you need the body to change, you can create a base layout. A second layout can be created that will then extend the base layout and make any updates that you need. In this case, your title, header, menu, and footer will be the same, but the body will be different. You can write the layouts as such:

<definition name="DefaultTemplate" template="/WEB-INF/views/template/SiteTemplate.jsp">
        <put-attribute name="title"     value="Home" />
        <put-attribute name="header"    value="/WEB-INF/views/template/header.jsp" />
            <put-attribute name="menu"      value="/WEB-INF/views/template/menu.jsp" />
            <put-attribute name="body"      value="" />
            <put-attribute name="footer"    value="/WEB-INF/views/template/footer.jsp" />
</definition>

<defintion name="SecondaryTemplate" extends="DefaultTemplate">
   <put-attribute name="body" value="location_of_your_jsp"/>
</defintion>

The second template, extends your first template, keeping all of the attributes from the first template, but updating the body attribute with the jsp that you want to reference for that particular page.

Lexi
  • 324
  • 2
  • 10
  • 18
0
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/layouts/layouts.xml</value>
                <value>/WEB-INF/layouts/views.xml</value>
            </list>
        </property>
        <property name="checkRefresh" value="true" />
    </bean>

Use checkrefresh attribute while configuring Tiles.

This will load all the fragments at first and then after that, it will load them from memory cache/disk cache.

You can check this using inspect element

But still your header or footer will not be static here, instead they will be loaded from cache

aditya lath
  • 410
  • 7
  • 6