java - 如何分配 map 的键和值 - 使用 Spring 的 JSP 表单中 modelAttribute 对象的属性?

标签 java spring forms jsp spring-mvc

我在 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: 中,我从 Controller getExam 方法中获取问题属性。我正在为每个问题创建单选按钮组并填写模型属性“answerSheetWrapper”(也许我做的不正确,所以请告诉我该怎么做)。 我希望 map “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>

这是我的 Controller 方法,其中发生表单操作:

@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

那么我的代码哪里出了问题呢?感谢您的帮助。

最佳答案

您似乎忘记了将 answerSheetWrapper 添加到模型中。目前您只添加了问题,因此请像这样修改您的 Controller 代码:

map.addAttribute("questions", questions);
map.addAttribute("answerSheetWrapper", new AnswerSheetWrapper());

还有这个

<form:radiobutton path="answerSheet['${question.id}']"
                  value="${variant.id}"/>${variant.text}

最好使用 form:radiobuttonlabel 属性编写

<form:radiobutton path="answerSheet['${question.id}']"
                  value="${variant.id}" label="${variant.text}" />

关于java - 如何分配 map 的键和值 - 使用 Spring 的 JSP 表单中 modelAttribute 对象的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27676318/

相关文章:

java - Web应用程序和桌面之间的通信,有什么更好的方法?

java - Flyway使用java类对sql文件进行排序

forms - 浏览器如何决定哪些表单字段是用户名/密码?

java - 数字格式和输入不匹配异常有何不同

java - Maven 中的 SOAPUI 测试报告

spring - 通过 XML 配置时, Autowiring 不起作用

java - 在JSP中获取Hibernate初始化对象

forms - 使用 KnpPaginatorBundle 对 Symfony 表单集合进行分页

php - 如何通过表单标签将数据发送到另一个页面?

java - 在 x64 Windows 上使用 x32 JDK 在 x64 Eclipse 上针对 x32 服务器进行编译