如何在输入标记的动态名称中获取数据

时间:2016-05-27 07:24:09

标签: spring-mvc thymeleaf

我使用thymeleaf,这是我的代码段

<form action="" th:action="@{/soal/save}" method="post">
    <div th:each="soal, number : ${dataModel}">
        <div>
            <p style="font-family: verdana, sans-serif; font-size: 16px;"
               th:text="${number.count + '. ' + soal.teks}"/>
            <input type="hidden" name="idSoal" th:value="${soal.id}"/>
            <div>
                <div style="margin-left: 30px; font-family: verdana, sans-serif; font-size: 14px;">
                    <input th:id="${'pilihan[' + soal.id + 'A]'}" type="radio"
                           th:name="${'pilihan[' + soal.id + ']'}" value="A"/>
                    <label th:for="${'pilihan[' + soal.id + 'A]'}">
                        <span th:text="${'A. ' + soal.PilihanA}"></span>
                    </label> <br/>

                    <input th:id="${'pilihan[' + soal.id + 'B]'}" type="radio"
                           th:name="${'pilihan[' + soal.id + ']'}" value="B"/>
                    <label th:for="${'pilihan[' + soal.id + 'B]'}">
                        <span th:text="${'B. ' + soal.PilihanB}"></span>
                    </label> <br/>

                    <input th:id="${'pilihan[' + soal.id + 'C]'}" type="radio"
                           th:name="${'pilihan[' + soal.id + ']'}" value="C"/>
                    <label th:for="${'pilihan[' + soal.id + 'C]'}">
                        <span th:text="${'C. ' + soal.PilihanC}"></span>
                    </label> <br/>

                    <input th:id="${'pilihan[' + soal.id + 'D]'}" type="radio"
                           th:name="${'pilihan[' + soal.id + ']'}" value="D"/>
                    <label th:for="${'pilihan[' + soal.id + 'D]'}">
                        <span th:text="${'D. ' + soal.PilihanD}"></span>
                    </label> <br/>

                    <input th:id="${'pilihan[' + soal.id + 'E]'}" type="radio"
                           th:name="${'pilihan[' + soal.id + ']'}" value="E"/>
                    <label th:for="${'pilihan[' + soal.id + 'E]'}">
                        <span th:text="${'E. ' + soal.PilihanE}"></span>
                    </label> <br/>

                </div>
                <br/>
            </div>
        </div>
    </div>
    <input type="submit" value="Selsai" class="btn btn-success"/>
</form>

我控制器的代码段:

@RequestMapping(value = "/save" , method = RequestMethod.POST )
public String save(@RequestParam("pilihan") HashMap<String, String> pilihans) {

    System.out.println("Pilihan = " + pilihans);
    for (String pilihan : pilihans.keySet()) {
        System.out.println("Pihan = " + pilihan); 
    }

    return "soal";
}

当我提交此表单时,我有错误:

Required HashMap parameter 'pilihan' is not present

如何捕获输入标签的值?

[解决]

仅使用此代码:

@RequestMapping(value = "/save" , method = RequestMethod.POST )
public String save(@RequestParam Map<String, String> pilihans) {
    // ...
}

1 个答案:

答案 0 :(得分:0)

我在此处将您的解决方案添加为单独的答案:

@RequestMapping(value = "/save" , method = RequestMethod.POST )
public String save(@RequestParam Map<String, String> pilihans) {
    // ...
}