Search code examples
springspring-mvcthymeleafspring-el

Is it possible to pass NULL as argument in ${...} expression?


I am working with Spring and Thymeleaf. I need to remove one parameter from current url and I am doing something like this in view:

urlBuilder=${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder).fromCurrentRequest()},
currentUrl=${urlBuilder.replaceQueryParam('page',null).build()

However, that null value is not null actually. If I debug that line, value of that parameter is not null but an empty object, here is the screenshot:

enter image description here

Is there any way to pass real null from SpEL?

Behavior of builder component is slightly different in both cases, as when you pass null, then query param will be removed. In given example, query param stays on the URL.


Solution

  • You are calling a varargs method. The null is wrapped into an array with one null element (which is not considered empty). The solution is to omit the null parameter:

    urlBuilder.replaceQueryParam('page')