首页 > 解决方案 > 在 Spring MVC 中将多个相同的对象发送到 Controller

问题描述

我正在开发一个测验应用程序。我希望用户能够创建一个包含 10 个问题的测验(尽管我对可变长度持开放态度)。如果我使用@ModelAttribute 创建超过 1 个问题,而不是获得超过 1 个 QuestionAnswerInfo 对象,我会得到一个每个字段用逗号分隔的对象。这似乎不是一个列表,而只是用逗号分隔的字符串。

我希望每个问题都进来并单独处理。解决这个问题的最佳方法是什么?

这是我找到的最佳答案,但在我的上下文中我似乎无法理解它:Send multiple objects of same class from jsp to spring controller

模型(抽象实体只是 ID 生成)

@Entity
public class Quiz extends AbstractEntity {

    public String name;

    @OneToMany (cascade = CascadeType.ALL)
    @JoinColumn(name = "quiz_question_foreign_id", referencedColumnName = "id")
    private List<QuestionAnswerInfo> questions = new ArrayList<>();

    public Quiz(){}

    public Quiz(String name, ArrayList<QuestionAnswerInfo> questions)  {
        super();
        this.name = name;
        this.questions = questions;
    }
//Getters and Setters

}

@Entity
public class QuestionAnswerInfo extends AbstractEntity{

    private String question;
    private String answer;
    private String questionType;
    private String additionalAnswerInfo;

    public QuestionAnswerInfo (){}

    public QuestionAnswerInfo(String question, String answer, String questionType, String additionalAnswerInfo) {
        super();
        this.question = question;
        this.answer = answer;
        this.questionType = questionType;
        this.additionalAnswerInfo = additionalAnswerInfo;
    }

//Getters and Setters

控制器

@Controller
public class QuizController {

    //Repositories

    @RequestMapping("create")
    public String displayCreateNewQuiz(Model model) {
        model.addAttribute(new Quiz());
        model.addAttribute("questions", new QuestionAnswerInfo());
        return "create";
    }

    @PostMapping("create")
    public String processCreateNewQuiz(@ModelAttribute Quiz newQuiz, @ModelAttribute QuestionAnswerInfo questions,
                                       Model model) {
        newQuiz.getQuestions().add(questions);
        quizRepository.save(newQuiz);
        return "index";
    }

看法

<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
<h1>Create a Quiz</h1>

<form method="post">
    <div>
        <label th:for="name">Quiz Name</label>
        <input th:field="${quiz.name}"/>
    </div>
    <br>
    <p>Question 1</p>
    <div>
        <label th:for="question">Add a Question</label>
        <input th:field="${questions.question}"/>
    </div>
    <div>
        <label th:for="answer">Add an Answer</label>
        <input th:field="${questions.answer}"/>
    </div>
    <div>
        <label th:for="questionType">Question Type</label>
        <input th:field="${questions.questionType}"/>
    </div>
    <div>
        <label th:for="additionalAnswerInfo">Add Additional Information</label>
        <input th:field="${questions.additionalAnswerInfo}"/>
    </div>

    <p>Question 2</p>
    <div>
        <label th:for="question">Add a Question</label>
        <input th:field="${questions.question}"/>
    </div>
    <div>
        <label th:for="answer">Add an Answer</label>
        <input th:field="${questions.answer}"/>
    </div>
    <div>
        <label th:for="questionType">Question Type</label>
        <input th:field="${questions.questionType}"/>
    </div>
    <div>
        <label th:for="additionalAnswerInfo">Add Additional Information</label>
        <input th:field="${questions.additionalAnswerInfo}"/>
    </div>



    <input type="submit" value="Create Quiz"/>
</form>

</body>
</html>

当他们参加测验时,我最终会在收集用户答案时遇到类似的问题。

标签: javaspringspring-mvcthymeleaf

解决方案


您需要使用js来处理多个问题,以便在请求中将有请求正文中的问题列表。


推荐阅读