1 /*
2 * $Id: InsertTemplateModel.java 1058106 2011-01-12 12:22:58Z 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 java.io.IOException;
25
26 import org.apache.tiles.Attribute;
27 import org.apache.tiles.AttributeContext;
28 import org.apache.tiles.TilesContainer;
29 import org.apache.tiles.access.TilesAccess;
30 import org.apache.tiles.autotag.core.runtime.ModelBody;
31 import org.apache.tiles.autotag.core.runtime.annotation.Parameter;
32 import org.apache.tiles.request.Request;
33
34 /**
35 * <p>
36 * <strong>Insert a template.</strong>
37 * </p>
38 * <p>
39 * Insert a template with the possibility to pass parameters (called
40 * attributes). A template can be seen as a procedure that can take parameters
41 * or attributes. <code><tiles:insertTemplate></code> allows to define
42 * these attributes and pass them to the inserted jsp page, called template.
43 * Attributes are defined using nested tag
44 * <code><tiles:putAttribute></code> or
45 * <code><tiles:putListAttribute></code>.
46 * </p>
47 * <p>
48 * You must specify <code>template</code> attribute, for inserting a template
49 * </p>
50 *
51 * <p>
52 * <strong>Example : </strong>
53 * </p>
54 *
55 * <pre>
56 * <code>
57 * <tiles:insertTemplate template="/basic/myLayout.jsp" flush="true">
58 * <tiles:putAttribute name="title" value="My first page" />
59 * <tiles:putAttribute name="header" value="/common/header.jsp" />
60 * <tiles:putAttribute name="footer" value="/common/footer.jsp" />
61 * <tiles:putAttribute name="menu" value="/basic/menu.jsp" />
62 * <tiles:putAttribute name="body" value="/basic/helloBody.jsp" />
63 * </tiles:insertTemplate>
64 * </code>
65 * </pre>
66 *
67 * @version $Rev: 1058106 $ $Date: 2011-01-12 23:22:58 +1100 (Wed, 12 Jan 2011) $
68 * @since 2.2.0
69 */
70 public class InsertTemplateModel {
71
72 /**
73 * Executes the operation.
74 * @param template The template to render.
75 * @param templateType The type of the template attribute.
76 * @param templateExpression The expression to evaluate to get the value of the template.
77 * @param role A comma-separated list of roles. If present, the template
78 * will be rendered only if the current user belongs to one of the roles.
79 * @param preparer The preparer to use to invoke before the definition is
80 * rendered. If specified, it overrides the preparer specified in the
81 * definition itself.
82 * @param flush If <code>true</code>, the response will be flushed after the insert.
83 * @param request The request.
84 * @param modelBody The body.
85 * @throws IOException If something goes wrong.
86 * @since 2.2.0
87 */
88 public void execute(@Parameter(required = true) String template,
89 String templateType, String templateExpression, String role,
90 String preparer, boolean flush, Request request, ModelBody modelBody)
91 throws IOException {
92 TilesContainer container = TilesAccess.getCurrentContainer(request);
93 container.startContext(request);
94 modelBody.evaluateWithoutWriting();
95 container = TilesAccess.getCurrentContainer(request);
96 renderTemplate(container, template, templateType, templateExpression,
97 role, preparer, flush, request);
98 }
99
100 /**
101 * Renders a template.
102 *
103 * @param container The container to use.
104 * @param template The template to render.
105 * @param templateType The type of the template attribute.
106 * @param templateExpression The expression to evaluate to get the value of the template.
107 * @param role A comma-separated list of roles. If present, the template
108 * will be rendered only if the current user belongs to one of the roles.
109 * @param preparer The preparer to use to invoke before the definition is
110 * rendered. If specified, it overrides the preparer specified in the
111 * definition itself.
112 * @param flush If <code>true</code>, the response will be flushed after the insert.
113 * @param request The request.
114 * @throws IOException If something goes wrong.
115 */
116 private void renderTemplate(TilesContainer container, String template,
117 String templateType, String templateExpression, String role,
118 String preparer, boolean flush, Request request) throws IOException {
119 try {
120 AttributeContext attributeContext = container
121 .getAttributeContext(request);
122 Attribute templateAttribute = Attribute.createTemplateAttribute(template,
123 templateExpression, templateType, role);
124 attributeContext.setPreparer(preparer);
125 attributeContext.setTemplateAttribute(templateAttribute);
126 container.renderContext(request);
127 if (flush) {
128 request.getWriter().flush();
129 }
130 } finally {
131 container.endContext(request);
132 }
133 }
134 }