org.thymeleaf.spring4.SpringTemplateEngine Maven / Gradle / Ivy
Show all versions of thymeleaf-spring4 Show documentation
/*
* =============================================================================
*
* Copyright (c) 2011-2018, 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;
import java.util.Set;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.dialect.IDialect;
import org.thymeleaf.messageresolver.IMessageResolver;
import org.thymeleaf.messageresolver.StandardMessageResolver;
import org.thymeleaf.spring4.dialect.SpringStandardDialect;
import org.thymeleaf.spring4.messageresolver.SpringMessageResolver;
/**
*
* Implementation of {@link ISpringTemplateEngine}, meant for Spring applications,
* that establishes by default an instance of {@link SpringStandardDialect}
* as a dialect (instead of an instance of {@link org.thymeleaf.standard.StandardDialect}.
*
*
* It also configures a {@link SpringMessageResolver} as message resolver, and
* implements the {@link MessageSourceAware} interface in order to let Spring
* automatically setting the {@link MessageSource} used at the application
* (bean needs to have id {@code "messageSource"}). If this Spring standard setting
* needs to be overridden, the {@link #setTemplateEngineMessageSource(MessageSource)} can
* be used.
*
*
* @author Daniel Fernández
*
* @since 1.0
*
*/
public class SpringTemplateEngine
extends TemplateEngine
implements ISpringTemplateEngine, MessageSourceAware {
private static final SpringStandardDialect SPRINGSTANDARD_DIALECT = new SpringStandardDialect();
private MessageSource messageSource = null;
private MessageSource templateEngineMessageSource = null;
public SpringTemplateEngine() {
super();
// This will set the SpringStandardDialect, overriding the Standard one set in the super constructor
super.setDialect(SPRINGSTANDARD_DIALECT);
}
/**
*
* Implementation of the {@link MessageSourceAware#setMessageSource(MessageSource)}
* method at the {@link MessageSourceAware} interface, provided so that
* Spring is able to automatically set the currently configured {@link MessageSource} into
* this template engine.
*
*
* If several {@link MessageSource} implementation beans exist, Spring will inject here
* the one with id {@code "messageSource"}.
*
*
* This property should not be set manually in most scenarios (see
* {@link #setTemplateEngineMessageSource(MessageSource)} instead).
*
*
* @param messageSource the message source to be used by the message resolver
*/
@Override
public void setMessageSource(final MessageSource messageSource) {
this.messageSource = messageSource;
}
/**
*
* Convenience method for setting the message source that will
* be used by this template engine, overriding the one automatically set by
* Spring at the {@link #setMessageSource(MessageSource)} method.
*
*
* @param templateEngineMessageSource the message source to be used by the message resolver
* @since 2.0.15
*/
@Override
public void setTemplateEngineMessageSource(final MessageSource templateEngineMessageSource) {
this.templateEngineMessageSource = templateEngineMessageSource;
}
/**
*
* Returns whether the SpringEL compiler should be enabled in SpringEL expressions or not.
*
*
* (This is just a convenience method, equivalent to calling
* {@link SpringStandardDialect#getEnableSpringELCompiler()} on the dialect instance itself. It is provided
* here in order to allow users to enable the SpEL compiler without
* having to directly create instances of the {@link SpringStandardDialect})
*
*
* Expression compilation can significantly improve the performance of Spring EL expressions, but
* might not be adequate for every environment. Read
* the
* official Spring documentation for more detail.
*
*
* Also note that although Spring includes a SpEL compiler since Spring 4.1, most expressions
* in Thymeleaf templates will only be able to properly benefit from this compilation step when at least
* Spring Framework version 4.2.4 is used.
*
*
* This flag is set to {@code false} by default.
*
*
* @return {@code true} if SpEL expressions should be compiled if possible, {@code false} if not.
*/
public boolean getEnableSpringELCompiler() {
final Set dialects = getDialects();
for (final IDialect dialect : dialects) {
if (dialect instanceof SpringStandardDialect) {
return ((SpringStandardDialect) dialect).getEnableSpringELCompiler();
}
}
return false;
}
/**
*
* Sets whether the SpringEL compiler should be enabled in SpringEL expressions or not.
*
*
* (This is just a convenience method, equivalent to calling
* {@link SpringStandardDialect#setEnableSpringELCompiler(boolean)} on the dialect instance itself. It is provided
* here in order to allow users to enable the SpEL compiler without
* having to directly create instances of the {@link SpringStandardDialect})
*
*
* Expression compilation can significantly improve the performance of Spring EL expressions, but
* might not be adequate for every environment. Read
* the
* official Spring documentation for more detail.
*
*
* Also note that although Spring includes a SpEL compiler since Spring 4.1, most expressions
* in Thymeleaf templates will only be able to properly benefit from this compilation step when at least
* Spring Framework version 4.2.4 is used.
*
*
* This flag is set to {@code false} by default.
*
*
* @param enableSpringELCompiler {@code true} if SpEL expressions should be compiled if possible, {@code false} if not.
*/
public void setEnableSpringELCompiler(final boolean enableSpringELCompiler) {
final Set dialects = getDialects();
for (final IDialect dialect : dialects) {
if (dialect instanceof SpringStandardDialect) {
((SpringStandardDialect) dialect).setEnableSpringELCompiler(enableSpringELCompiler);
}
}
}
/**
*
* Returns whether the {@code } marker tags rendered to signal the presence
* of checkboxes in forms when unchecked should be rendered before the checkbox tag itself,
* or after (default).
*
*
* (This is just a convenience method, equivalent to calling
* {@link SpringStandardDialect#getRenderHiddenMarkersBeforeCheckboxes()} on the dialect instance
* itself. It is provided here in order to allow users to modify this behaviour without
* having to directly create instances of the {@link SpringStandardDialect})
*
*
* A number of CSS frameworks and style guides assume that the {@code
*
* Note this hidden field is introduced in order to signal the existence of the field in the form being sent,
* even if the checkbox is unchecked (no URL parameter is added for unchecked check boxes).
*
*
* This flag is set to {@code false} by default.
*
*
* @return {@code true} if hidden markers should be rendered before the checkboxes, {@code false} if not.
*
* @since 3.0.10
*/
public boolean getRenderHiddenMarkersBeforeCheckboxes() {
final Set dialects = getDialects();
for (final IDialect dialect : dialects) {
if (dialect instanceof SpringStandardDialect) {
return ((SpringStandardDialect) dialect).getRenderHiddenMarkersBeforeCheckboxes();
}
}
return false;
}
/**
*
* Sets whether the {@code } marker tags rendered to signal the presence
* of checkboxes in forms when unchecked should be rendered before the checkbox tag itself,
* or after (default).
*
*
* (This is just a convenience method, equivalent to calling
* {@link SpringStandardDialect#setRenderHiddenMarkersBeforeCheckboxes(boolean)} on the dialect instance
* itself. It is provided here in order to allow users to modify this behaviour without
* having to directly create instances of the {@link SpringStandardDialect})
*
*
* A number of CSS frameworks and style guides assume that the {@code
*
* Note this hidden field is introduced in order to signal the existence of the field in the form being sent,
* even if the checkbox is unchecked (no URL parameter is added for unchecked check boxes).
*
*
* This flag is set to {@code false} by default.
*
*
* @param renderHiddenMarkersBeforeCheckboxes {@code true} if hidden markers should be rendered
* before the checkboxes, {@code false} if not.
*
* @since 3.0.10
*/
public void setRenderHiddenMarkersBeforeCheckboxes(final boolean renderHiddenMarkersBeforeCheckboxes) {
final Set dialects = getDialects();
for (final IDialect dialect : dialects) {
if (dialect instanceof SpringStandardDialect) {
((SpringStandardDialect) dialect).setRenderHiddenMarkersBeforeCheckboxes(renderHiddenMarkersBeforeCheckboxes);
}
}
}
@Override
protected final void initializeSpecific() {
// First of all, give the opportunity to subclasses to apply their own configurations
initializeSpringSpecific();
// Once the subclasses have had their opportunity, compute configurations belonging to SpringTemplateEngine
super.initializeSpecific();
final MessageSource messageSource =
this.templateEngineMessageSource == null ? this.messageSource : this.templateEngineMessageSource;
final IMessageResolver messageResolver;
if (messageSource != null) {
final SpringMessageResolver springMessageResolver = new SpringMessageResolver();
springMessageResolver.setMessageSource(messageSource);
messageResolver = springMessageResolver;
} else {
messageResolver = new StandardMessageResolver();
}
super.setMessageResolver(messageResolver);
}
/**
*
* This method performs additional initializations required for a
* {@code SpringTemplateEngine} subclass instance. This method
* is called before the first execution of
* {@link TemplateEngine#process(String, org.thymeleaf.context.IContext)}
* or {@link TemplateEngine#processThrottled(String, org.thymeleaf.context.IContext)}
* in order to create all the structures required for a quick execution of
* templates.
*
*
* THIS METHOD IS INTERNAL AND SHOULD NEVER BE CALLED DIRECTLY.
*
*
* The implementation of this method does nothing, and it is designed
* for being overridden by subclasses of {@code SpringTemplateEngine}.
*
*/
protected void initializeSpringSpecific() {
// Nothing to be executed here. Meant for extension
}
}