[SOLVED] Request parameter and Model attribute with the same name

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[SOLVED] Request parameter and Model attribute with the same name

This post was updated on .
Hey, guys,

I'm using Thymeleaf 3.0.9.RELEASE along with Spring 5.0.2.BUILD-SNAPSHOT.

I have the following weird problem...

In my controller, I'm adding a Page model attribute which contains information about my page:
public class SomeController {

    protected void populateModel(final Model model, final Page<?> page) {
        model.addAttribute("page", page);

Then in my application, I try to access:

In my view, when I try to access some property of the "page" model attribute, I get errors of this type:
"Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'totalElements' cannot be found on object of type 'java.lang.String' - maybe not public"

And if I try to print out the "page" variable like so:
<div data-th-text=${page}></div>
I only see the string "1" printed out.

This lead me to believe that somehow Thymeleaf is taking the value of the request parameter instead of the value of the model attribute. I tested this further with adding a new line in my controller, saying:
model.addAttribute("someDifferentName", page);
Then I can actually access the Page object in my view by referring to the "someDifferentName" model attribute.

I thought that request parameters are accessed with the ${param.*} syntax (as seen in the documentation: http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html).

Am I doing something wrong? Is this expected behaviour or is it a bug in Thymeleaf 3?


Turns out there was some code somewhere in my application which was setting all request parameters as model attributes. After removing that logic, everything works as expected. For more information, see https://github.com/thymeleaf/thymeleaf/issues/665