如何删除特定视图的apache磁贴中的磁贴? [英] How to remove a tile in apache tiles for a specific view?

查看:106
本文介绍了如何删除特定视图的apache磁贴中的磁贴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从视图中删除图块。我的主要观点看起来像这样

I want to know how to remove a tile from a view. My main view is looking like this

图块配置由4个部分组成:标题,菜单,正文和页脚。

The tile config is made up out of 4 pieces: header, menu, body and footer.

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

Now I know if I request a new page I can override the main view to for example replace the body so that I have different content displayed there.

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

But I want to be able if I click on link in the menu that will take me to a page that only has a header and body (no menu or footer).

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

The user will then complete a wizard where they can go from one page to another and then once they are done it should go back to the main layout again.

这是我的问题:如何从视图中删除菜单和页脚?我的问题在这里停止。

And this is my question: How do I remove the menu and the footer from a view? My question stops here.

由于没有太多关于瓷砖的文档,我可以找到,我想我会为其他一些挣扎的人提供一步一步的例子。获得使用Spring工具套件使用Apache Tiles和Spring MVC的工作示例(我的版本是STS 3.2.0)。

Since there is not much documentation on tiles, that I could find, I thought I would include a step by step example for some one else who struggles to get a working example of using Apache Tiles and Spring MVC using Spring Tool Suite (My version is STS 3.2.0).


  1. 创建新的STS项目

  1. Create a new STS project

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

File >> New >> Spring Template Project >> Spring MVC Project

选择Spring MVC Project选项

Select "Spring MVC Project" option

提供项目名称和顶级包

这将创建一个类似于下面的项目

This will create a project looking like the below

来自:


<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文件...例如标题,菜单,页脚和正文(这可以是实际的内容,通常你将有超过1个正文,具体取决于用户在菜单中点击的内容) - 布局将像这篇文章中的第一张图片。 / li>
  1. Now specify the view – the JSP files that would be used to specify the views… e.g. the header, menu, footer and the body (this can the actual content and usually you will have more than 1 body depending on what the user has clicked in the menu) - the layout will be like the very first picture in this post.

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

JSP files created in the view folder each with there content

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。这是用于确定单击菜单项时下一步的位置。如果您打开家庭控制器并将其更改为以下




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. 现在让我们配置磁贴

使用

 <?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,它定义了每个视图中可以覆盖的基本布局的部分。

The tiles.xml file has a "mainTemplate.jsp" defined as the base definition. Create a file "mainTemplate.jsp" which has the main html layout. The files has "tiles:insertAttribute" which defines the pieces of the base layout that can be overridden in each view.

<%@ 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. 最后你应该得到一个文件结构看起来像这样

推荐答案

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

In the Tiles.xml remove the header and footer value like below:

<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>

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

Then if you wants the only header and body:

<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>

这篇关于如何删除特定视图的apache磁贴中的磁贴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