【发布时间】:2015-11-13 03:54:16
【问题描述】:
好的,所以我正在尝试使用 thymeleaf、spring boot 和 jpa 将模型中的对象属性作为列表放入视图中,我已经阅读了几个小时的代码,但我似乎无法为了发现我的问题,同样在同一个应用程序中,我有一个非常相似的功能,所以我有点知道该怎么做,但我似乎无法弄清楚这个。我不断收到错误Property or field 'question' cannot be found on null。我不知道我哪里出错了。我拥有的对象称为 QuestionAnswerSet,我在数据库中有一个问题字符串和一个答案字符串,我可以通过应用程序提交,所以数据库没有问题。我的 pom 文件也一切正常,因为正如我之前所说,我已经完成了一个非常相似的功能。
这是我的控制器。
@Controller
public class QuestionAnswerSetController
{
private QuestionAnswerSetRepository questionAnswerSetRepo;
@RequestMapping("sets")
public String sets (ModelMap model)
{
List<QuestionAnswerSet> questionAnswerSets = questionAnswerSetRepo.findAll();
model.put("questionAnswerSets", questionAnswerSets);
return "sets";
}
@RequestMapping(value="editSet/{questionAnswerSetId}", method=RequestMethod.GET)
public String editSetGet (@PathVariable Long questionAnswerSetId, ModelMap model)
{
return "editCourse";
}
@RequestMapping(value="createSet", method=RequestMethod.GET)
public String createSetGet (ModelMap model)
{
QuestionAnswerSet questionAnswerSet = new QuestionAnswerSet();
model.put("questionAnswerSet", questionAnswerSet);
return "createSet";
}
@RequestMapping(value="createSet", method=RequestMethod.POST)
public String createSetPost (@ModelAttribute QuestionAnswerSet questionAnswerSet, ModelMap model)
{
questionAnswerSetRepo.save(questionAnswerSet);
return "redirect:/sets";
}
@Autowired
public void setQuestionAnserSetRepo(QuestionAnswerSetRepository questionAnserSetRepo) {
this.questionAnswerSetRepo = questionAnserSetRepo;
}
}
这是我的html
<div th:each="Set : ${questionAnswerSets}" th:object="${questionAnswerSet}">
<span th:text="${questionAnswerSet.question}"></span>
</div>
<div th:if="${#lists.isEmpty(questionAnswerSets)}">
There is no sets to display.
</div>
这是我的存储库,它非常标准,尽管我会包含它
public interface QuestionAnswerSetRepository extends JpaRepository<QuestionAnswerSet, Long> {
}
这是我的 QuestionAnswerSet.java 对象,这是我试图以列表形式返回的对象
@Entity
public class QuestionAnswerSet {
private Long id;
private String question;
private String answer;
private User user;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
@ManyToOne
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
这是我控制台中的错误
org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'question' cannot be found on null
【问题讨论】:
标签: spring-boot thymeleaf spring-el