This project has retired. For details please refer to its Attic page.
InsertDefinitionModel xref
View Javadoc

1   /*
2    * $Id: InsertDefinitionModel.java 880940 2009-11-16 20:11:22Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.tiles.template;
23  
24  import org.apache.tiles.Attribute;
25  import org.apache.tiles.AttributeContext;
26  import org.apache.tiles.TilesContainer;
27  
28  /***
29   * <p>
30   * <strong>Insert a definition.</strong>
31   * </p>
32   * <p>
33   * Insert a definition with the possibility to override and specify parameters
34   * (called attributes). A definition can be seen as a (partially or totally)
35   * filled template that can override or complete attribute values.
36   * <code>&lt;tiles:insertDefinition&gt;</code> allows to define these attributes
37   * and pass them to the inserted jsp page, called template. Attributes are
38   * defined using nested tag <code>&lt;tiles:putAttribute&gt;</code> or
39   * <code>&lt;tiles:putListAttribute&gt;</code>.
40   * </p>
41   * <p>
42   * You must specify <code>name</code> tag attribute, for inserting a definition
43   * from definitions factory.
44   * </p>
45   * <p>
46   * <strong>Example : </strong>
47   * </p>
48   *
49   * <pre>
50   * &lt;code&gt;
51   *           &lt;tiles:insertDefinition name=&quot;.my.tiles.defininition flush=&quot;true&quot;&gt;
52   *              &lt;tiles:putAttribute name=&quot;title&quot; value=&quot;My first page&quot; /&gt;
53   *              &lt;tiles:putAttribute name=&quot;header&quot; value=&quot;/common/header.jsp&quot; /&gt;
54   *              &lt;tiles:putAttribute name=&quot;footer&quot; value=&quot;/common/footer.jsp&quot; /&gt;
55   *              &lt;tiles:putAttribute name=&quot;menu&quot; value=&quot;/basic/menu.jsp&quot; /&gt;
56   *              &lt;tiles:putAttribute name=&quot;body&quot; value=&quot;/basic/helloBody.jsp&quot; /&gt;
57   *           &lt;/tiles:insertDefinition&gt;
58   *         &lt;/code&gt;
59   * </pre>
60   *
61   * @version $Rev: 880940 $ $Date: 2009-11-16 21:11:22 +0100 (lun, 16 nov 2009) $
62   * @since 2.2.0
63   */
64  public class InsertDefinitionModel {
65  
66      /***
67       * Starts the operation.
68       *
69       * @param container The Tiles container.
70       * @param requestItems The request objects.
71       * @since 2.2.0
72       */
73      public void start(TilesContainer container, Object... requestItems) {
74          container.startContext(requestItems);
75      }
76  
77      /***
78       * Ends the operation.
79       *
80       * @param container The Tiles container.
81       * @param definitionName The name of the definition to render.
82       * @param template If specified, this template will be used instead of the
83       * one used by the definition.
84       * @param templateType The type of the template attribute.
85       * @param templateExpression The expression to evaluate to get the value of the template.
86       * @param role A comma-separated list of roles. If present, the definition
87       * will be rendered only if the current user belongs to one of the roles.
88       * @param preparer The preparer to use to invoke before the definition is
89       * rendered. If specified, it overrides the preparer specified in the
90       * definition itself.
91       * @param requestItems The request objects.
92       * @since 2.2.0
93       */
94      public void end(TilesContainer container, String definitionName,
95              String template, String templateType, String templateExpression,
96              String role, String preparer, Object... requestItems) {
97          try {
98              AttributeContext attributeContext = container
99                      .getAttributeContext(requestItems);
100             Attribute templateAttribute = Attribute.createTemplateAttribute(template,
101                     templateExpression, templateType, role);
102             attributeContext.setPreparer(preparer);
103             attributeContext.setTemplateAttribute(templateAttribute);
104             container.render(definitionName, requestItems);
105         } finally {
106             container.endContext(requestItems);
107         }
108     }
109 
110     /***
111      * Executes the operation.
112      *
113      * @param container The Tiles container.
114      * @param definitionName The name of the definition to render.
115      * @param template If specified, this template will be used instead of the
116      * one used by the definition.
117      * @param templateType The type of the template attribute.
118      * @param templateExpression The expression to evaluate to get the value of the template.
119      * @param role A comma-separated list of roles. If present, the definition
120      * will be rendered only if the current user belongs to one of the roles.
121      * @param preparer The preparer to use to invoke before the definition is
122      * rendered. If specified, it overrides the preparer specified in the
123      * definition itself.
124      * @param requestItems The request objects.
125      * @since 2.2.0
126      */
127     public void execute(TilesContainer container, String definitionName,
128             String template, String templateType, String templateExpression,
129             String role, String preparer, Object... requestItems) {
130         start(container, requestItems);
131         end(container, definitionName, template, templateType, templateExpression, role, preparer, requestItems);
132     }
133 }