Thymeleaf template injection of spring boot

reference:

Insert picture description here

Insert picture description here

The scene is when the name of the view is user-controllable,

The actual test found that it is related to the version of Spring boot, which is related to the default Thymeleaf version.

spring boot:1.5.1.RELEASE spring-boot-starter-thymeleaf:2.1.5
spring boot:2.0.0.RELEASE spring-boot-starter-thymeleaf:3.0.9
spring boot:2.2.0.RELEASE spring-boot-starter-thymeleaf:3.0.11

The 3.x version of thymeleaf is only affected.


The thymeleaf-spring5 of renderFragment 3.x version is like this:

            if (!viewTemplateName.contains("::")) {
    
    
                templateName = viewTemplateName;
                markupSelectors = null;
            } else {
    
    
                IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);

                FragmentExpression fragmentExpression;
                try {
    
    
                    fragmentExpression = (FragmentExpression)parser.parseExpression(context, "~{" + viewTemplateName + "}");
                } catch (TemplateProcessingException var25) {
    
    
                    throw new IllegalArgumentException("Invalid template name specification: '" + viewTemplateName + "'");
                }

And 2.x thymeleaf-spring4 is like this

                Configuration configuration = viewTemplateEngine.getConfiguration();
                ProcessingContext processingContext = new ProcessingContext(context);
                templateCharacterEncoding = getStandardDialectPrefix(configuration);
                StandardFragment fragment = StandardFragmentProcessor.computeStandardFragmentSpec(configuration, processingContext, viewTemplateName, templateCharacterEncoding, "fragment");
                if (fragment == null) {
    
    
                    throw new IllegalArgumentException("Invalid template name specification: '" + viewTemplateName + "'");
                }

Guess you like

Origin blog.csdn.net/caiqiiqi/article/details/108446723