Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc Spring MVC中类映射请求参数的处理_Spring Mvc - Fatal编程技术网

Spring mvc Spring MVC中类映射请求参数的处理

Spring mvc Spring MVC中类映射请求参数的处理,spring-mvc,Spring Mvc,假设我有一个问题列表的表格,其中我需要0/1的答案。我可以很容易地用收音机模拟一个静态列表,比如 <input type="radio" name="question1" value="0"> <input type="radio" name="question1" value="1"> <br> <input type="radio" name="question2" value="0"> <input type="radio" name=

假设我有一个问题列表的表格,其中我需要0/1的答案。我可以很容易地用收音机模拟一个静态列表,比如

<input type="radio" name="question1" value="0">
<input type="radio" name="question1" value="1">
<br>
<input type="radio" name="question2" value="0">
<input type="radio" name="question2" value="1">

@RequestMapping("/answer")
public String answer(Integer question1, Integer question2) {


@请求映射(“/answer”) 公共字符串答案(整数问题1、整数问题2){
但是我有一个动态的问题列表,每个问题都有一个数字ID。因此,我尝试如下建模(HTML是通过问题列表上的迭代动态创建的):



其中42和51是问题id。 我希望捕获Spring控制器映射参数中的所有值,如下所示:

@RequestMapping("/answer")
public String answer(@RequestAttribute("question") HashMap<Integer, Integer> question) {
@RequestMapping(“/answer”)
公共字符串答案(@RequestAttribute(“问题”)哈希映射问题){
它不起作用(未调用该方法)。 我还尝试使用字符串ID:

<input type="radio" name="question['42']" value="0">

@RequestMapping("/answer")
public String answer(@RequestAttribute("question") HashMap<String, Integer> question) {

@请求映射(“/answer”)
公共字符串答案(@RequestAttribute(“问题”)哈希映射问题){
和以前一样。 仅当我使用string/string映射时,它才起作用,但在本例中,我获得映射中的所有请求参数,然后需要解析这些参数:

@RequestMapping("/answer")
public String answer(@RequestAttribute("question") HashMap<String, String> question) {

--> question.keys: "question[42]", "question[51]"
@RequestMapping(“/answer”)
公共字符串答案(@RequestAttribute(“问题”)哈希映射问题){
-->问题关键:“问题[42],“问题[51]”

那么,处理动态无线电的正确方法是什么,或者更一般地说,是像映射一样的请求参数?

我不知道为什么,但如果我将映射放在bean中,它会起作用:

public class QuestionForm {
    private HashMap<Long, String> question;

    public HashMap<Long, String> getQuestion() {
        return question;
    }

    public void setQuestion(HashMap<Long, String> question) {
        this.question = question;
    }
}

@RequestMapping("/answer")
public String answer(QuestionForm questionForm) {
公共类问题表单{
私有哈希映射问题;
公共HashMap getQuestion(){
返回问题;
}
公共无效设置问题(HashMap问题){
这个问题=问题;
}
}
@请求映射(“/answer”)
公共字符串答案(问题表单问题表单){
public class QuestionForm {
    private HashMap<Long, String> question;

    public HashMap<Long, String> getQuestion() {
        return question;
    }

    public void setQuestion(HashMap<Long, String> question) {
        this.question = question;
    }
}

@RequestMapping("/answer")
public String answer(QuestionForm questionForm) {