【问题标题】:Send multiple of same object to Controller in Spring MVC在 Spring MVC 中将多个相同对象发送到 Controller
【发布时间】:2021-05-05 15:13:48
【问题描述】:

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

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

这是我找到的最佳答案,但在我的上下文中我似乎无法理解: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>

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

【问题讨论】:

    标签: java spring spring-mvc thymeleaf


    【解决方案1】:

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

    【讨论】:

    • 我只有几个月的信息编程。你能更具体地说明我需要做什么吗?谢谢!
    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 2016-02-24
    相关资源
    最近更新 更多