New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle MapAccessor exception when attribute does not exist and render blank #315
Comments
Any chance to get some feedback on this issue @danielfernandez ? |
I understand the difference in behaviour can be distracting, but it is consistent with the two different access methods being used. In the case of However, The different context-related structures ( |
Thanks for the reply! In my example, I agree that if |
Ah, good. Sorry I didn't notice |
Great, thanks! |
…quest param or servletContext attribute from SpringEL. Related to #85
After evaluation, we won't be adding any This is the way Spring's Besides, Thymeleaf 3.0 has made an effort for reducing the amount of times a custom Closing this therefore as declined. |
With version 2.1.3.RELEASE, when trying to output a map attribute that does not exist via the
MapAccessor
implementation, Thymeleaf currently throws an exception and the rendering fails. It would be nice if it could instead handle the exception and render the attribute value as an empty string / blank.After all, when calling
Map.get("keyThatDoesNotExist")
the map does not throw an exception. It simply returnsnull
. And I think it makes sense thatnull
should be interpreted by Thymeleaf to be rendered blank. At least it seems that's what is done in other places.Following is a simple example of the issue and the current workaround I'm using.
I have the following user map:
Now, in my template, I would like to do the following:
This, however, will fail with an exception such as:
Now, if I change the expression to
${user['gender']}
, it doesn't throw an exception and simply renders the attribute as empty (because it doesn't use the MapAccessor implementation I believe), which is what I would like to happen. So the below in my template works nicely.I would, however, much rather use the dot-syntax and don't quite understand why the two different formats should behave differently. It seems like an unnecessary detail to be aware of and the dot-syntax is probably far more popular than the brackets.
I realize that Thymeleaf might not be able to prevent the exception from being thrown if a Spring implementation is used behind the scenes, but it should be able to catch and ignore the exception when appropriate.
The text was updated successfully, but these errors were encountered: