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

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

There is a newer version: 3.0.15.RELEASE
Show newest version
/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2012, 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.lang.reflect.Field;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.View;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.view.AbstractTemplateView;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.IWebContext;
import org.thymeleaf.fragment.ChainedFragmentSpec;
import org.thymeleaf.fragment.IFragmentSpec;
import org.thymeleaf.spring4.context.SpringWebContext;
import org.thymeleaf.spring4.naming.SpringContextVariableNames;


/**
 * 

* Base implementation of the Spring MVC {@link org.springframework.web.servlet.View} * interface. *

*

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

*

* This is the default view implementation resolved by {@link ThymeleafViewResolver}. *

* * @author Daniel Fernández * * @since 1.0 * */ public class ThymeleafView extends AbstractThymeleafView { /* * If this is not null, we are using Spring 3.1+ and there is the possibility * to automatically add @PathVariable's to models. This will be computed at class * initialization time. */ private static final String pathVariablesSelector; private IFragmentSpec fragmentSpec = null; static { /* * Compute whether we can obtain @PathVariable's from the request and add them * automatically to the model (Spring 3.1+) */ String pathVariablesSelectorValue = null; try { // We are looking for the value of the View.PATH_VARIABLES constant, which is a String final Field pathVariablesField = View.class.getDeclaredField("PATH_VARIABLES"); pathVariablesSelectorValue = (String) pathVariablesField.get(null); } catch (final NoSuchFieldException ignored) { pathVariablesSelectorValue = null; } catch (final IllegalAccessException ignored) { pathVariablesSelectorValue = null; } pathVariablesSelector = pathVariablesSelectorValue; } /** *

* Creates a new instance of ThymeleafView. *

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

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

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

* Returns the fragment specification ({@link IFragmentSpec}) defining the part * of the template that should be processed. *

*

* This fragment spec will be used for selecting the section of the template * that should be processed, discarding the rest of the template. If null, * the whole template will be processed. *

*

* Subclasses of {@link ThymeleafView} might choose not to honor this parameter, * disallowing the processing of template fragments. *

* * @return the fragment spec currently set, or null of no fragment has been * specified yet. * * @since 2.0.11 */ public IFragmentSpec getFragmentSpec() { return this.fragmentSpec; } /** *

* Sets the fragment specification ({@link IFragmentSpec}) defining the part * of the template that should be processed. *

*

* This fragment spec will be used for selecting the section of the template * that should be processed, discarding the rest of the template. If null, * the whole template will be processed. *

*

* Subclasses of {@link ThymeleafView} might choose not to honor this parameter, * disallowing the processing of template fragments. *

* * @param fragmentSpec the fragment specification to be set. * * @since 2.0.11 */ public void setFragmentSpec(final IFragmentSpec fragmentSpec) { this.fragmentSpec = fragmentSpec; } public void render(final Map model, final HttpServletRequest request, final HttpServletResponse response) throws Exception { renderFragment(null, model, request, response); } protected void renderFragment(final IFragmentSpec fragmentSpecToRender, final Map model, final HttpServletRequest request, final HttpServletResponse response) throws Exception { final ServletContext servletContext = getServletContext() ; if (getTemplateName() == null) { throw new IllegalArgumentException("Property 'templateName' is required"); } if (getLocale() == null) { throw new IllegalArgumentException("Property 'locale' is required"); } if (getTemplateEngine() == null) { throw new IllegalArgumentException("Property 'templateEngine' is required"); } final Map mergedModel = new HashMap(30); final Map templateStaticVariables = getStaticVariables(); if (templateStaticVariables != null) { mergedModel.putAll(templateStaticVariables); } if (pathVariablesSelector != null) { @SuppressWarnings("unchecked") final Map pathVars = (Map) request.getAttribute(pathVariablesSelector); if (pathVars != null) { mergedModel.putAll(pathVars); } } if (model != null) { mergedModel.putAll(model); } final RequestContext requestContext = new RequestContext(request, response, getServletContext(), mergedModel); // For compatibility with ThymeleafView addRequestContextAsVariable(mergedModel, SpringContextVariableNames.SPRING_REQUEST_CONTEXT, requestContext); // For compatibility with AbstractTemplateView addRequestContextAsVariable(mergedModel, AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, requestContext); final IWebContext context = new SpringWebContext(request, response, servletContext, getLocale(), mergedModel, getApplicationContext()); final TemplateEngine viewTemplateEngine = getTemplateEngine(); final String templateContentType = getContentType(); final Locale templateLocale = getLocale(); final String templateCharacterEncoding = getCharacterEncoding(); IFragmentSpec templateFragmentSpec = null; final IFragmentSpec viewFragmentSpec = getFragmentSpec(); if (viewFragmentSpec == null) { templateFragmentSpec = fragmentSpecToRender; } else { if (fragmentSpecToRender == null) { templateFragmentSpec = viewFragmentSpec; } else { templateFragmentSpec = new ChainedFragmentSpec(viewFragmentSpec, fragmentSpecToRender); } } response.setLocale(templateLocale); if (templateContentType != null) { response.setContentType(templateContentType); } else { response.setContentType(DEFAULT_CONTENT_TYPE); } if (templateCharacterEncoding != null) { response.setCharacterEncoding(templateCharacterEncoding); } viewTemplateEngine.process(getTemplateName(), context, templateFragmentSpec, response.getWriter()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy