org.apache.tiles.template.InsertDefinitionModel Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tiles.template;
import org.apache.tiles.api.Attribute;
import org.apache.tiles.api.AttributeContext;
import org.apache.tiles.api.TilesContainer;
import org.apache.tiles.api.access.TilesAccess;
import org.apache.tiles.autotag.core.runtime.ModelBody;
import org.apache.tiles.autotag.core.runtime.annotation.Parameter;
import org.apache.tiles.request.Request;
import java.io.IOException;
/**
*
* Insert a definition.
*
*
* Insert a definition with the possibility to override and specify parameters
* (called attributes). A definition can be seen as a (partially or totally)
* filled template that can override or complete attribute values.
* <tiles:insertDefinition>
allows to define these attributes
* and pass them to the inserted jsp page, called template. Attributes are
* defined using nested tag <tiles:putAttribute>
or
* <tiles:putListAttribute>
.
*
*
* You must specify name
tag attribute, for inserting a definition
* from definitions factory.
*
*
* Example :
*
*
*
* <code>
* <tiles:insertDefinition name=".my.tiles.defininition flush="true">
* <tiles:putAttribute name="title" value="My first page" />
* <tiles:putAttribute name="header" value="/common/header.jsp" />
* <tiles:putAttribute name="footer" value="/common/footer.jsp" />
* <tiles:putAttribute name="menu" value="/basic/menu.jsp" />
* <tiles:putAttribute name="body" value="/basic/helloBody.jsp" />
* </tiles:insertDefinition>
* </code>
*
* @since 2.2.0
*/
public class InsertDefinitionModel {
/**
* Executes the operation.
*
* @param definitionName The name of the definition to render.
* @param template If specified, this template will be used instead of the
* one used by the definition.
* @param templateType The type of the template attribute.
* @param templateExpression The expression to evaluate to get the value of the template.
* @param role A comma-separated list of roles. If present, the definition
* will be rendered only if the current user belongs to one of the roles.
* @param preparer The preparer to use to invoke before the definition is
* rendered. If specified, it overrides the preparer specified in the
* definition itself.
* @param flush If true
, the response will be flushed after the insert.
* @param request The request.
* @param modelBody The body.
* @throws IOException If something goes wrong.
* @since 2.2.0
*/
public void execute(
@Parameter(name = "name", required = true) String definitionName,
String template,
String templateType,
String templateExpression,
String role,
String preparer,
boolean flush,
Request request,
ModelBody modelBody
) throws IOException {
TilesContainer container = TilesAccess.getCurrentContainer(request);
container.startContext(request);
modelBody.evaluateWithoutWriting();
container = TilesAccess.getCurrentContainer(request);
renderDefinition(container, definitionName, template, templateType, templateExpression, role, preparer, flush, request);
}
/**
* Renders a definition.
*
* @param container The container to use.
* @param definitionName The name of the definition to render.
* @param template If specified, this template will be used instead of the
* one used by the definition.
* @param templateType The type of the template attribute.
* @param templateExpression The expression to evaluate to get the value of the template.
* @param role A comma-separated list of roles. If present, the definition
* will be rendered only if the current user belongs to one of the roles.
* @param preparer The preparer to use to invoke before the definition is
* rendered. If specified, it overrides the preparer specified in the
* definition itself.
* @param flush If true
, the response will be flushed after the insert.
* @param request The request.
* @throws IOException If something goes wrong.
*/
private void renderDefinition(
TilesContainer container,
String definitionName,
String template,
String templateType,
String templateExpression,
String role,
String preparer,
boolean flush,
Request request
) throws IOException {
try {
AttributeContext attributeContext = container.getAttributeContext(request);
Attribute templateAttribute = Attribute.createTemplateAttribute(template, templateExpression, templateType, role);
attributeContext.setPreparer(preparer);
attributeContext.setTemplateAttribute(templateAttribute);
container.render(definitionName, request);
if (flush) {
request.getWriter().flush();
}
} finally {
container.endContext(request);
}
}
}