【问题标题】:How to remove a tile in apache tiles for a specific view?如何删除特定视图的 apache 磁贴中的磁贴?
【发布时间】:2014-01-04 18:04:34
【问题描述】:

我想知道如何从视图中移除图块。我的主视图是这样的

磁贴配置由 4 部分组成:页眉、菜单、正文和页脚。

现在我知道如果我请求一个新页面,我可以覆盖主视图,例如替换正文,以便在那里显示不同的内容。

但如果我点击菜单中的链接,我希望能够将我带到一个只有页眉和正文(没有菜单或页脚)的页面。

然后用户将完成一个向导,他们可以从一个页面转到另一个页面,然后一旦完成,它应该再次返回到主布局。

这是我的问题:如何从视图中删除菜单和页脚?我的问题到此为止。

由于没有太多关于磁贴的文档,我可以找到,我想我会为其他努力获得使用 Spring Tool Suite 使用 Apache Tiles 和 Spring MVC 的工作示例的人提供一个分步示例(我的版本是 STS 3.2.0)。

使用 STS 创建简单网站的步骤如下

  1. 创建一个新的 STS 项目

    文件 >> 新建 >> Spring 模板项目 >> Spring MVC 项目

    选择“Spring MVC 项目”选项

    为您的项目命名和顶级包

    这将创建一个如下所示的项目

  2. 将 POM 更改为使用 SPRING 3.2.0.RELEASE

发件人:

<org.springframework-version>3.1.1.RELEASE</org.springframework-version>

收件人:

<org.springframework-version>3.2.0.RELEASE</org.springframework-version>
  1. 将以下依赖项添加到 POM 文件以包含 Apache Tile 依赖项
<!-- Tiles -->
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-api</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-core</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-jsp</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-servlet</artifactId>
    <version>3.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-template</artifactId>
    <version>3.0.1</version>
</dependency> 
  1. 更改“servlet-context.xml”以使用 TilesViewResolver 而不是默认的 InternalViewResolver
<!-- Remove -->
<beans:bean class="org.springframework.web.servlet.view.tiles3.TilesViewResolver">
    <beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"></beans:property>
    <beans:property name="order" value="0"></beans:property>
</beans:bean>
 
<!-- Add -->
<beans:bean class="org.springframework.web.servlet.view.tiles3.TilesViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"></beans:property>
    <beans:property name="order" value="0"></beans:property>
</beans:bean>
  1. 添加参考以了解磁贴的配置位置
<beans:bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" id="tilesConfigurer">
    <beans:property name="definitions" value="/WEB-INF/spring/tiles.xml"> </beans:property>
</beans:bean>
  1. 现在指定视图 – 用于指定视图的 JSP 文件……例如页眉、菜单、页脚和正文(这可以是实际内容,通常您将拥有多个正文,具体取决于用户在菜单中单击的内容)- 布局将与本文中的第一张图片一样。

在视图文件夹中创建的每个 JSP 文件都有内容

header.jsp

<h2>This is the header</h2>

footer.jsp

<p>This is the footer</p>

content1.jsp

<h1>This is content page 1</h1>
<p>Blah blah content 1</p>

content2.jsp

<h1>This is content page 2</h1>
<p>Blah blah content 2</p>

menu.jsp

<h2>Menu</h2>
    <a href="">Go to home page</a><br/>
    <a href="page1">Display page 1</a><br/>
    <a href="page2">Display page 2</a>
  1. 创建项目时,它会创建一个名为“HomeController.java”的默认控制器。这用于确定单击菜单项时下一步要去哪里。如果你打开home控制器,改成下面这样
package com.stp.myapp;
import java.util.Locale;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
    /**
     * The request mapping has a value. That is what we are 
* requesting for. When opening the site it will request the main 
* page or the index page. The “/” indicates it is the index page.
* In this simple example we simply return the new ModalAndView with 
* the page in the view folder. In This case it is the mainPage.
* The mainPage is the reference of what is configured in the 
* apache tiles configuration – not the actual page that will be 
* displayed. 
     */
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public ModelAndView home(Locale locale, Model model) {
        return new ModelAndView("mainPage");
        }

        /**
         * The request mapping is for page1 (page1 is the value from the menu.
         */

        @RequestMapping(value = "/page1", method = RequestMethod.GET)
        public ModelAndView viewArticle(Locale locale, Model model) {
        return new ModelAndView("displayPageContent1");
        }

        @RequestMapping(value = "/page2", method = RequestMethod.GET)
        public ModelAndView viewEmployees(Locale locale, Model model) {
        return new ModelAndView("displayPageContent2");
    }

}
  1. 现在让我们配置图块

创建一个“tiles.xml”文件
 <?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">

