Package org.thymeleaf

Examples of org.thymeleaf.TemplateEngine


    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setCacheTTLMs(3600000L);
    templateResolver.setCacheable(false);

    templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);

    templateEngine.addDialect(new DataTablesDialect());
  }
View Full Code Here


    // the configuration of the Thymeleaf TemplateEngine is static and we need to recreate on modification
    private synchronized void configureTemplateEngine() {
        logger.info("configure template engine");
        if (templateEngine == null || templateEngine.isInitialized()) {
            templateEngine = new TemplateEngine();
        }
        if (templateResolvers.size() > 0) {
            templateEngine.setTemplateResolvers(templateResolvers);
        }
        if (messageResolvers.size() > 0) {
View Full Code Here

      final HttpServletResponse res) throws Throwable {
    final ControllerResource ctrlRes = WebApplication.DFLT.resolve(req);
    if (ctrlRes == null) {
      return false;
    }
    final TemplateEngine templateEngine = WebApplication.DFLT
        .getTemplateEngine();

    res.setContentType("text/html;charset=UTF-8");
    res.setHeader("Pragma", "no-cache");
    res.setHeader("Cache-Control", "no-cache");
View Full Code Here

            final HttpServletResponse response)
            throws Exception {

        final ServletContext servletContext = getServletContext() ;
        final String viewTemplateName = getTemplateName();
        final TemplateEngine viewTemplateEngine = getTemplateEngine();
        if (!viewTemplateEngine.isInitialized()) {
            viewTemplateEngine.initialize();
        }

        if (viewTemplateName == null) {
            throw new IllegalArgumentException("Property 'templateName' is required");
        }
        if (getLocale() == null) {
            throw new IllegalArgumentException("Property 'locale' is required");
        }
        if (viewTemplateEngine == null) {
            throw new IllegalArgumentException("Property 'templateEngine' is required");
        }


        final Map<String, Object> mergedModel = new HashMap<String, Object>(30);
        final Map<String, Object> templateStaticVariables = getStaticVariables();
        if (templateStaticVariables != null) {
            mergedModel.putAll(templateStaticVariables);
        }
        if (pathVariablesSelector != null) {
            @SuppressWarnings("unchecked")
            final Map<String, Object> pathVars = (Map<String, Object>) request.getAttribute(pathVariablesSelector);
            if (pathVars != null) {
                mergedModel.putAll(pathVars);
            }
        }
        if (model != null) {
            mergedModel.putAll(model);
        }

        final ApplicationContext applicationContext = getApplicationContext();

        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);


        // Expose Thymeleaf's own evaluation context as a model variable
        final ConversionService conversionService =
                (ConversionService) request.getAttribute(ConversionService.class.getName()); // might be null!
        final ThymeleafEvaluationContext evaluationContext =
                new ThymeleafEvaluationContext(applicationContext, conversionService);
        mergedModel.put(ThymeleafEvaluationContext.THYMELEAF_EVALUATION_CONTEXT_CONTEXT_VARIABLE_NAME, evaluationContext);

       
        final SpringWebContext context =
                new SpringWebContext(request, response, servletContext, getLocale(), mergedModel, getApplicationContext());


        final String templateName;
        final IFragmentSpec nameFragmentSpec;
        if (!viewTemplateName.contains("::")) {
            // No fragment specified at the template name

            templateName = viewTemplateName;
            nameFragmentSpec = null;

        } else {
            // Template name contains a fragment name, so we should parse it as such

            final Configuration configuration = viewTemplateEngine.getConfiguration();
            final ProcessingContext processingContext = new ProcessingContext(context);

            final String dialectPrefix = getStandardDialectPrefix(configuration);

            final StandardFragment fragment =
                    StandardFragmentProcessor.computeStandardFragmentSpec(
                            configuration, processingContext, viewTemplateName, dialectPrefix, StandardFragmentAttrProcessor.ATTR_NAME);

            if (fragment == null) {
                throw new IllegalArgumentException("Invalid template name specification: '" + viewTemplateName + "'");
            }

            templateName = fragment.getTemplateName();
            nameFragmentSpec = fragment.getFragmentSpec();
            final Map<String,Object> nameFragmentParameters = fragment.getParameters();

            if (nameFragmentParameters != null) {

                if (FragmentSelectionUtils.parameterNamesAreSynthetic(nameFragmentParameters.keySet())) {
                    // We cannot allow synthetic parameters because there is no way to specify them at the template
                    // engine execution!
                    throw new IllegalArgumentException(
                            "Parameters in a view specification must be named (non-synthetic): '" + viewTemplateName + "'");
                }

                context.setVariables(nameFragmentParameters);

            }


        }


        final String templateContentType = getContentType();
        final Locale templateLocale = getLocale();
        final String templateCharacterEncoding = getCharacterEncoding();
       
        IFragmentSpec templateFragmentSpec = fragmentSpecToRender;
        final IFragmentSpec viewFragmentSpec = getFragmentSpec();
        if (viewFragmentSpec != null) {
            if (templateFragmentSpec == null) {
                templateFragmentSpec = viewFragmentSpec;
            } else {
                templateFragmentSpec =
                    new ChainedFragmentSpec(viewFragmentSpec, templateFragmentSpec);
            }
        }
        if (nameFragmentSpec != null) {
            if (templateFragmentSpec == null) {
                templateFragmentSpec = nameFragmentSpec;
            } else {
                templateFragmentSpec =
                        new ChainedFragmentSpec(nameFragmentSpec, templateFragmentSpec);
            }
        }
       

        response.setLocale(templateLocale);
        if (templateContentType != null) {
            response.setContentType(templateContentType);
        } else {
            response.setContentType(DEFAULT_CONTENT_TYPE);
        }
        if (templateCharacterEncoding != null) {
            response.setCharacterEncoding(templateCharacterEncoding);
        }
       
        viewTemplateEngine.process(templateName, context, templateFragmentSpec, response.getWriter());
       
    }
