【问题标题】:Spring Boot Adding Model to the View with Thymeleaf and MVCSpring Boot 使用 Thymeleaf 和 MVC 将模型添加到视图中
【发布时间】: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


    【解决方案1】:

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

    在 null 上找不到属性或字段“问题”

    Spring EL 尝试评估以下内容:

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

    并且它无法找到 questionAnswerSet ,它是 null 因此错误。

    使用这样的东西:

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

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

    【讨论】:

    • 工作完美。谢谢!
    猜你喜欢
    • 2017-06-21
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 2019-03-12
    • 2017-09-10
    相关资源
    最近更新 更多