1/*2 * $Id: InsertTemplateTag.java 929598 2010-03-31 15:53:02Z apetrelli $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122package org.apache.tiles.jsp.taglib;
2324import java.io.IOException;
2526import javax.servlet.jsp.JspContext;
27import javax.servlet.jsp.JspException;
28import javax.servlet.jsp.tagext.SimpleTagSupport;
2930import org.apache.tiles.jsp.context.JspUtil;
31import org.apache.tiles.template.InsertTemplateModel;
3233/***34 * This is the tag handler for <tiles:insertTemplate>, which includes a35 * template ready to be filled.36 *37 * @version $Rev: 929598 $ $Date: 2010-03-31 17:53:02 +0200 (mer, 31 mar 2010) $38 */39publicclassInsertTemplateTagextends SimpleTagSupport {
4041/***42 * The template model.43 */44private InsertTemplateModel model = new InsertTemplateModel();
4546/***47 * The role to check. If the user is in the specified role, the tag is taken48 * into account; otherwise, the tag is ignored (skipped).49 *50 * @since 2.1.151 */52private String role;
5354/***55 * The view preparer to use before the rendering.56 *57 * @since 2.1.158 */59private String preparer;
6061/***62 * This flag, if <code>true</code>, flushes the content after rendering.63 *64 * @since 2.1.165 */66privateboolean flush;
6768/***69 * This flag, if <code>true</code>, ignores exception thrown by preparers70 * and those caused by problems with definitions.71 *72 * @since 2.1.173 */74privateboolean ignore;
7576/***77 * A string representing the URI of a template (for example, a JSP page).78 *79 * @since 2.1.080 */81private String template;
8283/***84 * The type of the template attribute.85 *86 * @since 2.2.087 */88private String templateType;
8990/***91 * The expression to evaluate to get the value of the template.92 *93 * @since 2.2.094 */95private String templateExpression;
9697/***98 * Returns the role to check. If the user is in the specified role, the tag is99 * taken into account; otherwise, the tag is ignored (skipped).100 *101 * @return The role to check.102 * @since 2.1.1103 */104public String getRole() {
105return role;
106 }
107108/***109 * Sets the role to check. If the user is in the specified role, the tag is110 * taken into account; otherwise, the tag is ignored (skipped).111 *112 * @param role The role to check.113 * @since 2.1.1114 */115publicvoid setRole(String role) {
116this.role = role;
117 }
118119/***120 * Returns the preparer name.121 *122 * @return The preparer name.123 * @since 2.1.1124 */125public String getPreparer() {
126return preparer;
127 }
128129/***130 * Sets the preparer name.131 *132 * @param preparer The preparer name.133 * @since 2.1.1134 */135publicvoid setPreparer(String preparer) {
136this.preparer = preparer;
137 }
138139/***140 * Returns the flush flag. If <code>true</code>, current page out stream141 * is flushed after insertion.142 *143 * @return The flush flag.144 * @since 2.1.1145 */146publicboolean isFlush() {
147return flush;
148 }
149150/***151 * Sets the flush flag. If <code>true</code>, current page out stream152 * is flushed after insertion.153 *154 * @param flush The flush flag.155 * @since 2.1.1156 */157publicvoid setFlush(boolean flush) {
158this.flush = flush;
159 }
160161/***162 * Returns the ignore flag. If it is set to true, and the attribute163 * specified by the name does not exist, simply return without writing164 * anything. The default value is false, which will cause a runtime165 * exception to be thrown.166 *167 * @return The ignore flag.168 * @since 2.1.1169 */170publicboolean isIgnore() {
171return ignore;
172 }
173174/***175 * Sets the ignore flag. If this attribute is set to true, and the attribute176 * specified by the name does not exist, simply return without writing177 * anything. The default value is false, which will cause a runtime178 * exception to be thrown.179 *180 * @param ignore The ignore flag.181 * @since 2.1.1182 */183publicvoid setIgnore(boolean ignore) {
184this.ignore = ignore;
185 }
186187/***188 * Returns a string representing the URI of a template (for example, a JSP189 * page).190 *191 * @return The template URI.192 * @since 2.1.0193 */194public String getTemplate() {
195return template;
196 }
197198/***199 * Sets a string representing the URI of a template (for example, a JSP200 * page).201 *202 * @param template The template URI.203 * @since 2.1.0204 */205publicvoid setTemplate(String template) {
206this.template = template;
207 }
208209/***210 * Returns the type of the template attribute.211 *212 * @return The template type.213 * @since 2.2.0214 */215public String getTemplateType() {
216return templateType;
217 }
218219/***220 * Sets the type of the template attribute.221 *222 * @param templateType The template type.223 * @since 2.2.0224 */225publicvoid setTemplateType(String templateType) {
226this.templateType = templateType;
227 }
228229/***230 * Returns the expression to evaluate to get the value of the template.231 *232 * @return The template expression.233 * @since 2.2.0234 */235public String getTemplateExpression() {
236return templateExpression;
237 }
238239/***240 * Sets the expression to evaluate to get the value of the template.241 *242 * @param templateExpression The template expression.243 * @since 2.2.0244 */245publicvoid setTemplateExpression(String templateExpression) {
246this.templateExpression = templateExpression;
247 }
248249/*** {@inheritDoc} */250 @Override
251publicvoid doTag() throws JspException, IOException {
252 JspContext jspContext = getJspContext();
253 model.start(JspUtil.getCurrentContainer(jspContext), jspContext);
254 JspUtil.evaluateFragment(getJspBody());
255 model.end(JspUtil.getCurrentContainer(jspContext), template,
256 templateType, templateExpression, role, preparer, jspContext);
257if(isFlush()){
258 jspContext.getOut().flush();
259 }
260 }
261 }