java - 在 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>
当他们参加测验时,我最终会在收集用户答案时遇到类似的问题。
解决方案
您需要使用js
来处理多个问题,以便在请求中将有请求正文中的问题列表。
推荐阅读
- azure-devops - 将值从一个任务输出传递到另一任务
- python - TensorFlow 对象检测 API 和图像大小
- maple - Maple 无法评估整个绘图范围内的功能
- http - 当服务器不可用时,如何从 http.dart 库获得更准确的错误消息?
- javascript - Slick 滑块错误:TypeError: o[i].slick is undefined
- javascript - 选择元素单击时的 JQuery 单击事件在 chrome 中不起作用(在 windows 环境中不会出现问题)
- python - 如何为 C 代码创建 python 接口?
- lua - LUA:如何从字符串创建二维数组/表
- python - 获取训练集标签的问题
- typescript - 如何从firestore获取当前用户数据?