Spring 使用 Thymeleaf 和 MVC 引导将模型添加到视图

Spring Boot Adding Model to the View with Thymeleaf and MVC

好的,所以我尝试使用 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 对象,我正试图将其 return 作为列表

@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

是的,这应该很简单,这里是例外:

在 null

上找不到

属性 或字段 'question'

Spring EL 尝试评估以下内容:

   <div th:each="Set : ${questionAnswerSets}"   th:object="${questionAnswerSet}">
       <span th:text="${questionAnswerSet.question}"></span>
   </div>

并且无法找到 questionAnswerSet,它为空,因此出现错误。

使用这样的东西:

<div th:each="questionAnswerSet : ${questionAnswerSets}">
        <span th:text="${questionAnswerSet.question}"></span>
</div>

参考文档: http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#using-theach