1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception { this.renderFragment(this.markupSelectors, model, request, response); }
protected void renderFragment(Set<String> markupSelectorsToRender, Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception { ServletContext servletContext = this.getServletContext(); String viewTemplateName = this.getTemplateName(); ISpringTemplateEngine viewTemplateEngine = this.getTemplateEngine(); if (viewTemplateName == null) { throw new IllegalArgumentException("Property 'templateName' is required"); } else if (this.getLocale() == null) { throw new IllegalArgumentException("Property 'locale' is required"); } else if (viewTemplateEngine == null) { throw new IllegalArgumentException("Property 'templateEngine' is required"); } else { Map<String, Object> mergedModel = new HashMap(30); Map<String, Object> templateStaticVariables = this.getStaticVariables(); if (templateStaticVariables != null) { mergedModel.putAll(templateStaticVariables); }
if (pathVariablesSelector != null) { Map<String, Object> pathVars = (Map)request.getAttribute(pathVariablesSelector); if (pathVars != null) { mergedModel.putAll(pathVars); } }
if (model != null) { mergedModel.putAll(model); }
ApplicationContext applicationContext = this.getApplicationContext(); RequestContext requestContext = new RequestContext(request, response, this.getServletContext(), mergedModel); SpringWebMvcThymeleafRequestContext thymeleafRequestContext = new SpringWebMvcThymeleafRequestContext(requestContext, request); addRequestContextAsVariable(mergedModel, "springRequestContext", requestContext); addRequestContextAsVariable(mergedModel, "springMacroRequestContext", requestContext); mergedModel.put("thymeleafRequestContext", thymeleafRequestContext); ConversionService conversionService = (ConversionService)request.getAttribute(ConversionService.class.getName()); ThymeleafEvaluationContext evaluationContext = new ThymeleafEvaluationContext(applicationContext, conversionService); mergedModel.put("thymeleaf::EvaluationContext", evaluationContext); IEngineConfiguration configuration = viewTemplateEngine.getConfiguration(); WebExpressionContext context = new WebExpressionContext(configuration, request, response, servletContext, this.getLocale(), mergedModel); String templateName; Set markupSelectors; if (!viewTemplateName.contains("::")) { templateName = viewTemplateName; markupSelectors = null; } else { IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);
FragmentExpression fragmentExpression; try { fragmentExpression = (FragmentExpression)parser.parseExpression(context, "~{" + viewTemplateName + "}"); } catch (TemplateProcessingException var24) { throw new IllegalArgumentException("Invalid template name specification: '" + viewTemplateName + "'"); }
ExecutedFragmentExpression fragment = FragmentExpression.createExecutedFragmentExpression(context, fragmentExpression); templateName = FragmentExpression.resolveTemplateName(fragment); markupSelectors = FragmentExpression.resolveFragments(fragment); Map<String, Object> nameFragmentParameters = fragment.getFragmentParameters(); if (nameFragmentParameters != null) { if (fragment.hasSyntheticParameters()) { throw new IllegalArgumentException("Parameters in a view specification must be named (non-synthetic): '" + viewTemplateName + "'"); }
context.setVariables(nameFragmentParameters); } }
String templateContentType = this.getContentType(); Locale templateLocale = this.getLocale(); String templateCharacterEncoding = this.getCharacterEncoding(); Set processMarkupSelectors; if (markupSelectors != null && markupSelectors.size() > 0) { if (markupSelectorsToRender != null && markupSelectorsToRender.size() > 0) { throw new IllegalArgumentException("A markup selector has been specified (" + Arrays.asList(markupSelectors) + ") for a view that was already being executed as a fragment (" + Arrays.asList(markupSelectorsToRender) + "). Only one fragment selection is allowed."); }
processMarkupSelectors = markupSelectors; } else if (markupSelectorsToRender != null && markupSelectorsToRender.size() > 0) { processMarkupSelectors = markupSelectorsToRender; } else { processMarkupSelectors = null; }
response.setLocale(templateLocale); if (!this.getForceContentType()) { String computedContentType = SpringContentTypeUtils.computeViewContentType(request, templateContentType != null ? templateContentType : "text/html;charset=ISO-8859-1", templateCharacterEncoding != null ? Charset.forName(templateCharacterEncoding) : null); response.setContentType(computedContentType); } else { if (templateContentType != null) { response.setContentType(templateContentType); } else { response.setContentType("text/html;charset=ISO-8859-1"); }
if (templateCharacterEncoding != null) { response.setCharacterEncoding(templateCharacterEncoding); } }
viewTemplateEngine.process(templateName, processMarkupSelectors, context, response.getWriter()); } } |