我目前正在用spring jpa和thymeleaf构建一个webservice。
我的模型'Question.class'与我的模型'Answer.class'有一个onetmany关系:
@OneToMany(targetEntity = Answer.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "questionAnswerId")
private List<Answer> answers;
也有我的'Answer.class'与'Question.class'的ManyToOne关系:
@ManyToOne(targetEntity = Question.class, fetch = FetchType.LAZY)
@JoinColumn(name="question_answer_id")
private Question questionAnswerId;
我的控制器方法来更新实体问题:
@PostMapping(value = "/frageDetail/updateQuestion/{id}")
public String updateQuestion(@PathVariable(name = "id") int id,
@ModelAttribute(value="question") Question questionDetails) {
Question temp = questionRepository.findById(id);
temp.setQuestionName(questionDetails.getQuestionName());
temp.setQuestionDescription(questionDetails.getQuestionDescription());
temp.setQuestionImage(questionDetails.getQuestionImage());
temp.setQuestionKo(questionDetails.isQuestionKo());
temp.setQuestionTimelimit(questionDetails.getQuestionTimelimit());
//temp.setAnswers(questionDetails.getAnswers());
questionRepository.save(temp);
return "redirect:/fragenAdministration";
}
我的观点:
<form action="#" th:action="@{/frageDetail/updateQuestion/{id}(id=${question.id})}" th:object="${question}" th:method="post">
<div class="input">
<input type="text" placeholder="Fragename" class="form-control" style="margin-bottom: 20px" th:field="*{questionName}">
</div>
<div style="margin-bottom: 20px">
<textarea style="resize: none;" class="form-control" placeholder="Frage Beschreibung" rows="6" th:field="*{questionDescription}"> </textarea>
</div>
<div class="form-group" style="margin-top:45px;">
<div class="checkbox" th:each="answer : ${answerList}">
<!-- works, but wont update to database -->
<input type="checkbox" th:checked="${answer.answerOk}" th:text="${answer.answerName}">
<!-- error, Invalid property 'answer' of bean class [com.example.demo.model.Question]:
Bean property 'answer' is not readable or has an invalid getter method: Does the return
type of the getter match the parameter type of the setter? -->
<input type="checkbox" th:field="*{answer.answerOk}" th:text="${answer.answerName}">
</div>
<div class="checkbox">
<label><input type="checkbox" th:field="*{questionKo}">KO Frage</label>
</div>
<div class="form-group">
<label for="timeLimit" class="col-sm-2 control-label" style="text-align: left;">ZeitLimit</label>
<div class="col-sm-10">
<input type="time" class="form-control" id="kflgreen" th:field="*{questionTimelimit}">
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<button class="btn btn-success btn-block">Save</button>
</div>
</div>
</div>
</form>
一切正常,除了布尔值'answerOk'的映射,这是'Answer.class'中的一个字段。使用:checked="${answer.answerOk}",如果answerOk为真,则复选框被选中;这是需要的。但要在取消复选框时更新数据库中的值,必须使用th:字段(如th:field="*{questionKo}";)这对这个和所有其他字段都很好)
问题是,当我使用:field="*{answer.answerOk}"而不是:checked="${answer . answerok}",我得到错误消息:
"org.springframework.beans。NotReadablePropertyException: bean类[com.example.demo.model.Question]的无效属性'answer': bean属性'answer'不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配?
当然在Answer.class和Question.class中都有getter和setter
所以有没有人知道如何绑定字段answerOk,所以它会得到更新的数据库,以及当我检查/取消检查它?
非常感谢!
您必须将选择变量表达式(*{…})与' th:object = ${…}结合使用。
在您的例子中,*{answerOk}
解析为${answer.answerOk}
<div class="checkbox" th:each="answer : ${answerList}">
<input type="checkbox" th:field="*{answerOk}" th:text="${answer.answerName}">
</div>
https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html standard-expression-syntax