【问题标题】:How to assign keys and values of the map - the property of a modelAttribute object in JSP forms using Spring?如何分配映射的键和值 - 使用 Spring 的 JSP 表单中的 modelAttribute 对象的属性?
【发布时间】: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


    【解决方案1】:

    您似乎忘记将answerSheetWrapper 添加到模型中。目前您只添加问题,因此修改控制器代码如下:

    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}" />
    

    【讨论】:

      猜你喜欢
      • 2012-04-01
      • 2017-05-22
      • 2015-05-17
      • 2022-06-14
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 2021-12-15
      相关资源
      最近更新 更多