Apache Tiles Learning (iv), Tiles combat _apache

Source: Internet
Author: User
Absrtact: This article is a small demo used by tiles, for reference only. 1. Create Maven Project

New-->maven project-->


Check out create a simple project (skip archetype selection), Next enter the group ID and artifact ID, packaging Select War, Finish, and then create Maven Project.

Add dependent dependencies for tiles in the Pom.xml file:

<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>3.0.4</version>
</dependency>

The project was created and the associated jar package for tiles was introduced into the project. 2, Configuration Environment 2.1, add tiles configuration in Web.xml file <listener>
<listener-class>org.apache.tiles.extras.complete.completeautoloadtileslistener
</listener-class>
</listener>

2.2. Add difinition definition file Tiles.xml

Create a Tiles.xml file in the/web-inf/directory, add the following configuration: <?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>

</tiles-definitions>

3, create a template (template) 3.1, create a related directory


/web-inf/view: Place JSP Page view/web-inf/view/tempalte: Template folder to place tiles template file (This is not mandatory, you can put it on your own, then configure when configured on the line.     /web-inf/view/difirent: A part of the placement template that cannot be reused/web-inf/view/tempalte/template1: Here is a template folder with template JSP pages and some reusable interfaces. Tips:

You absolutely do not have to follow the above way to put JSP pages, the above is only for good management and consideration of security. You can completely create the following directory:/webapp (or Webcontext or Webroot)/jsp, place the page in the JSP directory, without any effect, because tiles will manually configure the path of the relevant file when the difinitions is defined, If this is not understood here, please continue to look down, you will understand. 3.2. Create a template

Now to create a template to try, create a template.jsp file under/web-inf/view/tempalte/template1, add the following:

template.jsp
<%@ page language= "java" contenttype= "text/html" Charset=utf-8 "pageencoding=" UTF-8 "%>"
<% @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" >

The above template is the following "classic layout" with tiles defined template


And, from the file above, you see:

<tiles:getasstring name= "title"/>

<tiles:insertattribute name= "header"/>

<tiles:insertattribute name= "Menu"/>

<tiles:insertattribute name= "Body"/>

<tiles:insertattribute name= "Footer"/>

The above is tiles inserts the attribute in the template, uses to leave out the blank, then renders the property the value to configure the page to present the entire page together. Thus, the composite view mode is achieved. 4, create attributes (attribute) 4.1, tiles properties

Can be seen as the name of each blank area in the template and the path of the page file that is actually displayed in the form of a key-value pair. 4.2, create the corresponding page of attributes
Create the following JSP page under the/web-inf/view/tempalte/template1 directory:
header.jsp menu.jsp footer.jsp

Create body1.jsp body2.jsp 5, add definition (difinition) under the/web-inf/view/diffrent directory

Now, add the definition of tiles and add the following in the/web-inf/tiles.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=" test "
		template= "/web-inf/view/template/template1/template.jsp" >
		<put-attribute name= "title" Value= " Tiles Tutorial Homepage "/>
		<put-attribute name=" header "
			value="/web-inf/view/template/template1/ header.jsp "/>
		<put-attribute name=" menu "value="/web-inf/view/template/template1/menu.jsp "/>"
		<put-attribute name= "Body" value= "/web-inf/view/diffrent/body1.jsp"/> <put-attribute name=
		" Footer "
			value="/web-inf/view/template/template1/footer.jsp "/>
	</definition>
</ Tiles-definitions>

6. Create test page

Create a example.jsp file in the/web-inf/directory and add the following:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<%@ taglib uri= "http://tiles.apache.org/tags-tiles" prefix= "tiles"%>
<tiles:insertdefinition name= "Test"/>

7, testing

Enter in Browser: http://localhost:8080/tiles-first/example.jsp

The display is as follows:

At this point, tiles a small demo is complete. 8. Project Analysis

When we want to change the body, we just have to configure the relevant file path in the Tiles.xml file. The above is simply using the servlet API to process the request, followed by SPRINGMVC to process the request and using tiles as a view.

Reprint: https://my.oschina.net/jast90/blog/284602

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.