I just upgraded my app to Spring Boot 3.0.5 and there seems to be something broken with spring-boot-starter-freemarker.
In my Freemarker template, I have this line that used to work before the upgrade:
<div>${RequestParameters.myParam!}</div>
After the upgrade, I get this error when loading the template:
freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> RequestParameters [in template "template.ftlh" at line 136, column 11]
----
Tip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----
----
FTL stack trace ("~" means nesting-related):
- Failed at: ${RequestParameters.myParam!} [in template "template.ftlh" at line 136, column 9]
It seems like RequestParameters is not available anymore.
Is there a fix in sight for this? Any workarounds?
Thanks!
Comment From: wilkinsona
Which version of Spring Boot were you using before the upgrade?
As far as I can remember, we haven't made many, if any, recent changes to our FreeMarker support. Once possibility is this change in Spring Framework.
Comment From: anabright
I upgraded from 2.7.10. If I go back to it, it works again.
Not sure if the issue you linked is the cause, it doesn't seem to mention any changes to the availability of RequestParameters
Comment From: wilkinsona
Sorry, I should have linked more precisely. I meant the change that I described in https://github.com/spring-projects/spring-framework/issues/29787#issuecomment-1375508371. Due to https://github.com/spring-projects/spring-framework/commit/d84ca2ba90d27a7c63d7b35a6259b5b9cf341118, there's no longer a RequestParameters entry in the model. Can you please open a Spring Framework issue and comment here with a link to it?
Comment From: startjava
use spring-boot 3.1.2
use url: http://localhost:8080/test20?selectedValue=d
template code:
controller code: @RequestMapping("test20") public String test20(Model model,HttpServletRequest request,HttpServletResponse response)
{ List listString=new ArrayList(); listString.add("a"); listString.add("b"); listString.add("c"); listString.add("d"); listString.add("e"); request.setAttribute("listString",listString); return "test20"; }
run result : freemarker.core.InvalidReferenceException: The following has evaluated to null or missing: ==> RequestParameters [in template "test20.ftlh" at line 12, column 12]
Tip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
FTL stack trace ("~" means nesting-related): - Failed at: #if RequestParameters.selectedValue =...
[in template "test20.ftlh" at line 12, column 7]
@wilkinsona same question!
Comment From: startjava
@anabright
Comment From: startjava
https://github.com/spring-projects/spring-framework/issues/30186
Comment From: wilkinsona
@startjava please stop making the same comments across multiple issues
Comment From: doctore
I had this problem upgrading an old project from Spring 3 to Spring 6, by now we have no time to replace the technology used to render the views, so my solution was adding it as a new model attribute of FreeMarker.
Replacing:
<#if RequestParameters['authfail']??>
...
</#if>
By, in backend:
@ModelAttribute("rawRequestParameters")
public String getRequestParameters(HttpServletRequest request) {
return ofNullable(request)
.map(HttpServletRequest::getQueryString)
.orElse("");
}
and in frontend:
<#if rawRequestParameters?? && rawRequestParameters == "authfail">
...
</#if>