【问题标题】:How to make @RequestParam configurable through .properties file?如何通过 .properties 文件使 @RequestParam 可配置?
【发布时间】:2015-12-11 19:08:38
【问题描述】:

我们如何通过 .properties 文件进行以下配置?

@RequestParam(value = "page", required = false, defaultValue="0") Integer page,
@RequestParam(value = "size", required = false,defaultValue="8") Integer size,

如何通过 Spring MVC 中的 .properties 文件使 defaultValue="0"defaultValue="8" 可配置?还有我们如何在@PageableDefault(size = 8, page = 0) 中做到这一点?

【问题讨论】:

    标签: spring-mvc spring-security spring-data-mongodb


    【解决方案1】:

    @RequestParam 支持 ${x.y} 等占位符语法,请查看documentation

    1. 在属性文件中将所需属性定义为 application.properties request.defaultPageValue=0
    2. 在您的 servlet xml 配置中加载属性文件。

      <context:property-placeholder ignore-unresolvable="true" location="/WEB-INF/application.properties"/>

    3. 使用 requestParam 中的属性

      @RequestParam(value = "page", required = false, defaultValue="${request.defaultPageValue}") Integer page,

    为了在Pageable中设置默认值,同样的想法使用placeHolder语法来加载整数,检查相关SO question

    【讨论】:

    • 它完全适合我 RequestParam,未测试 Pageable,哪个不适合您?
    • 对我来说没有用 public final int pageIndex = Integer.parseInt(Properties.getProperty("pagination.page.index")); public final int pageSize = Integer.parseInt(Properties.getProperty(".pagination.page.size")); @PageableDefault(size = pageIndex, page = pageSize) 可分页可分页
    猜你喜欢
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 2011-02-19
    • 2017-09-16
    • 2010-12-11
    • 1970-01-01
    • 2019-11-15
    • 2020-07-13
    相关资源
    最近更新 更多