这是我的用户回答实体:
package com.example.demo.entities;
import org.junit.ClassRule;
import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
@Entity
@Table(name = "user_answer")
public class UserAnswer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long device_id;
private Integer quiz_id;
private String questionNum;
private String answerNum;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
// 0, 1, 2, 3, 4 corresponding to A, B, C, D, E
public Long getDevice_id() {
return device_id;
}
public void setDevice_id(Long device_id) {
this.device_id = device_id;
}
public long getQuiz_id() {
return quiz_id;
}
public void setQuiz_id(Integer quiz_id) {
this.quiz_id = quiz_id;
}
public String getQuestionNum() {
return questionNum;
}
public void setQuestionNum(String questionNum) {
this.questionNum = questionNum;
}
public String getAnswerNum() {
return answerNum;
}
public void setAnswerNum(String answerNum) {
this.answerNum = answerNum;
}
}
下面是我做的:
@RequestMapping("/test")
public String viewTest(@ModelAttribute UserAnswer answer, @AuthenticationPrincipal CustomUserDetails userDetails,
HttpSession session, @RequestParam(name = "q", required = false, defaultValue = "0") String qNum,
@RequestParam(name = "id", required = false, defaultValue = "1") String quizID, Model model,
HttpServletRequest req) {
int index = Integer.parseInt(qNum) + 1;
JSONObject question = (JSONObject) HttpController.GetQuestion(quizID, qNum);
if (question != null) {
model.addAttribute("problem", question.get("problem"));
model.addAttribute("answer0", question.get("answer0"));
model.addAttribute("answer1", question.get("answer1"));
model.addAttribute("answer2", question.get("answer2"));
model.addAttribute("answer3", question.get("answer3"));
model.addAttribute("url", "/test?id=" + quizID + "&q=" + (index));
model.addAttribute("qNum", "" + index);
UserAnswer userAnswer = (UserAnswer) session.getAttribute("user_answer");
if (userAnswer != null) {
String previousQuesiton = userAnswer.getQuestionNum();
System.out.println("Previous question: " + previousQuesiton);
String previousAnswer = userAnswer.getAnswerNum();
if (previousAnswer == null) {
previousAnswer = "";
}
System.out.println("Previous answer: " + previousAnswer);
String newQuestion = qNum;
String newAnswer = answer.getAnswerNum();
newQuestion = previousQuesiton + newQuestion;
System.out.println("New question: " + newQuestion);
newAnswer = previousAnswer + newAnswer;
System.out.println("New answer: " + newAnswer);
answer.setAnswerNum(newAnswer);
answer.setQuiz_id(Integer.parseInt(quizID));
answer.setQuestionNum(newQuestion);
req.getSession().setAttribute("user_answer",answer);
return "test.html";
} else
answer.setQuestionNum(qNum);
req.getSession().setAttribute("user_answer",answer);
return "test.html";
} else {
// Cookie[] cookies = req.getCookies();
// UserAnswer userAnswer = (UserAnswer) req.getSession().getAttribute("user_answer");
// String previousAnswer = userAnswer.getAnswerNum();
// String newAnswer = previousAnswer + answer.getAnswerNum();
// userAnswer.setAnswerNum(newAnswer);
if (userDetails != null) {
UserAnswer userAnswer = (UserAnswer) req.getSession().getAttribute("user_answer");
String previousAnswer = userAnswer.getAnswerNum();
String newAnswer = previousAnswer + answer.getAnswerNum();
userAnswer.setAnswerNum(newAnswer);
System.out.println("user login");
userAnswer.setDevice_id(userDetails.getDeviceID());
userAnswerRepository.save(userAnswer);
} else{
UserAnswer not_login_userAnswer = (UserAnswer) req.getSession().getAttribute("user_answer");
String previousAnswer = not_login_userAnswer.getAnswerNum();
String newAnswer = previousAnswer + answer.getAnswerNum();
not_login_userAnswer.setAnswerNum(newAnswer);
Long deviceID = getCookie(req);
not_login_userAnswer.setDevice_id(deviceID);
userAnswerRepository.save(not_login_userAnswer);
}
req.getSession().removeAttribute("user_answer");
System.out.println("Question not found");
// go to results page
return "ThankForTakingTest.html";
}
}
这是我的属性文件:
spring.jpa.hibernate.ddl-auto= update
我想要的实现是:
当我完成测验时,我想用访客用户的设备id将所有答案和问题编号保存在user\u answer数据库中。此外,在用户答案表中,它也有自己的id,这就是我这样做的原因,因为我想确保当新用户来做我的测试时,他们将有他们的新设备id,当他们完成所有的问题时,他们的所有答案将保存在用户答案中。
暂无答案!
目前还没有任何答案,快来回答吧!