All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.thymeleaf.spring4.view.AbstractThymeleafView Maven / Gradle / Ivy

There is a newer version: 3.0.15.RELEASE
Show newest version
/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2013, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   Licensed 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.thymeleaf.spring4.view;

import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletException;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.web.context.support.WebApplicationObjectSupport;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.support.RequestContext;
import org.thymeleaf.spring4.SpringTemplateEngine;


/**
 * 

* Abstract implementation class of the Spring MVC {@link org.springframework.web.servlet.View} * interface for Thymeleaf. *

*

* Views represent a template being executed, after being resolved (and * instantiated) by a {@link org.springframework.web.servlet.ViewResolver}. *

* * @author Daniel Fernández * @author Josh Long * * @since 2.0.11 * */ public abstract class AbstractThymeleafView extends WebApplicationObjectSupport implements View, BeanNameAware { /** *

* Default charset set to ISO-8859-1 for compatibility reasons with Spring's AbstractView. * Value is "text/html;charset=ISO-8859-1". *

*/ public static final String DEFAULT_CONTENT_TYPE = "text/html;charset=ISO-8859-1"; private String beanName = null; private String contentType = DEFAULT_CONTENT_TYPE; private boolean contentTypeSet = false; private String characterEncoding = null; private SpringTemplateEngine templateEngine = null; private String templateName = null; private Locale locale = null; private Map staticVariables = null; /** *

* Creates a new instance of ThymeleafView. *

*/ protected AbstractThymeleafView() { super(); } /** *

* Creates a new instance of ThymeleafView, specifying the * template name. *

* * @param templateName the template name. */ protected AbstractThymeleafView(final String templateName) { super(); this.templateName = templateName; } /** *

* Returns the content type that will used for this view. *

*

* Content type will be computed this way: *

*
    *
  • If a value is specified calling {@link #setContentType(String)} on * this object, that value will be used.
  • *
  • If a value is specified at the view resolver by calling * {@link ThymeleafViewResolver#setContentType(String)}, that one will * be used.
  • *
  • If none of the above is true, the {@link #DEFAULT_CONTENT_TYPE} constant * with value {@value #DEFAULT_CONTENT_TYPE} will be used.
  • *
* * @return the content type * @see ThymeleafViewResolver#getContentType() */ public String getContentType() { return this.contentType; } /** *

* Sets the content type that will used for this view. *

*

* Content type will be computed this way: *

*
    *
  • If a value is specified calling this method, that value will be used.
  • *
  • If a value is specified at the view resolver by calling * {@link ThymeleafViewResolver#setContentType(String)}, that one will * be used.
  • *
  • If none of the above is true, the {@link #DEFAULT_CONTENT_TYPE} constant * with value {@value #DEFAULT_CONTENT_TYPE} will be used.
  • *
* * @param contentType the content type to be used. * @see ThymeleafViewResolver#setContentType(String) */ public void setContentType(final String contentType) { this.contentType = contentType; this.contentTypeSet = true; } /* * Internally used (by ThymeleafViewResolver) in order to know whether a value * for the content type has been explicitly set or not. */ protected boolean isContentTypeSet() { return this.contentTypeSet; } /** *

* Returns the character encoding set to be used for rendering this view. *

*

* Many times, character encoding is specified as a part of the content * type using the {@link #setContentType(String)} method, but this is not mandatory, * and it could be that only the MIME type is specified that way, thus allowing * to set the character encoding using the {@link #setCharacterEncoding(String)} * counterpart of this getter method. *

* * @return the character encoding. */ public String getCharacterEncoding() { return this.characterEncoding; } /** *

* Specifies the character encoding to be set into the response when * the view is rendered. *

*

* Many times, character encoding is specified as a part of the content * type using the {@link #setContentType(String)} method, but this is not mandatory, * and it could be that only the MIME type is specified that way, thus allowing * to set the character encoding using this method. *

* * @param characterEncoding the character encoding to be used (e.g. UTF-8, * ISO-8859-1, etc.) */ public void setCharacterEncoding(final String characterEncoding) { this.characterEncoding = characterEncoding; } /** *

* Returns the bean name. *

* * @return the bean name. */ public String getBeanName() { return this.beanName; } /** *

* Sets the bean name. *

* * @param beanName the new bean name. */ public void setBeanName(final String beanName) { this.beanName = beanName; } /** *

* Returns the name of the template being processed by this view object. *

*

* This name will be specified in the same shape it will be resolved by the * template resolvers (i.e. as it is returned by controllers, without any * prefixes/suffixes). *

* * @return the template name. */ public String getTemplateName() { return this.templateName; } /** *

* Sets the name of the template to be processed by this view object. *

*

* This name will be specified in the same shape it will be resolved by the * template resolvers (i.e. as it is returned by controllers, without any * prefixes/suffixes). *

* * @param templateName the template name */ public void setTemplateName(final String templateName) { this.templateName = templateName; } /** *

* Returns the locale to be used for template processing. *

* * @return the locale */ protected Locale getLocale() { return this.locale; } /** *

* Sets the locale to be used for template processing. Usually, * the View Resolver will set this automatically from user session * / application data. *

* * @param locale the locale to be used. */ protected void setLocale(final Locale locale) { this.locale = locale; } /** *

* Returns the template engine instance –a {@link SpringTemplateEngine} instance, * specifically– to be used for processing the template specified by this view object. *

* * @return the template engine instance */ protected SpringTemplateEngine getTemplateEngine() { return this.templateEngine; } /** *

* Sets the template engine instance –a {@link SpringTemplateEngine} instance, * specifically– to be used for processing the template specified by this view object. *

* * @param templateEngine the template engine instance to be used */ protected void setTemplateEngine(final SpringTemplateEngine templateEngine) { this.templateEngine = templateEngine; } /** *

* Return the static variables, which will be available at the context * every time this view is processed. *

*

* These static variables are added to the context before the view is * processed, so that they can be referenced from the context like any * other context variables, for example: ${myStaticVar}. *

* * @return the map of static variables to be set into view's execution. */ public Map getStaticVariables() { if (this.staticVariables == null) { return Collections.emptyMap(); } return Collections.unmodifiableMap(this.staticVariables); } /** *

* Add a new static variable. *

*

* These static variables are added to the context before this view * is processed, so that they can be referenced from * the context like any other context variables, for example: * ${myStaticVar}. *

* * @param name the name of the static variable * @param value the value of the static variable */ public void addStaticVariable(final String name, final Object value) { if (this.staticVariables == null) { this.staticVariables = new HashMap(3, 1.0f); } this.staticVariables.put(name, value); } /** *

* Sets a set of static variables, which will be available at the context * when this view is processed. *

*

* This method does not overwrite the existing static variables, it * simply adds the ones specify to any variables already registered. *

*

* These static variables are added to the context before this view is * processed, so that they can be referenced from * the context like any other context variables, for example: * ${myStaticVar}. *

* * * @param variables the set of variables to be added. */ public void setStaticVariables(final Map variables) { if (variables != null) { if (this.staticVariables == null) { this.staticVariables = new HashMap(3, 1.0f); } this.staticVariables.putAll(variables); } } protected static void addRequestContextAsVariable( final Map model, final String variableName, final RequestContext requestContext) throws ServletException { if (model.containsKey(variableName)) { throw new ServletException( "Cannot expose request context in model attribute '" + variableName + "' because of an existing model object of the same name"); } model.put(variableName, requestContext); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy