Spring MVC: how to get case insensitive order from a page

I am trying to maintain case insensitive order in my Spring MVC application when users click on column headers in my web page. When the page is rendered, the Thymeleaf extension creates an anchor and href

a current URL with some parameters supported Pageable

: ie page, size and sort

.

The format sort=propertyName,ASC

works great, but I can't seem to figure out how to tell the sort to be case insensitive with the url. I can do this in code easily enough, but the standard web page support doesn't seem to support it.

After some debugging, it seems that the standard framework org.springframework.data.web.SortHandlerMethodArgumentResolver

simply has no support for org.springframework.data.domain.Sort.Order.ignoreCase

.

I'm a little confused about this and I'm wondering if there is a good reason?

I can think of creating my own class SortHandlerMethodArgumentResolver

and making it parse ASCI|DESCI

(to denote case insensitive) and ASCS|DESCS

(to denote case insensitive) and create an appropriate object, Sort

but that strikes me as quite a lot of work and a serious "code smell".

I can't be the first to stumble upon this. Does anyone have any advice?

+3


source to share


1 answer


I think the only option is to implement your own SortHandlerMethodArgumentResolver. The documentation has a quick guide for this http://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html

To customize this behavior, use SpringDataWebConfiguration or the HATEOAS equivalent and override the pageableResolver () or sortResolver () function and import the customized config file instead of using @ Enable-annotation.

For the format, I would make it a comma separated string of three elements: field name, direction, ignoreCase flag. Something like that:

sort=name,ASC,ignore

      

The last element is optional, so it can have:

sort=name,ASC

      

which would mean ignoreCase is false.



It should also be possible to specify just the field name, for example:

sort=name

      

which would mean the default direction for ASC and ignoreCase is false.

The only problem is if you want to pass the ignoreCase flag, you have to pass the direction, which shouldn't be a big problem I think.

Hope this helps!

Btw here is the JIRA element for this improvement https://jira.spring.io/browse/DATACMNS-658 (Extend the SortHandlerMethodArgument handler to be able to detect the request to be ignored)

+3


source







All Articles