问题描述
限时送ChatGPT账号..好的,所以我正在尝试使用百里香叶、弹簧靴和 jpa 将模型中对象的属性作为列表放到视图中,我已经阅读了几个小时的代码,但我似乎无法理解为了发现我的问题,同样在同一个应用程序中,我有一个非常相似的功能在工作,所以我有点知道该怎么做,但我似乎无法弄清楚这个问题.我不断收到错误在空值上找不到属性或字段问题"
.我不知道我哪里出错了.我拥有的对象叫做QuestionAnswerSet,我在数据库中有一个问题字符串和一个答案字符串,我可以通过应用程序提交,所以这不是数据库的问题.我的 pom 文件一切都很好,因为正如我之前所说,我已经完成了一个非常相似的功能.
这是我的控制器.
@Controller公共类 QuestionAnswerSetController{私人 QuestionAnswerSetRepository questionAnswerSetRepo;@RequestMapping("设置")公共字符串集(ModelMap 模型){列表<问题答案集>questionAnswerSets = questionAnswerSetRepo.findAll();model.put("questionAnswerSets", questionAnswerSets);返回集合";}@RequestMapping(value="editSet/{questionAnswerSetId}", method=RequestMethod.GET)public String editSetGet (@PathVariable Long questionAnswerSetId, ModelMap 模型){返回 "editCourse";}@RequestMapping(value="createSet", method=RequestMethod.GET)public String createSetGet(ModelMap 模型){QuestionAnswerSet questionAnswerSet = new QuestionAnswerSet();model.put("questionAnswerSet", questionAnswerSet);返回创建集";}@RequestMapping(value="createSet", method=RequestMethod.POST)public String createSetPost (@ModelAttribute QuestionAnswerSet questionAnswerSet, ModelMap 模型){questionAnswerSetRepo.save(questionAnswerSet);返回重定向:/集";}@自动连线公共无效 setQuestionAnserSetRepo(QuestionAnswerSetRepository questionAnserSetRepo) {this.questionAnswerSetRepo = questionAnserSetRepo;}}
这是我的 html
<span th:text="${questionAnswerSet.question}"></span><div th:if="${#lists.isEmpty(questionAnswerSets)}">没有要显示的集合.
这是我的存储库,它非常标准,尽管我会包含它
public interface QuestionAnswerSetRepository extends JpaRepository{}
这是我的 QuestionAnswerSet.java 对象,这是我试图作为列表返回的内容
@Entity公共类问题答案集{私人长ID;私人字符串问题;私人字符串答案;私人用户用户;@ID@GeneratedValue公共长 getId() {返回标识;}公共无效setId(长ID){this.id = id;}公共字符串 getQuestion() {返回问题;}public void setQuestion(字符串问题){this.question = 问题;}公共字符串 getAnswer() {返回答案;}公共无效setAnswer(字符串答案){this.answer = 答案;}@ManyToOne公共用户 getUser() {返回用户;}公共无效设置用户(用户用户){this.user = 用户;}}
这是我控制台中的错误
org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): 在 null 上找不到属性或字段问题"
解决方案 是的,这应该很简单,这里有一个例外:
在 null 上找不到属性或字段问题"
Spring EL 尝试评估以下内容:
<span th:text="${questionAnswerSet.question}"></span>
并且它无法找到 questionAnswerSet ,它是 null 因此错误.
使用这样的东西:
<span th:text="${questionAnswerSet.question}"></span>
参考文档:http://www.thymeleaf/doc/tutorials/2.1/usingthymeleaf.html#using-theach
Ok, so I'm trying to put an attribute of an object from the model to the view as a list using thymeleaf, spring boot and jpa, I've been reading over the code for hours and I can't seem to spot my problem, also in the same application I have a very similar function working so I sort of know how to do it, but I just cannot seem to figure out this one. I keep getting an error Property or field 'question' cannot be found on null
. I have no idea where I'm going wrong. The object I'm have is called QuestionAnswerSet, and I have a question string and an answer string in the database, that I can submit through the app, so it's not a problem with the database. Also everything is good with my pom file because as I said earlier I have done a very similar function.
Here's my controller.
@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;
}
}
Here's my 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>
Here's my repository, it's pretty standard, just though I would include it
public interface QuestionAnswerSetRepository extends JpaRepository<QuestionAnswerSet, Long> {
}
And here's my QuestionAnswerSet.java object, which is what I'm trying to return as a list
@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;
}
}
And Here's the error in my console
org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'question' cannot be found on null
解决方案
Yup, that should be pretty straightforward, here is the exception :
Property or field 'question' cannot be found on null
Spring EL tries to evaluate the below :
<div th:each="Set : ${questionAnswerSets}" th:object="${questionAnswerSet}">
<span th:text="${questionAnswerSet.question}"></span>
</div>
And it is unable to find questionAnswerSet ,which is null hence the error.
Use something like this :
<div th:each="questionAnswerSet : ${questionAnswerSets}">
<span th:text="${questionAnswerSet.question}"></span>
</div>
Refer Doc : http://www.thymeleaf/doc/tutorials/2.1/usingthymeleaf.html#using-theach
这篇关于Spring Boot 使用 Thymeleaf 和 MVC 向视图添加模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论