Skip to content
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

Recommended way to go after upgrade to SpringBoot3 - attributes #920

Closed
ottlinger opened this issue Oct 25, 2022 · 2 comments
Closed

Recommended way to go after upgrade to SpringBoot3 - attributes #920

ottlinger opened this issue Oct 25, 2022 · 2 comments
Assignees

Comments

@ottlinger
Copy link

After playing around with the latest SpringBoot 3.0-RC1 I realized my application is not working anymore due to:

Caused by: java.lang.IllegalArgumentException: The 'request','session','servletContext' and 'response' expression utility objects are no longer available by default for template expressions and their use is not recommended. In cases where they are really needed, they should be manually added as context variables.

Is there recommended way to integrate these attributes into the views?
Personally I wouldn't want to integrate all of these things manually in all my controller as I have request-specific highlighting in the navigation bar, which applies to all of my app components.

An example for that is:

                <li class="nav-item"
                    th:classappend="${(#request.getServletPath() == '/' ||  #request.getServletPath() == '') ? 'active' : ''}">
                    <a aria-current="page" class="nav-link" th:href="@{/}"><i class="fas fa-home ml-2"></i> <span
                            th:text="#{menu.home}"></span></a>

I hope that this is the correct project to file this issue - thanks for any advice/hint in advance.

@danielfernandez
Copy link
Member

These objects are not directly available in templates in Thymeleaf 3.1 for security reasons. The recommended way to make this information available to templates is to add the specific pieces of information that are really needed by the template as context variables (model attributes in Spring).

So in your case, the base case would be your controller code adding the servlet path to the model like e.g.:

   model.addAttribute("servletPath", request.getServletPath();

...and then you would use the servletPath context variable normally in your template.

However, you mention that you would prefer not to add this in all your controllers.

In such case, in order to avoid doing this in each of the controller methods of your controller class, you can add the parts of the request you need just once by making use of Spring's @ModelAttribute annotation. Or even if you want to make these parts of the request available to absolutely all controller methods, then you could create a @ControllerAdvice class that registers these @ModelAttribute methods for all controllers in your app.

And if you need several pieces of data from your HttpServletRequest and you don't want these to be declared by multiple @ModelAttribute methods, then you can just group them into a value object with getter methods and create a single @ModelAttribute-annotated method returning that value object.

Hope this helps.

@ottlinger
Copy link
Author

Thanks for the explanation, I'll see how this integrates into my application but I loved the way it was before: very easy to integrate ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants