forms - 重定向到页面一定次数 - Spring Boot
问题描述
我正在尝试制作测验创建应用程序。一开始,我要求用户输入测验的标题、描述和包含在测验中的问题数量。根据我想将用户重定向到“问题和答案”页面的问题数量。我想添加另一个名为“count”的变量,它会保留页面被访问的次数,这样我就可以显示下一个或提交按钮。
我不确定如何计算页面被重定向的次数以及如何根据问题的数量将代码重定向到某个页面。
这是 QuizController 类中的 saveQuiz 方法:
@PostMapping("/saveQuiz/{cid}")
public String saveQuiz(@PathVariable("cid") Long cid, @Valid @ModelAttribute Quiz quiz,
Model model, @RequestParam("questionNumber") int noOfQuestions) throws ParseException {
Chapter chapter = chapterService.findChapterById(cid);
quiz.setQuizName(quiz.getQuizName());
quiz.setGuidelines(quiz.getGuidelines());
quiz.setChapter(chapter);
quizService.saveQuiz(quiz);
model.addAttribute("quiz", quiz);
model.addAttribute("noOfQuestions", noOfQuestions);
return "redirect:/add_quiz_questions/"+quiz.getId();
}
然后在我的 QuestionController 类中,我有以下方法
@Controller
public class QuestionController {
@Autowired
QuizService quizService;
@Autowired
QuestionService questionService;
@Autowired
AnswerService answerService;
private static int count = 0;
@GetMapping("/add_quiz_questions/{qid}")
public String addQuestions(@PathVariable("qid") Long qid, Model model) {
count++;
Quiz quiz = quizService.findQuizById(qid);
model.addAttribute("quiz", quiz);
model.addAttribute("count", count);
return "add_quiz_questions";
}
@PostMapping("/saveQuizQuestion/{qid}")
public String saveQuestions(@PathVariable("qid") Long qid, @Valid @ModelAttribute QuizForm quizForm,
Model model, @RequestParam("noOfQuestions") int noOfQuestions) throws ParseException {
Quiz quiz = quizService.findQuizById(qid);
Question question = new Question();
question.setQuestion(quizForm.getQuestion());
//Add answers
Set<Answer> answers = new HashSet<>();
Answer a = new Answer();
a.setAnswer(quizForm.getOption1());
a.setCorrect(1);
answers.add(a);
a.setAnswer(quizForm.getOption2());
a.setCorrect(0);
answers.add(a);
a.setAnswer(quizForm.getOption3());
a.setCorrect(0);
answers.add(a);
answerService.saveAnswers(answers);
question.setAnswers(answers);
questionService.saveQuestion(question);
Chapter chapter = quiz.getChapter();
Course course = chapter.getCourse();
Set<File> files = chapter.getFiles();
int nrFiles = files.size();
model.addAttribute("chapter", chapter);
model.addAttribute("course", course);
model.addAttribute("files", files);
model.addAttribute("numberOfFiles", nrFiles);
model.addAttribute("quiz", quiz);
if(count == noOfQuestions) //check if the page has been redirected as many times as there were questions then redirect to chapter page
return "redirect:/chapter_details/"+chapter.getId();
else
return "redirect:/add_quiz_questions/"+quiz.getId();
}
}
这是 Thymeleaf 页面:
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<div class="card">
<h5 class="card-header info-color white-text text-center py-4">
<strong>Quiz questions</strong>
</h5>
<div class="card-body px-lg-5">
<!-- Form -->
<form class="text-center" style="color: #757575;" th:action="@{/saveQuizQuestion/{qid}(qid=${quiz.id})}" method="post" th:object="${quizForm}">
<p>Create your quiz</p>
<!-- Question -->
<div class="md-form mt-3">
<input type="text" id="question" class="form-control" name="question">
<label for="question">Question</label>
</div>
<!-- Right answer -->
<div class="md-form">
<input type="text" id="ans1" class="form-control" name="option1">
<label for="ans1">Answer 1</label>
</div>
<!-- Answer 2 -->
<div class="md-form">
<input type="text" id="ans2" class="form-control" name="option2">
<label for="ans2">Answer 2</label>
</div>
<!-- Answer 3 -->
<div class="md-form">
<input type="text" id="ans3" class="form-control" name="option3">
<label for="ans3">Answer 3</label>
</div>
<input type="hidden" th:value="${count}" name="count"/>
<input type="hidden" th:value="${noOfQuestions}" name="noOfQuestions"/>
<button th:if="${noOfQuestions < count}" class="btn btn-outline-info btn-rounded btn-block z-depth-0 my-4 waves-effect" type="submit">Next</button>
<button th:if="${noOfQuestions == count}" class="btn btn-outline-info btn-rounded btn-block z-depth-0 my-4 waves-effect" type="submit">Submit</button>
</form>
<!-- Form -->
</div>
</div>
</html>
我相信我使用count变量的方式是错误的,但它只是为了给出一个想法。如果有人可以帮助我澄清我的问题,我将不胜感激。先感谢您。
解决方案
您可以使用 @SessionAttribute 注释在会话中创建一个计数变量。并且每当他们提交时,您将再次将计数变量设置为默认值。
推荐阅读
- mysql - 每个客户一个实例 - Sequelize 和 MySQL
- r - 从 R 中的数据框中替换或删除 � 和 "\xd0"
- python - Osmnx 将大图保存为 graphml
- html - HTML/CSS - 处理拒绝垂直居中的内容
- javascript - 在 Javascript 数组中查找和更改项目
- android - 如何更改在覆盖函数中使用的扩展类的参数?科特林
- .net - 迁移到 paket:仅使用 Windows
- apache-spark - 具有微服务和火花流的软件工程流数据管道 (ETL)
- python - 如何以编程方式构造和执行配置文件中指定的可调用对象的 DAG
- json - Kotlinx.Serialization 使用 OkHTTPClient 总是返回失败