This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch fix/tiles in repository https://gitbox.apache.org/repos/asf/struts-site.git
commit 04c16955d4e33f3d9d507fbd2c97cf7d03c4afbc Author: Lukasz Lenart <lukaszlen...@apache.org> AuthorDate: Sat Feb 3 16:13:58 2024 +0100 Cleans up docs about Tiles --- source/plugins/tiles/index.md | 170 ++++++++++++++++++-------------------- source/plugins/tiles/tiles-use.md | 70 ---------------- 2 files changed, 79 insertions(+), 161 deletions(-) diff --git a/source/plugins/tiles/index.md b/source/plugins/tiles/index.md index 4c9ce28a0..3ecc7daa9 100644 --- a/source/plugins/tiles/index.md +++ b/source/plugins/tiles/index.md @@ -12,8 +12,8 @@ parent: * Will be replaced with the ToC, excluding a header {:toc} -Tiles is a templating framework designed to easily allow the creation of web application pages with a consistent look and feel. It can -be used for both page decorating and componentization. +Tiles is a templating framework designed to easily allow the creation of web application pages with a consistent +look and feel. It can be used for both page decorating and componentization. The Tiles plugin allows actions to return Tiles pages. @@ -26,100 +26,89 @@ The Tiles plugin allows actions to return Tiles pages. The following steps must be taken in order to enable tiles support within your Struts2 application: -1. Include the struts-tiles-plugin as a dependency in your web application. If you are using maven2, the dependency configuration will - be similar to: +1. Include the struts-tiles-plugin as a dependency in your web application. If you are using maven2, the dependency + configuration will be similar to: + ```xml + <dependency> + <groupId>org.apache.struts</groupId> + <artifactId>struts2-tiles-plugin</artifactId> + <version>${version.tiles}</version> + </dependency> + ``` -```xml -<dependency> - <groupId>org.apache.struts</groupId> - <artifactId>struts2-tiles-plugin</artifactId> - <version>${version.tiles}</version> -</dependency> - -``` - -2. Register the tiles listener. This listener will typically either be the standard tiles listener `org.apache.tiles.listener.TilesListener` - or the Struts2 replacement `org.apache.struts2.tiles.TilesListener`. The latter provides tighter integration with Struts features such - as freemarker integration. - - -```xml -<listener> - <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> -</listener> - -``` - -3. All package definitions which require tiles support must either extend the `tiles-default` package or must register - the [Tiles Result] type definition. +2. Register the Tiles listener. This listener will typically either be the standard tiles listener `org.apache.tiles.listener.TilesListener` + or the Struts2 replacement `org.apache.struts2.tiles.TilesListener`. The latter provides tighter integration with + Struts features such as freemarker integration. + ```xml + <listener> + <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> + </listener> + ``` -```xml -<result-types> - <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/> -</result-types> +3. All package definitions, which require tiles support, must either extend the `tiles-default` package or must register + the **Tiles Result** type definition. -``` + ```xml + <result-types> + <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/> + </result-types> + ``` 4. Configure your actions to utilize a tiles definition: - -```xml -<action name="sample" class="org.apache.struts2.tiles.example.SampleAction" > - <result name="success" type="tiles">tilesWorks</result> -</action> - -``` + ```xml + <action name="sample" class="org.apache.struts2.tiles.example.SampleAction" > + <result name="success" type="tiles">tilesWorks</result> + </action> + ``` 5. Instead of xml configuration you can use annotations - -```java -@Result(name = "success", type="tiles") -@TilesDefinition(extend = "fooLayout", putAttributes = { - @TilesPutAttribute(name = "header", value = "/WEB-INF/tiles/header.jsp"), - @TilesPutAttribute(name = "body", value = "/WEB-INF/tiles/body.ftl") -}) -public class FooAction extends ActionSupport { -``` - -6. You have to define Tiles Definitons in a `tiles.xml` file. That can be placed in `resources` or in `WEB-INF`. - - -```xml -<!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="fooLayout" template="/WEB-INF/tiles/layout.jsp"> - <put-attribute name="title" value="Tiles Sample"/> - <put-attribute name="header" value=".header"/> - <put-attribute name="body" value=".bodyp"/> - </definition> - - <definition name="tilesWorks" extends="fooLayout"> - <put-attribute name="header" value="/WEB-INF/tiles/header.jsp"/> - <put-attribute name="body" value="/WEB-INF/tiles/body.jsp"/> - </definition> - -</tiles-definitions> -``` - -As from Struts 2.3.28, the plugin automatically loads all Tiles definitions matching the following pattern `tiles*.xml` - you don't have -to specify them via `org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG` in `web.xml`, but you can use this option if your -application is going to work in restricted servlet environment e.g. Google AppEngine. In such case, definitions will be read from -the provided init-param. - -> Note: When using a Tomcat WAR versoning mechanism which uses `##` you must specify all the tiles definition directly using -> the `init-param`, in other case it won't be loaded. It's due to limitation of `URL` class. + ```java + @Result(name = "success", type="tiles") + @TilesDefinition(extend = "fooLayout", putAttributes = { + @TilesPutAttribute(name = "header", value = "/WEB-INF/tiles/header.jsp"), + @TilesPutAttribute(name = "body", value = "/WEB-INF/tiles/body.ftl") + }) + public class FooAction extends ActionSupport { + ``` + +6. You have to define Tiles Definitions in a `tiles.xml` file. That can be placed in `resources` or in `WEB-INF`. + + ```xml + <!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="fooLayout" template="/WEB-INF/tiles/layout.jsp"> + <put-attribute name="title" value="Tiles Sample"/> + <put-attribute name="header" value=".header"/> + <put-attribute name="body" value=".bodyp"/> + </definition> + + <definition name="tilesWorks" extends="fooLayout"> + <put-attribute name="header" value="/WEB-INF/tiles/header.jsp"/> + <put-attribute name="body" value="/WEB-INF/tiles/body.jsp"/> + </definition> + + </tiles-definitions> + ``` + +As from Struts 2.3.28, the plugin automatically loads all Tiles definitions matching the following pattern `tiles*.xml` - +you don't have to specify them via `org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG` in `web.xml`, +but you can use this option if your application is going to work in restricted servlet environment e.g., Google AppEngine. +In such case, definitions will be read from the provided init-param. + +> Note: When using a Tomcat WAR versoning mechanism which uses `##` you must specify all the tiles definition directly +> using the `init-param`, in other case it won't be loaded. It's due to limitation of `URL` class. ### Accessing Struts attributes -As from Struts version 2.5.3 it's possible accessing defined values on a `ValueStack` using `S2` prefix when defining an expression -in tiles definition, e.g.: +As from Struts version 2.5.3 it's possible accessing defined values on a `ValueStack` using `S2` prefix when defining +an expression in tiles definition, e.g.: ```xml <definition name="home" extends="logged-in"> @@ -128,13 +117,12 @@ in tiles definition, e.g.: </definition> ``` -In such case Tiles will delegate evaluation of the expression to Struts and `ValueStack` will be examined to evaluate the expression. +In such case Tiles will delegate evaluation of the expression to Struts and `ValueStack` will be examined. ### I18N -Instead of defining new tiles definitions per supported language (i.e.: `tiles.xml`, `tiles_de.xml`, `tiles_pl.xml`) you can use `I18N` -prefix to evaluate provided expression as a key in Struts resource bundles. - +Instead of defining new tiles definitions per supported language (i.e.: `tiles.xml`, `tiles_de.xml`, `tiles_pl.xml`) +you can use `I18N` prefix to evaluate provided expression as a key in Struts resource bundles. ```xml <definition name="home" extends="logged-in"> @@ -158,18 +146,18 @@ This example shows a Tiles layout page using Struts tags: <body> <tiles:insertAttribute name="header"/> <tiles:insertAttribute name="body"/> - <p>Notice that this is a layout made in JSP</p> + <p>Notice that this is a layout made in JSP</p> </body> </html> - ``` -Please check [tiles](https://github.com/apache/struts-examples/tree/master/tiles) example in [struts-examples](https://github.com/apache/struts-examples/tree/master/tiles) project. +Please check [tiles](https://github.com/apache/struts-examples/tree/master/tiles) example in [struts-examples](https://github.com/apache/struts-examples/tree/master/tiles) project. ## Settings -This plugin does inherits settings from [Tiles configuration](https://tiles.apache.org/framework/config-reference.html). +This plugin does inherit settings from [Tiles configuration](https://tiles.apache.org/framework/config-reference.html). ## Installation -This plugin can be installed by copying the plugin jar into your application's `/WEB-INF/lib` directory. No other files need to be copied or created. +This plugin can be installed by copying the plugin jar into your application's `/WEB-INF/lib` directory. +No other files need to be copied or created. diff --git a/source/plugins/tiles/tiles-use.md b/source/plugins/tiles/tiles-use.md deleted file mode 100644 index 3262f94b7..000000000 --- a/source/plugins/tiles/tiles-use.md +++ /dev/null @@ -1,70 +0,0 @@ ---- -layout: plugin -title: Tiles Use ---- - -# Tiles Use - -Tiles uses a definition file (XML document) that must be instantiated before use. One way to render the definition file would be to use Spring. Another way would be to use a separate Listener, as shown by Tilesconfigurer. - -**TilesConfigurer.java** - -```java - -package com.opensymphony.webwork.views.tiles; - -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; - -import org.apache.struts.tiles.DefinitionsFactoryConfig; -import org.apache.struts.tiles.DefinitionsFactoryException; -import org.apache.struts.tiles.TilesUtil; -import org.apache.struts.tiles.xmlDefinition.I18nFactorySet; - -/* -* Modified from Spring's source -* -* here's how a smaple web xml should look like: -* <web-app> -* <context-param> -* <param-name>tilesDefinitions</param-name> -* <param-value>/WEB-INF/tiles.xml</param-value> -* </context-param> -* -* <listener> -* <listener-class>com.opensymphony.webwork.views.tiles.TilesConfigurer</listener-class> -* </listener> -* </web-app> -* -* To use the definitions specified you would use a dispatcher result (since -* tiles jsp is just another jsp) to render tiles view. -*/ -public class TilesConfigurer implements ServletContextListener { - - private boolean initialized = false; - - public void contextInitialized (ServletContextEvent evt) { - - if (!initialized) { - DefinitionsFactoryConfig factoryConfig = new DefinitionsFactoryConfig(); - factoryConfig.setFactoryClassname(I18nFactorySet.class.getName()); - factoryConfig.setParserValidate(true); - factoryConfig.setDefinitionConfigFiles(evt.getServletContext().getInitParameter("tilesDefinitions")); - try { - TilesUtil.createDefinitionsFactory(evt.getServletContext(), factoryConfig); - } catch (DefinitionsFactoryException e) { - e.printStackTrace(); - } - initialized = true; - } - - } - - public void contextDestroyed (ServletContextEvent evt) { - } - -} - -``` - -> The TilesConfigurer was adapted from the Spring source code.