Quantcast

Reserved words in Thymeleaf?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Reserved words in Thymeleaf?

HersheySquirt
After upgrading to thymeleaf 3 I'm seeing this error when thymeleaf attempts to parse an existing template that worked in earlier versions.

SEVERE: Servlet.service() for servlet [SpringDispatcherServlet] in context with path [/some-path] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "ServletContext resource [/WEB-INF/templa[...]/applicationDetails.html]")] with root cause
java.lang.IllegalArgumentException: Cannot set variable called 'application' into web variables map: such name is a reserved word

The code that throws the exception is in WebEngineContext.  I don't even see where we're setting a variable called 'application' but we are setting one called 'app'.  Why would application even be a reserved word in thymeleaf?  I didn't see any gotchas in the upgrade doc about any of this.

public void setVariables(final Map<String, Object> variables) {
        if (variables == null || variables.isEmpty()) {
            return;
        }
        // First perform reserved word check on every variable name to be inserted
        for (final String name : variables.keySet()) {
            if (SESSION_VARIABLE_NAME.equals(name) ||
                    PARAM_VARIABLE_NAME.equals(name) ||
                    APPLICATION_VARIABLE_NAME.equals(name)) {
                throw new IllegalArgumentException(
                        "Cannot set variable called '" + name + "' into web variables map: such name is a reserved word");
            }
        }
        this.requestAttributesVariablesMap.setVariables(variables);
    }
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reserved words in Thymeleaf?

HersheySquirt
I figured out what the problem was with this.  I had a fragment that took an argument called 'application' which broke in thymeleaf 3.0.  When I changed it to 'app' it worked fine.  Hopefully this will help others who run into the same issue.  Be careful what you put in your 'method signature'.

For example, this th:fragment declaration worked fine in thymeleaf 2.x:
<div th:fragment="applicationBreadcrumb2(application)">

In thymeleaf 3.0 I needed to rename the param variable to get it to parse correctly and avoid the reserved word problem:
<div th:fragment="applicationBreadcrumb2(app)">
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reserved words in Thymeleaf?

mrusso
In reply to this post by HersheySquirt
I ran into this exact same issue. I didn't see any upgrade notes about new reserved words, either. My fix was the same as yours -- just rename "application" to "app".
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reserved words in Thymeleaf?

ThomasLangTHD
Same problem here.
Mine was about the location of views:
I wanted to render views residing in \src\main\resources\templates\application.

An exception occured whilst trying rendering. Renaming the view location to app did the job.
Thank you for your tip!

By the way ... are there any resources available where reserved words are documented?
Loading...