简体   繁体   English

如何获取地图 <String, String[]> 控制器中的@RequestParam获取数据?

[英]How to get Map<String, String[]> data with @RequestParam in controller?

I have a page with many questions, and they can have multiple answers. 我的页面上有很多问题,他们可以有多个答案。

It's look like this, for every question: 对于每个问题,它看起来都是这样的:

<div th:each="answer: ${question.answers}">
    <input th:type="${question.type == T(test_system.entity.QuestionType).SINGLE ? 'radio' : 'checkbox'}"
           type="radio" th:name="${question.id}" th:value="${answer.id}"/>
    [[${answer.text}]]
</div>

I want to get a Map<String, String[]> in controller, which processes this request. 我想在控制器中获取Map<String, String[]> ,该控制器处理该请求。 (key is id of question, values is answers for this question) (键是问题的ID,值是该问题的答案)

I have this code in controller: 我在控制器中有以下代码:

@RequestMapping(value = "/work/{id}/finish", method = RequestMethod.POST)
public String sendTestAnswers(@PathVariable final long id, @RequestParam final Map<String, String[]> data, final HttpServletRequest request, final Model model) {
    ...
}

But in data I see Map<String, String> , with only one anwer for every question. 但是在数据中我看到Map<String, String> ,每个问题只有一个答案。

Also in request.getParameterMap() I see Map<String, String[]> with all values that I need. 同样在request.getParameterMap()中,我看到了Map<String, String[]>以及所有需要的值。

How can I bind arguments to get prepared object with all data that I need? 如何绑定参数以获取准备好的对象以及所需的所有数据? I don't want to work with HttpServletRequest directly. 我不想直接使用HttpServletRequest。

I think you should give MultiValueMap a try: http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/MultiValueMap.html 我认为您应该尝试MultiValueMap: http : //docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/MultiValueMap.html

It is better suited for these kind of data. 它更适合此类数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Swagger @RequestParam Map进行文档记录<String, String> - How to document with Swagger @RequestParam Map<String, String> Spring RequestParam Map <String,String> - Spring RequestParam Map<String, String> 如何转换@RequestParam地图 <String, String> 到自定义Map &lt;&gt; - How to convert a @RequestParam Map<String, String> to a custom a Map<> 如何为 Map 类型的 RequestParam 指定默认值<string, string> ?</string,> - How to specify default value for RequestParam of type Map<String, String>? Spring MVC 填充@RequestParam Map<string, string></string,> - Spring MVC populate @RequestParam Map<String, String> MultipartFile 不在“@RequestParam Map”中<String, Object> requestParams”变量 - MultipartFile not in “@RequestParam Map<String, Object> requestParams” variable 从控制器类型@RequestParam ArrayList获取JS变量<HashMap<String,String> - Get JS variables from controller type @RequestParam ArrayList<HashMap<String,String> @RequestParam地图 <String, String> 在Spring Boot中将变量名称打印为键 - @RequestParam Map<String, String> Prints variable name as key in spring boot Spring @RequestParam-混合命名的参数和Map <String,String> 参数 - Spring @RequestParam - Mixing named params and Map<String,String> params 如何在Spring MVC Controller的Java Map中使用子域获取JSON字符串 - How to get JSON String with Subdomain in Java Map at Spring MVC Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM