Spring MVC: Exposing cookies to the view

Recently I needed to display some information on the web page
according to the value of a cookie. I think that this is one of the
common web application functionalities that have been around for a
decade.
If you have ever used Spring MVC you know that view resolvers could be
instructed to expose request and session attributes as view variables.
It is done through the

<property name="exposeSessionAttributes"><value>true</value></property>
<property name="exposeRequestAttributes"><value>true</value></property>

properties in the appropriate view resolver configuration.
I wonder why there is no similar functionality for cookies, such as:

<property name="exposeCookies"><value>true</value></property>

or something similar.

Currently you have two options to deal with this issue:

  • Find cookies of interest in the controller and add it to the model manually.
  • Deal with the cookies in the view by using JavaScript.

I know that both of these ways are perfectly valid and will do the
trick for you, but this is one of the fundamental web features (and so
easy to implement) that I wonder how no one asked for it before. I
googled for it and found nothing. Or am I just missing something?

Leave a comment

Your email address will not be published. Required fields are marked *