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

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

本文介绍了使用Spring MVC进行动态表单和数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my Spring MVC application I need to implement a dynamic questionnaire form: I have N questions and for each I have 3 options.

所以在我的页面中我'我会这样:

So in my page I'll have something like this:

|    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 绑定......

Questions are stored in a database and for the options I'll use radio buttons.I'll use a forEach tag to creare dynamic rows, but I don't know how to post data and handle ModelAttribute binding in this scenario...

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

Which could be a good structure for my model attribute class? Is it possible to use binding for a dynamic form with Spring MVC?

推荐答案

你可以做,

i正在考虑问题类如下:

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

选项类如:

public class Option {

    private Integer optionKey;
    private String optionText;

    //getters and setters
}

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

and one QuestionsModel class for form binding like:

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

和Controller类<$ c内$ c> GET 处理程序方法,例如填充问题:

and inside Controller class GET handler method, populate questions for example:

@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 .. 保留旧值,并渲染表单元素,如:

finally in jsp page use <form:hidden.. to keep old values, and render form elements like:

<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中收到绑定和模型,如:

you can receive binding and model in POST like:

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

    return "redirect:/";
}

这篇关于使用Spring MVC进行动态表单和数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:15