View Full Code Here

            final HttpServletResponse response)
            throws Exception {

        final ServletContext servletContext = getServletContext() ;
        final String viewTemplateName = getTemplateName();
        final TemplateEngine viewTemplateEngine = getTemplateEngine();
        if (!viewTemplateEngine.isInitialized()) {
            viewTemplateEngine.initialize();
        }

        if (viewTemplateName == null) {
            throw new IllegalArgumentException("Property 'templateName' is required");
        }
        if (getLocale() == null) {
            throw new IllegalArgumentException("Property 'locale' is required");
        }
        if (viewTemplateEngine == null) {
            throw new IllegalArgumentException("Property 'templateEngine' is required");
        }


        final Map<String, Object> mergedModel = new HashMap<String, Object>(30);
        final Map<String, Object> templateStaticVariables = getStaticVariables();
        if (templateStaticVariables != null) {
            mergedModel.putAll(templateStaticVariables);
        }
        if (pathVariablesSelector != null) {
            @SuppressWarnings("unchecked")
            final Map<String, Object> pathVars = (Map<String, Object>) request.getAttribute(pathVariablesSelector);
            if (pathVars != null) {
                mergedModel.putAll(pathVars);
            }
        }
        if (model != null) {
            mergedModel.putAll(model);
        }

        final ApplicationContext applicationContext = getApplicationContext();

        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);


        // Expose Thymeleaf's own evaluation context as a model variable
        final ConversionService conversionService =
                (ConversionService) request.getAttribute(ConversionService.class.getName()); // might be null!
        final ThymeleafEvaluationContext evaluationContext =
                new ThymeleafEvaluationContext(applicationContext, conversionService);
        mergedModel.put(ThymeleafEvaluationContext.THYMELEAF_EVALUATION_CONTEXT_CONTEXT_VARIABLE_NAME, evaluationContext);

       
        final SpringWebContext context =
                new SpringWebContext(request, response, servletContext, getLocale(), mergedModel, getApplicationContext());


        final String templateName;
        final IFragmentSpec nameFragmentSpec;
        if (!viewTemplateName.contains("::")) {
            // No fragment specified at the template name

            templateName = viewTemplateName;
            nameFragmentSpec = null;

        } else {
            // Template name contains a fragment name, so we should parse it as such

            final Configuration configuration = viewTemplateEngine.getConfiguration();
            final ProcessingContext processingContext = new ProcessingContext(context);

            final String dialectPrefix = getStandardDialectPrefix(configuration);

            final StandardFragment fragment =
                    StandardFragmentProcessor.computeStandardFragmentSpec(
                            configuration, processingContext, viewTemplateName, dialectPrefix, StandardFragmentAttrProcessor.ATTR_NAME);

            if (fragment == null) {
                throw new IllegalArgumentException("Invalid template name specification: '" + viewTemplateName + "'");
            }

            templateName = fragment.getTemplateName();
            nameFragmentSpec = fragment.getFragmentSpec();
            final Map<String,Object> nameFragmentParameters = fragment.getParameters();

            if (nameFragmentParameters != null) {

                if (FragmentSelectionUtils.parameterNamesAreSynthetic(nameFragmentParameters.keySet())) {
                    // We cannot allow synthetic parameters because there is no way to specify them at the template
                    // engine execution!
                    throw new IllegalArgumentException(
                            "Parameters in a view specification must be named (non-synthetic): '" + viewTemplateName + "'");
                }

                context.setVariables(nameFragmentParameters);

            }


        }


        final String templateContentType = getContentType();
        final Locale templateLocale = getLocale();
        final String templateCharacterEncoding = getCharacterEncoding();
       
        IFragmentSpec templateFragmentSpec = fragmentSpecToRender;
        final IFragmentSpec viewFragmentSpec = getFragmentSpec();
        if (viewFragmentSpec != null) {
            if (templateFragmentSpec == null) {
                templateFragmentSpec = viewFragmentSpec;
            } else {
                templateFragmentSpec =
                    new ChainedFragmentSpec(viewFragmentSpec, templateFragmentSpec);
            }
        }
        if (nameFragmentSpec != null) {
            if (templateFragmentSpec == null) {
                templateFragmentSpec = nameFragmentSpec;
            } else {
                templateFragmentSpec =
                        new ChainedFragmentSpec(nameFragmentSpec, templateFragmentSpec);
            }
        }
       

        response.setLocale(templateLocale);
        if (templateContentType != null) {
            response.setContentType(templateContentType);
        } else {
            response.setContentType(DEFAULT_CONTENT_TYPE);
        }
        if (templateCharacterEncoding != null) {
            response.setCharacterEncoding(templateCharacterEncoding);
        }
       
        viewTemplateEngine.process(templateName, context, templateFragmentSpec, response.getWriter());
       
    }
