使用Spring MVC进行动态表单和数据绑定



在我的Spring MVC应用程序中,我需要实现一个动态问卷表单:我有N个问题,每个问题有3个选项。

所以在我的页面上,我会有这样的东西:

|    Question 1   |   1   |   2   |   3   |
|    Question 2   |   1   |   2   |   3   |
|    Question 3   |   1   |   2   |   3   |
|    ...          |   1   |   2   |   3   |
|    Question N   |   1   |   2   |   3   |

问题存储在数据库中,对于选项,我将使用单选按钮。我将使用forEach标记来创建动态行,但我不知道在这种情况下如何发布数据和处理ModelAttribute绑定。。。

哪一个可能是我的模型属性类的好结构?是否可以使用Spring MVC对动态表单使用绑定?

如何在这种情况下发布数据和处理ModelAttribute绑定

你可以,

我正在考虑Question类,如:

public class Question {
    private String question;
    private Map<Integer,Option> optionMap;
    private Integer selectedOptionKey;
        //getters and setters
}

Option类类似:

public class Option {
    private Integer optionKey;
    private String optionText;
    //getters and setters
}

和一个用于表单绑定的QuestionsModel类,如:

public class QuestionsModel {
    private Map<Integer, Question> questionMap;
    //getters and setters
}

在Controller类GET处理程序方法中,填充问题,例如:

@RequestMapping(method=RequestMethod.GET)
    public String index(Model model){                
    Option optionA = new Option(1, "A");
    Option optionB = new Option(2, "B");
    Option optionC = new Option(3, "C");
    Map<Integer, Option> optionMap = new HashMap<Integer, Option>();
    optionMap.put(optionA.getOptionKey(),optionA);
    optionMap.put(optionB.getOptionKey(),optionB);
    optionMap.put(optionC.getOptionKey(),optionC);
    Question question1 = new Question("A Q", optionMap, 1);
    Question question2 = new Question("B Q", optionMap, 1);
    Question question3 = new Question("C Q", optionMap, 1);
    Map<Integer, Question> questionMap = new HashMap<Integer, Question>();
    questionMap.put(1, question1);
    questionMap.put(2, question2);
    questionMap.put(3, question3);
    model.addAttribute("questionsModel", new QuestionsModel(questionMap));
    return "index";
}

最后,在jsp页面中使用<form:hidden..来保留旧值,并呈现以下表单元素

<c:url value="/questionPost" var="postUrl"/>
<form:form action="${postUrl}" modelAttribute="questionsModel" method="post">
    <table>
    <tr>
        <th>Question</th>
        <th>Options</th>        
    </tr>   
    <c:forEach items="${questionsModel.questionMap}" var="currQue" varStatus="queIndex">
        <tr>
            <td>
                <form:hidden path="questionMap[${queIndex.count}].question"/>
                <label>Question:</label><c:out value="${currQue.value.question}"/><br/>
            </td>
            <td>
            <c:forEach items="${currQue.value.optionMap}" var="opt" varStatus="optionIndex">
                <form:hidden path="questionMap[${queIndex.count}].optionMap[${optionIndex.count}].optionText"/>
                <form:hidden path="questionMap[${queIndex.count}].optionMap[${optionIndex.count}].optionKey"/>
                <form:radiobutton path="questionMap[${queIndex.count}].selectedOptionKey"
                    value="${opt.value.optionKey}" label="${opt.value.optionText}"/>
            </c:forEach>
             </td>
        </tr>
    </c:forEach>
    </table>
    <input type="submit"/>
</form:form>

您可以在POST中接收绑定和模型,如:

@RequestMapping(value="/questionPost", method=RequestMethod.POST)
public String indexPost(@ModelAttribute("questionsModel") QuestionsModel questionModel, BindingResult result){
    System.out.println(questionModel.getQuestionMap());
    return "redirect:/";
} 

此类是我的模型属性:

public class Questionnaire {
    private List<Question> questions = new ArrayList<>();
    private List<Answer> answers = new ArrayList<>();
    // set + get
}

和:

public class Question {
    private int id;
    private String text;
    // set+ get
}
public class Answer {
    private int questionId;
    private int value;
    // set + get
}

在将questions列表放入模型之前,我先填充它。

在我的页面中,我使用以下代码:

<c:forEach items="${questionnaire.questions}" var="question"
    varStatus="gridRow">
    <div>
    ${question.text} 
        <s:bind path="questionnaire.answers[${gridRow.index}].questionID">
            <input type="hidden" name="${status.expression}"
                id="${status.expression}" value="${question.id}" />
        </s:bind>
        <s:bind path="questionnaire.answers[${gridRow.index}].value">
            <sf:radiobuttons path="${status.expression}"
                items="${radio_button_options}" />
        </s:bind>
    </div>
</c:forEach>

发布此表单后,我在控制器中得到了一个完全填充的questionnaire实例。

注意我发现这篇文章对解决我的问题很有帮助

最新更新