org.thymeleaf.spring4.SpringTemplateEngine Maven / Gradle / Ivy
Show all versions of thymeleaf-spring4 Show documentation
/*
* =============================================================================
*
* Copyright (c) 2011-2014, 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 org.springframework.beans.factory.InitializingBean;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.thymeleaf.ITemplateEngine;
import org.thymeleaf.TemplateEngine;
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 ITemplateEngine} meant for Spring MVC 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 "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 MessageSourceAware, InitializingBean {
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 "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
*/
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
*/
public void setTemplateEngineMessageSource(final MessageSource templateEngineMessageSource) {
this.templateEngineMessageSource = templateEngineMessageSource;
}
public void afterPropertiesSet() throws Exception {
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);
}
}