重定向到页面一定次数 - Spring Boot
Posted
技术标签:
【中文标题】重定向到页面一定次数 - Spring Boot【英文标题】:Redirect to page a certain amount of times - Spring Boot 【发布时间】:2021-01-05 18:35:40 【问题描述】:我正在尝试制作测验创建应用程序。 一开始,我要求用户输入测验的标题、描述和包含在测验中的问题数量。 根据我想将用户重定向到'问题和答案'页面的问题数量。我想添加另一个名为 '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 变量的方式是错误的,但它只是为了提供一个想法。如果有人可以帮助我澄清我的问题,我将不胜感激。 提前谢谢你。
【问题讨论】:
【参考方案1】:您可以使用@SessionAttribute 注释在会话中创建计数变量。 并且每当他们提交时,您将再次将 count 变量设置为默认值。
【讨论】:
以上是关于重定向到页面一定次数 - Spring Boot的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Spring Boot 不会重定向到 403 上的 Keycloak 登录页面?
spring boot项目在eclipse里跑可以访问登录页,但是打jar包后访问登录页报重定向次数过多
执行登录保存请求后,Spring Boot Security POST 重定向到登录页面