java - Spring从Controller中的URL获取所有参数

标签 java spring spring-mvc

我在前端有一个表单,用户可以在其中从月份列表中选择年份和一个或多个月份。这些参数将作为 Get 方法发送到 Controller 。 给出这样的 URL:

..../{year}/{months}/excel/

其中 months 是所选月份的变量列表,即 [01,02,10]。

我如何在 Controller 中接收所有参数? 到目前为止,这是我的 Controller :

    @RequestMapping(value = "/{year}/{months}/excel/", method = RequestMethod.GET, produces = EXCEL_FORMAT_HEADER)
    public @ResponseBody
    ModelAndView getRankingByYearExcel(@PathVariable("year") Integer year,
                                       @RequestParam Map<String, Object> months)
 {...}

最佳答案

我这样做并工作,将 months 声明为字符串数组:

@RequestMapping(value = "/{year}/excel/", method = RequestMethod.GET, produces = EXCEL_FORMAT_HEADER)
public @ResponseBody
ModelAndView getRankingByYearExcel(@PathVariable("year") Integer year,
                                   @RequestParam String[] months)

并在 URL 中以字符串数组的形式发送可变月份:

../2016/excel/?months=1,3,12

谢谢你指引我朝这个方向前进

关于java - Spring从Controller中的URL获取所有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51065058/

相关文章:

java - Spring Boot 和 Hibernate : Start an application even if a connection to the database is not available

java - 为什么 Controller 中的 @RequestMapping Spring 注释捕获了我想要的更多内容?

待分发的 Java TCP/IP 应用程序

java - 使用 spring-security-saml 在应用程序中没有配置 IDP 错误

java - 如何使用简单的 for 循环创建数组?

java - Spring 安全: make a mysql authentication

java - 如何在 Spring 5 中使用自定义 'TransactionAttributeSource'

java - Spring MVC/Hibernate/mySQL "read"始终为空

java - Spring 如何从返回 CompletableFuture 对象的端点获取结果?

java - 错误 :(175) Attribute "adSize" already defined with incompatible format.(Admob)