【发布时间】:2014-12-28 12:21:04
【问题描述】:
我在 Spring 中有一个问卷调查网络应用程序。这是获取带有问题和答案选项的表单并将其返回到exam.jsp的代码:
@RequestMapping(value = "/")
public String getExam(ModelMap map) {
List<Question> questions = new ArrayList<Question>();
// Here I am getting the all data for questions list
// and basically I am sending to the view the list of questions, where each question has list of variants
map.addAttribute("questions", questions);
return "exam";
}
我的模型类:
public class Question {
private int id;
private String text;
private List<Variant> variants;
//getters and setters
}
public class Variant {
private int id;
private int questionId;
private int correctness;
private String text;
//getters and setters
}
public class AnswerSheetWrapper {
Map<Integer, Integer> answerSheet;
//getter and setter
}
在我的 exam.jsp: 中,我从控制器 getExam 方法中获取问题属性。我正在为每个问题创建单选按钮组并填写 modelattribute 'answerSheetWrapper' (也许我做的不正确,所以请告诉我该怎么做)。 我希望地图 'answerSheet' 将问题 ID 作为键和变量 ID 作为值:
<form:form action="/exam/calculate" modelAttribute="answerSheetWrapper">
<c:forEach items="${questions}" var="question">
${question.text}<br />
<c:forEach items="${question.variants}" var="variant">
<form:radiobutton path="answerSheet['${question.id}']" value="${variant.id}"/>${variant.text} <!--Here code throws Exception when runned-->
</c:forEach>
<br />
</c:forEach>
<input type="submit" value="Göndər"/>
</form:form>
这是发生表单操作的控制器方法:
@RequestMapping(value = "/exam/calculate")
public String calculate(@ModelAttribute("answerSheetWrapper")AnswerSheetWrapper answerSheetWrapper) {
// do processing with modelAttribute object
return "someView";
}
我不确定我在 form:radiobutton 中指定的路径是否正确。
当我运行我收到的应用程序时:
HTTP Status 500 - An exception occurred processing JSP page /resources/pages/exam.jsp at line 29
根本原因是:
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'answerSheetWrapper' available as request attribute
那么我的代码中的问题在哪里?任何帮助表示赞赏。
【问题讨论】:
标签: java spring forms jsp spring-mvc