Spring Tiles框架:仅刷新正文内容

时间:2015-09-03 04:17:59

标签: spring spring-mvc

我有一个Tiles布局页面,其中包含Header,Menu,Body。在此布局中,只要用户在菜单列表中执行某些操作,整个布局就会刷新。我想要标题,菜单是静态的,只有正文部分才能刷新。

有没有办法阻止刷新标题,菜单和仅更新菜单上的Body内容,可以使用Spring Tiles实现?

我有一些相同的问题,但它与结构有关。我不知道结构 如果有人给出一段代码,那将会很有帮助。

1 个答案:

答案 0 :(得分:0)

(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";
        }
**NOTE:return type value is same as definition name in views.xml**