<!-- This file has several definitions of page layouts -->
<tiles-definitions>

    <!-- The main definition with the header footer menu and body -->
    <definition name="base.definition" template="/WEB-INF/views/mainTemplate.jsp">
        <put-attribute name="title" value=""></put-attribute>
        <put-attribute name="header" value="/WEB-INF/views/header.jsp"></put-attribute>
        <put-attribute name="menu" value="/WEB-INF/views/menu.jsp"></put-attribute>
        <put-attribute name="body" value=""></put-attribute>
        <put-attribute name="footer" value="/WEB-INF/views/footer.jsp"></put-attribute>
    </definition>

    <!-- Now you can specify as many layours as you want... The name will match the names the --> 
    <!-- HomeController.java returns aka... as we specified it as displayPageContent1 and displayPageContent2 -->
    <!-- You can override each of the base.definition entries. In this we change the page title and display a different -->
    <!-- page as the body. As we didn't override the menu of footer it will display as specified in tha base.defition-->
    <definition name="displayPageContent1" extends="base.definition">
        <put-attribute name="title" value="Page context 1 displaying..."></put-attribute>
        <put-attribute name="body" value="/WEB-INF/views/content1.jsp"></put-attribute>
    </definition>

    <definition name="displayPageContent2" extends="base.definition">
        <put-attribute name="title" value="Employees List"></put-attribute>
        <put-attribute name="body" value="/WEB-INF/views/content2.jsp"></put-attribute>
    </definition>

     <definition name="mainPage" extends="base.definition">
        <put-attribute name="title" value="This is the home page being displayed....."></put-attribute>
        <put-attribute name="body" value="/WEB-INF/views/home.jsp"></put-attribute>
    </definition>

</tiles-definitions>

tiles.xml 文件有一个“mainTemplate.jsp”定义为基本定义。创建一个具有主 html 布局的文件“mainTemplate.jsp”。这些文件有“tiles:insertAttribute”,它定义了可以在每个视图中覆盖的基本布局部分。

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
    <tiles:insertAttribute name="title" ignore="true"></tiles:insertAttribute>
</title>
</head>
<body>
<table border="1" cellpadding="2" cellspacing="2" align="left">
    <tr>
        <td colspan="2" align="center">
            <tiles:insertAttribute name="header"></tiles:insertAttribute>
        </td>
    </tr>
    <tr>
        <td>
            <tiles:insertAttribute name="menu"></tiles:insertAttribute>
        </td>
        <td>
            <tiles:insertAttribute name="body"></tiles:insertAttribute>
        </td>
    </tr>
    <tr>
        <td colspan="2"  align="center">
            <tiles:insertAttribute name="footer"></tiles:insertAttribute>
        </td>
    </tr>
</table>
</body>
</html>
  1. 最后你应该得到一个像这样的文件结构

【问题讨论】:

    标签: java spring-mvc apache-tiles


    【解决方案1】:

    Tiles.xml 中删除页眉和页脚值,如下所示:

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

    如果你想要唯一的标题和正文:

    <definition name="displayPageContent2" extends="base.definition">
      <put-attribute name="title" value="Employees List"></put-attribute>
      <put-attribute name="header" value="/WEB-INF/views/header.jsp" />"></put-attribute>
      <put-attribute name="body" value="/WEB-INF/views/content2.jsp"></put-attribute>
    </definition>
    

    【讨论】:

      【解决方案2】:

      我不知道这是否是您所期望的,但您始终可以在您的磁贴配置中添加对空文件的引用(首先创建一个) - 这样菜单就会消失:

      你可以在menu jsp中移动menu td,所以布局看起来和这个类似:

      <tr>        
      <tiles:insertAttribute name="menu"></tiles:insertAttribute>
      [...]
      </tr>
      

      定义:

      <definition name="displayPageContent1" extends="base.definition">
      [...]
      <put-attribute name="menu" value="/WEB-INF/views/empty.jsp"></put-attribute>    
      </definition>
      

      在上面给出的场景中,td with menu 不会出现,你可以对页脚部分做同样的事情。

      另一种方法是创建一个定义并将其嵌套到另一个定义中,这也会导致嵌套模板,然后您可以像这样插入那些自定义模板(来自其他项目的示例):

      <definition name="menu_template"  template="/WEB-INF/layouts/main_template/header.ftl">
      <put-attribute name="SYSTEM_NAME_SHORT" value="SOL"/>
      <put-attribute name="menu" value="/WEB-INF/views/menus/menu.ftl"/>
      </definition>
      
      <definition name="record.history" extends="main_template">
      <put-attribute name="content" value="/WEB-INF/views/xxx.ftl"/>
      <put-attribute name="header" >
      <definition extends="menu_template">
      <put-attribute name="menu" value="/WEB-INF/layouts/blank.ftl"/>
      </definition>
      </put-attribute>
      </definition>
      

      正如您在上面看到的,您可以将其他模板作为属性注入到主模板中 - 在这种情况下,菜单模板被插入到名为 header 的属性中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-12
        • 1970-01-01
        相关资源
        最近更新 更多