To integrate the tiles template into the SPRINGMVC framework, the approximate flow is as follows:
1. Add tiles support to the configuration file
My servlet configuration file is named Spring-mvc.xml. The configuration is as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd " > <!--activating @controller Mode - <Mvc:annotation-driven/> <!--Configuring the Package scan location - <Context:component-scanBase-package= "Com.test.maven.controller" /> <!--Configuring the View resolver (JSP file prefix suffix) - <BeanID= "Viewresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> < Propertyname= "prefix"> <value>/web-inf/jsp/</value> </ Property> < Propertyname= "suffix"> <value>. jsp</value> </ Property> </Bean> <!--Configuring Tiles Templates - <BeanID= "Tilesconfigurer"class= "Org.springframework.web.servlet.view.tiles3.TilesConfigurer"> < Propertyname= "Definitions"> <List> <value>/web-inf/tiles/tiles-definitions.xml</value> </List> </ Property> </Bean></Beans>
Very simple, just add the template configuration file path.
2. Configure the tiles template
We need to add a default template to tiles, adding some of the modules we want to join.
<?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> <definitionname= "DefaultTemplate"Template= "/web-inf/tiles/classic.jsp"> <Put-attributename= "title"value= "Hello World I m title" /> <Put-attributename= "Menu"value= "/web-inf/tiles/menu.jsp"/> <Put-attributename= "Footer"value= "/web-inf/tiles/footer.jsp" /> </definition></tiles-definitions>
DefaultTemplate is the name of the default template, which is the path to the template
Put-attribute represents the module that joins the template, and value points to its path
You can also add a new template, if you want to inherit a template, you can remove the templates in the definition, adding extends (not tried)
Note: when configuring the template file and invoking the template, the tiles tag library should be declared in the header of the file
Module files will not be mentioned, here is the template file, classic.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 "><HTML><Head><title><tiles:getasstringname= "title" /></title><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"></Head><Body> <Divclass= "page"> <Tiles:insertattributename= "Menu" /> <Divclass= "Content"> <Tiles:insertattributename= "Body" /> </Div> <Tiles:insertattributename= "Footer" /> </Div></Body></HTML>
The BODY element above is not the template, is the so-called body content, because in general, each call to the body within the element is not the same, so do not need to specify within the template, in the template file to indicate its location, and then when the call to fill in the contents of the inside.
3. Call the tiles template
<%@ 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 "><Body><tiles:insertdefinitionname= "DefaultTemplate"> <Tiles:putattributename= "Body">Hahahaha</Tiles:putattribute> <Tiles:putattributename= "Footer">Hello</Tiles:putattribute></tiles:insertdefinition></Body>
Invoking the template, first using the DefaultTemplate template, and then configuring the components within the template, if used by default, can be null directly, if the component is overridden, the configuration within the original template will be overwritten. However, if a post-add component, such as the upper body, does not have a default component, the content must be overridden when called.
Here, the simplest tiles configuration is over.
Insertdefinition
SPRINGMVC Framework Integrated Tiles template