我正在尝试提供反馈,但我无法获得有关如何将多维int数组从表单发送到控制器的任何示例。
来自图片的反馈:http://oi58.tinypic.com/p1z10.jpg
控制器
@RequestMapping(value = "/submitfeedback", method = RequestMethod.POST)
public String postsubmitfeedback(@ModelAttribute("answer") @RequestParam("email") String email,
@RequestParam("feedback_id") Integer feedback_id, @RequestParam(value="myanswer[]") int [] myanswer, Answer answer,
Locale locale) {
for(int i=0; i<myanswer.length;i++) {
System.out.println(myanswer[i]);
}
return "submitfeedback";
}
Jsp表格
<form:form commandName="feedback">
<c:forEach items="${questionList}" var="question">
<c:set var="counter" value="${counter + 1}"/>
<tr>
<td>${counter}</td>
<td>${question.question}</td>
<td><input type="radio" name="myanswer[${count}]" id="radio" value="1" /> 1</td>
<td><input type="radio" name="myanswer[${count}]" id="radio" value="2" /> 2</td>
<td><input type="radio" name="myanswer[${count}]" id="radio" value="3" /> 3</td>
<td><input type="radio" name="myanswer[${count}]" id="radio" value="4" /> 4</td>
<td><input type="radio" name="myanswer[${count}]" id="radio" value="5" /> 5</td>
</tr>
<c:set var="count" value="${count+1}"/>
</c:forEach>
</form:form>
答案 0 :(得分:0)
首先,我强烈建议您将参数封装在对象中。
class FeedbackForm {
private String email;
private Integer feedbackId;
private Integer answer;
/** Getters and setters. */
}
html表单输入的名称应与字段名称匹配。
其次,你有一个单选按钮控件,我怀疑你最终会得到一个整数列,因为你总是只能选择一个单选按钮。在上面的bean中,我已将其更正为单个答案。单选按钮有点奇怪,因为它们是表单内的多个输入。你应该有这样的事情:
<input type="radio" name="answer" value="1" onClick="changeMyHiddenField()" />1
<input type="radio" name="answer" value="2" onClick="changeMyHiddenField()" />2
<input type="radio" name="answer" value="3" onClick="changeMyHiddenField()" />3
<form {...}> <input id="myHiddenField" type="hidden" name="answer" /> </form>
第三,您没有验证客户的输入。你应该利用Spring的bean验证。
@Min(value = 1, message = "Min is 1.")
@Max(value = 5, message = "Max is 5.")
private Integer answer;
最后,您的方法签名应如下所示:
public String postFeedback(@ModelAttribute @Valid FeedbackForm form, HttpServletRequest request, Locale locale);