大家好! 这是我的第一篇文章,请谅解。我在github的第一个应用程序中的代码有问题。我有视图,尝试在数据库中添加带有答案的问题。两种模型都以一个视图的形式创建。发布表单时,我得到这样的对象:
NewQuestionDto (
question = QuestionDto (
id=null,
question=zzz,
points=12,
difficultyLevel=ADVANCED,
createdAt=2018-08-24,
categoryDto = CategoryDto (
id=1,
name=null
),
authorDto = UserDto (
id=1,
name=null,
surname=null,
age=null,
email=null,
gender=null,
points=0,
role=USER,
createdAt=2018-08-24
)
),
answers=[
AnswerDto(id=null, answer=q, correctAnswer=false, questionDto=null), AnswerDto(id=null, answer=w, correctAnswer=true, questionDto=null), AnswerDto(id=null, answer=e, correctAnswer=false, questionDto=null), AnswerDto(id=null, answer=r, correctAnswer=false, questionDto=null)
])
我不知道为什么answerDto中的questionDto对象为空?
MyController:
@GetMapping("/question/add")
public String addQuestionGet(Model model){
NewQuestionDto newQuestionDto = NewQuestionDto.builder()
.question(new QuestionDto())
.answers(Arrays.asList(
new AnswerDto(),
new AnswerDto(),
new AnswerDto(),
new AnswerDto()
))
.build();
model.addAttribute("newQuestion", newQuestionDto);
model.addAttribute("difficultyLevels", DifficultyLevel.values());
model.addAttribute("categories", categoryService.findAllCategories());
model.addAttribute("authors", userService.findAllUsersWithAdminRole());
return "/dashboard/question/add";
}
@PostMapping("/question/add")
public String addQuestionPost(@ModelAttribute NewQuestionDto newQuestionDto){
System.out.println(newQuestionDto);
questionService.addOrUpdateQuestion(newQuestionDto.getQuestion());
for (AnswerDto a : newQuestionDto.getAnswers()){
answerService.addOrUpdateAnswer(a);
}
return "redirect:/dashboard/questions/all";
}
NewQuestionDto
public class NewQuestionDto {
private QuestionDto question;
private List<AnswerDto> answers;
}
我的HTML文件:
<form method="POST" th:action="@{/dashboard/question/add}" th:object="${newQuestion}">
<!--Some code here-->
<h4 class="muted mt-4">Answers</h4>
<hr>
<div class="form-group mt-3">
<div class="form-inline mt-1" th:each="a, stat : *{answers}">
<input class="form-control mr-2" type="text" th:placeholder="Answer + ' ' + ${stat.index}" th:field="*{answers[__${stat.index}__].answer}" autocomplete="off"/>
<input type="checkbox" th:field="*{answers[__${stat.index}__].correctAnswer}"/>
</div>
</div>
<!--Buttons-->
<div class="form-group mt-3">
<button type="submit" class="btn btn-success btn-block">ADD</button>
<button type="reset" class="btn btn-outline-danger btn-block">RESET</button>
</div>
</form>
如果您需要我的应用程序中的更多代码,请告诉我。
谢谢!
已编辑
AnswerServiceImpl
@Override
public void addOrUpdateAnswer(AnswerDto answerDto) {
answerRepository.addOrUpdate(ConverterDto.fromAnswerDtoToAnswer(answerDto));
}
QuestionServiceImpl
@Override
public void addOrUpdateQuestion(QuestionDto questionDto) {
questionRepository.addOrUpdate(ConverterDto.fromQuestionDtoToQuestion(questionDto));
}
ConverterDTO
public static Answer fromAnswerDtoToAnswer(AnswerDto answerDto){
return answerDto == null ? null : new Answer(
answerDto.getId(),
answerDto.getAnswer(),
answerDto.getCorrectAnswer(),
fromQuestionDtoToQuestion(answerDto.getQuestionDto())
);
}
public static Question fromQuestionDtoToQuestion(QuestionDto questionDto){
return questionDto == null ? null : new Question(
questionDto.getId(),
questionDto.getQuestion(),
questionDto.getPoints(),
questionDto.getDifficultyLevel(),
questionDto.getCreatedAt(),
fromCategoryDtoToCategory(questionDto.getCategoryDto()),
fromUserDtoToUser(questionDto.getAuthorDto()),
new HashSet<>(),
new HashSet<>()
);
}