Spring MVC:如何从 Pageable 获取不区分大小写的排序 [英] Spring MVC: how to get case-insensitive ordering from Pageable

查看:23
本文介绍了Spring MVC:如何从 Pageable 获取不区分大小写的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击我网页上的列标题时,我试图在我的 Spring MVC 应用程序中支持不区分大小写的排序.当页面呈现时,Thymeleaf 扩展会创建一个锚点,href 是当前 URL,其中包含 Pageable 支持的一些参数:即 page、size 和 sort.

I am trying to support case-insensitive ordering in my Spring MVC app when users click on the column headings on my web page. When the page is rendered a Thymeleaf extension creates an anchor and the href is the current URL with some parameters supported by Pageable: i.e. page, size and sort.

sort=propertyName,ASC 格式工作正常,但我无法从 URL 中找出如何说排序不区分大小写.我可以很容易地在代码中完成它,但标准的 Pageable 支持似乎不支持它.

The sort=propertyName,ASC format works fine, but I can't find out how to say that the sort should be case-insensitive from the URL. I can do it in code easily enough but the standard Pageable support doesn't seem to support it.

经过一些调试,似乎标准框架 org.springframework.data.web.SortHandlerMethodArgumentResolverorg.springframework.data.domain.Sort.Order 没有任何支持.ignoreCase.

After some debugging it appears that the standard framework org.springframework.data.web.SortHandlerMethodArgumentResolver just doesn't have any support for org.springframework.data.domain.Sort.Order.ignoreCase.

我对此有些困惑,想知道是否有充分的理由?

I'm somewhat bemused about this, and am wondering if there's a good reason why?

我可以考虑创建自己的 SortHandlerMethodArgumentResolver 类,并使其解析 ASCI|DESCI(表示不区分大小写)和 ASCS|DESCS(表示区分大小写)并生成适当的 Sort 对象,但这让我觉得工作量很大,而且有严重的代码味道".

I can look into creating my own SortHandlerMethodArgumentResolver class, and make it parse ASCI|DESCI (to mean case-insensitive), and ASCS|DESCS (to mean case-sensitive) and produce the appropriate Sort object, but this strikes me as quite a bit of work and a serious "code smell".

我不可能是第一个发现这个问题的人.有人有什么建议吗?

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

推荐答案

我认为唯一的选择是实现您的自定义 SortHandlerMethodArgumentResolver.文档对此有简要指南 http://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html

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

要自定义此行为,请扩展 SpringDataWebConfiguration 或启用 HATEOAS 的等效项并覆盖 pageableResolver() 或sortResolver() 方法并导入您的自定义配置文件而不是使用@Enable-annotation.

To customize this behavior extend either SpringDataWebConfiguration or the HATEOAS-enabled equivalent and override the pageableResolver() or sortResolver() methods and import your customized configuration file instead of using the @Enable-annotation.

对于格式,我会将其设为由 3 个元素组成的逗号分隔字符串:字段名称、方向、ignoreCase 标志.像这样:

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

sort=name,ASC,ignore

最后一个元素是可选的,所以可以有:

The last element is optional so it's possible to have:

sort=name,ASC

这意味着 ignoreCase 是假的.

which would mean that ignoreCase is false.

也应该可以只指定字段名称,例如:

Also it should be possible to specify only field name like:

sort=name

这意味着 ASC 和 ignoreCase 的默认方向为 false.

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

唯一的问题是如果你想传递 ignoreCase 标志,你必须传递方向,我认为这应该不是一个大问题.

The only issue is if you want to pass ignoreCase flag you must pass the direction which should not be a big problem I think.

希望这会有所帮助!

顺便说一句,这里有一个用于此改进的 JIRA 项目 https://jira.spring.io/browse/DATACMNS-658(扩展 SortHandlerMethodArgument 解析器以能够检测忽略大小写的请求)

Btw here is a JIRA item for this improvement https://jira.spring.io/browse/DATACMNS-658 (Extend SortHandlerMethodArgument resolver to be able to detect the request for ignore-case)

这篇关于Spring MVC:如何从 Pageable 获取不区分大小写的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