View Full Code Here

            if (getTemplateEngine() == null) {
                throw new IllegalArgumentException("Property 'templateEngine' is required");
            }

            final TemplateEngine templateEngine = getTemplateEngine();
            final Configuration configuration = templateEngine.getConfiguration();

            final String dialectPrefix = getStandardDialectPrefix(configuration);
            final String fragmentSignatureAttributeName = StandardFragmentAttrProcessor.ATTR_NAME;

            final DOMSelector.INodeReferenceChecker nodeReferenceChecker =
View Full Code Here

            if (getTemplateEngine() == null) {
                throw new IllegalArgumentException("Property 'templateEngine' is required");
            }

            final TemplateEngine templateEngine = getTemplateEngine();
            final Configuration configuration = templateEngine.getConfiguration();

            final String dialectPrefix = getStandardDialectPrefix(configuration);
            final String fragmentSignatureAttributeName = StandardFragmentAttrProcessor.ATTR_NAME;

            final DOMSelector.INodeReferenceChecker nodeReferenceChecker =
View Full Code Here

        templateResolver.setSuffix(".html");
        templateResolver.setCharacterEncoding("utf-8");
        templateResolver.setCacheTTLMs(Long.valueOf(3600000L));
       
//        resolverEncoding = templateResolver.getCharacterEncoding();
        templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
  }
View Full Code Here

        templateResolver.setTemplateMode("HTML5");
        templateResolver.setSuffix(".html");
        templateResolver.setCacheable(cacheable);
        templateResolver.setCacheTTLMs(timeToLive);

        templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
    }
View Full Code Here

    public static String getOutputFor(final Node node, final AbstractGeneralTemplateWriter templateWriter, final String templateMode) {
       
        Validate.notNull(node, "Node cannot be null");
        Validate.notNull(templateWriter, "Template writer cannot be null");

        final TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.addTemplateResolver(new ClassLoaderTemplateResolver());
        templateEngine.addMessageResolver(new StandardMessageResolver());
        templateEngine.setTemplateModeHandlers(StandardTemplateModeHandlers.ALL_TEMPLATE_MODE_HANDLERS);
       
        final String templateName = "output";

        final TemplateProcessingParameters templateProcessingParameters =
                new TemplateProcessingParameters(templateEngine.getConfiguration(), templateName, new Context());
       
        final TemplateResolution templateResolution =
                new TemplateResolution(templateName, "resource:"+templateName,
                        new ClassLoaderResourceResolver(), "UTF-8", templateMode, new AlwaysValidTemplateResolutionValidity());

        templateEngine.initialize();
       
        final TemplateRepository templateRepository = templateEngine.getTemplateRepository();
       
        final Document document = new Document(templateName);
        document.addChild(node);
       
       
        final Arguments arguments =
                new Arguments(new TemplateEngine(),
                        templateProcessingParameters, templateResolution,
                        templateRepository, document);

        return getOutputFor(arguments, node, templateWriter);
           
View Full Code Here

TOP

Related Classes of org.thymeleaf.TemplateEngine

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.